diff --git a/src/shell/mod.rs b/src/shell/mod.rs index e8dd13e..ae42a6a 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -52,13 +52,17 @@ impl Shell { } pub fn exec(&mut self) { + // 开启终端raw模式 // 开启终端raw模式 crossterm::terminal::enable_raw_mode().expect("failed to enable raw mode"); + // 循环读取一行 + // 循环读取一行 loop { self.printer.init_before_readline(); // 读取一行 + // 读取一行 if self.readline() == 0 { println!(); break; @@ -66,6 +70,8 @@ impl Shell { let command_bytes = self.printer.buf.borrow().clone(); + // 如果命令不以空格开头且不跟上一条命令相同,这条命令会被记录 + // 如果命令不以空格开头且不跟上一条命令相同,这条命令会被记录 if !command_bytes.is_empty() && !command_bytes.starts_with(&[b' ']) @@ -82,6 +88,8 @@ impl Shell { self.write_commands(&command_bytes); }; + // 命令不为空,执行命令 + // 命令不为空,执行命令 if !command_bytes.iter().all(|&byte| byte == b' ') { self.exec_commands_in_line(&command_bytes); @@ -356,30 +364,6 @@ impl WindowSize { } } -// 测试终端颜色显示效果 -#[allow(dead_code)] -pub fn _print_color_example() { - let example = "abcdefghijklmnopqrstuvwxyz"; - println!("{}", example.bright_black()); - println!("{}", example.bright_blue()); - println!("{}", example.bright_cyan()); - println!("{}", example.bright_green()); - println!("{}", example.bright_magenta()); - println!("{}", example.bright_purple()); - println!("{}", example.bright_red()); - println!("{}", example.bright_white()); - println!("{}", example.bright_yellow()); - println!("{}", example.black()); - println!("{}", example.blue()); - println!("{}", example.cyan()); - println!("{}", example.green()); - println!("{}", example.magenta()); - println!("{}", example.purple()); - println!("{}", example.red()); - println!("{}", example.white()); - println!("{}", example.yellow()); -} - pub fn complete_command(command: &str) -> (&str, Vec) { let mut candidates: Vec = Vec::new(); for BuildInCmd(cmd) in BuildInCmd::BUILD_IN_CMD { @@ -401,16 +385,6 @@ pub fn complete_path(incomplete_path: &str) -> (&str, Vec) { incomplete_name = incomplete_path; } if let Ok(read_dir) = fs::read_dir(if dir.is_empty() { "." } else { dir }) { - // if incomplete_name == "" { - // for entry in read_dir { - // let entry = entry.unwrap(); - // let mut file_name = entry.file_name().into_string().unwrap(); - // if entry.file_type().unwrap().is_dir() { - // file_name.push('/'); - // } - // candidates.push(file_name); - // } - // } else { for entry in read_dir { let entry = entry.unwrap(); let mut file_name = entry.file_name().into_string().unwrap(); @@ -424,7 +398,6 @@ pub fn complete_path(incomplete_path: &str) -> (&str, Vec) { candidates.push(file_name); } } - // } } return (dir, candidates); diff --git a/src/shell/printer.rs b/src/shell/printer.rs index ba4fbea..73c5097 100644 --- a/src/shell/printer.rs +++ b/src/shell/printer.rs @@ -40,7 +40,6 @@ impl Printer { self.buf = Rc::new(RefCell::new(Vec::new())); self.prompt.update_path(); self.print_prompt(); - // self.cursor = crossterm::cursor::position().map_or(0, |(top, left)| left as usize); self.cursor = 0; } @@ -73,7 +72,6 @@ impl Printer { .unwrap(); } self.cursor += bytes.len(); - // self.print(buf[self.cursor..].to_vec()); stdout().flush().unwrap(); } @@ -114,14 +112,6 @@ impl Printer { } } - // pub fn flush_cursor(&self) { - // crossterm::execute!( - // io::stdout(), - // crossterm::cursor::MoveToColumn((self.cursor + self.prompt.len()) as u16) - // ) - // .unwrap(); - // } - pub fn cursor_left(&mut self, len: usize) { if self.cursor > 0 { crossterm::execute!( @@ -214,3 +204,27 @@ impl fmt::Display for Prompt { ) } } + +// 测试终端颜色显示效果 +#[allow(dead_code)] +pub fn _print_color_example() { + let example = "abcdefghijklmnopqrstuvwxyz"; + println!("{}", example.bright_black()); + println!("{}", example.bright_blue()); + println!("{}", example.bright_cyan()); + println!("{}", example.bright_green()); + println!("{}", example.bright_magenta()); + println!("{}", example.bright_purple()); + println!("{}", example.bright_red()); + println!("{}", example.bright_white()); + println!("{}", example.bright_yellow()); + println!("{}", example.black()); + println!("{}", example.blue()); + println!("{}", example.cyan()); + println!("{}", example.green()); + println!("{}", example.magenta()); + println!("{}", example.purple()); + println!("{}", example.red()); + println!("{}", example.white()); + println!("{}", example.yellow()); +}