Skip to content

Commit

Permalink
Merge branch 'main' of github.com:DragonOS-Community/NovaShell
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed Aug 28, 2024
2 parents a004a27 + 5b85994 commit 2f9f3ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 45 deletions.
43 changes: 8 additions & 35 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ 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;
}

let command_bytes = self.printer.buf.borrow().clone();

// 如果命令不以空格开头且不跟上一条命令相同,这条命令会被记录

// 如果命令不以空格开头且不跟上一条命令相同,这条命令会被记录
if !command_bytes.is_empty()
&& !command_bytes.starts_with(&[b' '])
Expand All @@ -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);
Expand Down Expand Up @@ -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<String>) {
let mut candidates: Vec<String> = Vec::new();
for BuildInCmd(cmd) in BuildInCmd::BUILD_IN_CMD {
Expand All @@ -401,16 +385,6 @@ pub fn complete_path(incomplete_path: &str) -> (&str, Vec<String>) {
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();
Expand All @@ -424,7 +398,6 @@ pub fn complete_path(incomplete_path: &str) -> (&str, Vec<String>) {
candidates.push(file_name);
}
}
// }
}

return (dir, candidates);
Expand Down
34 changes: 24 additions & 10 deletions src/shell/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -73,7 +72,6 @@ impl Printer {
.unwrap();
}
self.cursor += bytes.len();
// self.print(buf[self.cursor..].to_vec());

stdout().flush().unwrap();
}
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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());
}

0 comments on commit 2f9f3ad

Please sign in to comment.