From 46fb31144dc84542de735dc2dd1a5ad84d1efbee Mon Sep 17 00:00:00 2001 From: MemoryShore <105195940+MemoryShore@users.noreply.github.com> Date: Wed, 3 Apr 2024 17:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=8A=A8=E8=A1=A5?= =?UTF-8?q?=E5=85=A8=E8=B7=AF=E5=BE=84=E9=94=99=E8=AF=AF=20(#37)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shell/command/mod.rs | 17 +++----- src/shell/mod.rs | 84 ++++++++++++++++++---------------------- 2 files changed, 42 insertions(+), 59 deletions(-) diff --git a/src/shell/command/mod.rs b/src/shell/command/mod.rs index 3ea8a47..860d998 100644 --- a/src/shell/command/mod.rs +++ b/src/shell/command/mod.rs @@ -1,20 +1,11 @@ -use colored::Colorize; use help::Help; use path_clean::PathClean; use regex::{Captures, Regex}; use std::intrinsics::unlikely; use std::io::Read; -use std::{ - format, - fs::{self, File, OpenOptions}, - io::Write, - path::Path, - print, println, - string::String, - vec::Vec, -}; - -use crate::env::{Env, ENV_FILE_PATH, ROOT_PATH}; +use std::{format, fs::File, path::Path, print, println, string::String, vec::Vec}; + +use crate::env::{Env, ROOT_PATH}; use crate::shell::Shell; mod help; @@ -31,6 +22,7 @@ pub struct Command { cmd_type: CommandType, } +#[allow(dead_code)] #[derive(Debug, PartialEq, Eq, Clone)] pub enum CommandError { CommandNotFound(String), @@ -497,6 +489,7 @@ impl Shell { } } + #[allow(dead_code)] fn is_file_or_dir(&self, path_str: &String) -> Result { match self.path_format(path_str) { Ok(path_str) => Ok(path_str), diff --git a/src/shell/mod.rs b/src/shell/mod.rs index 6620227..b41f5af 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -226,7 +226,7 @@ impl Shell { let mut stack: String = String::with_capacity(str.len()); let mut left_quote: char = ' '; for ch in iter { - //存在未闭合的左引号,此时除能够配对的引号外,任何字符都加入栈中 + //存在未闭合的左引号,此时包括空格的任何字符都加入栈中,直到匹配到右引号 if left_quote != ' ' { if ch == left_quote { left_quote = ' '; @@ -262,7 +262,7 @@ impl Shell { let mut target_fragment = fragments.last().unwrap().clone(); target_fragment = target_fragment.replace("\'", "").replace("\"", ""); - let candidates = if fragments.len() < 2 { + let (prefix, candidates) = if fragments.len() < 2 { //补全命令 complete_command(&target_fragment) } else { @@ -277,7 +277,8 @@ impl Shell { self.printer.cursor -= old_fragment.len(); self.printer.flush_cursor(); self.printer.delete(old_fragment.len()); - self.printer.insert(candidate.as_bytes()); + self.printer + .insert(format!("{}{}", prefix, candidate).as_bytes()); } 2.. => { let old_cursor = self.printer.cursor; @@ -448,6 +449,8 @@ impl Printer { } } +// 测试终端颜色显示效果 +#[allow(dead_code)] pub fn _print_color_example() { let example = "abcdefghijklmnopqrstuvwxyz"; println!("{}", example.bright_black()); @@ -470,65 +473,52 @@ pub fn _print_color_example() { println!("{}", example.yellow()); } -pub fn complete_command(command: &str) -> Vec { +pub fn complete_command(command: &str) -> (&str, Vec) { let mut candidates: Vec = Vec::new(); for BuildInCmd(cmd) in BuildInCmd::BUILD_IN_CMD { if cmd.starts_with(command) { candidates.push(String::from(*cmd)); } } - candidates + ("", candidates) } -pub fn complete_path(incomplete_path: &str) -> Vec { - let current_dir = std::env::current_dir().unwrap(); - let current_dir = current_dir.to_str().unwrap(); - let path = if incomplete_path.starts_with('/') { - String::from(incomplete_path) - } else { - format!("{}/{}", current_dir, incomplete_path) - }; - +pub fn complete_path(incomplete_path: &str) -> (&str, Vec) { let mut candidates: Vec = Vec::new(); - let dir: &str; + let mut dir = ""; let incomplete_name: &str; - if let Some(index) = path.rfind('/') { - dir = &path[..=index]; - if index < path.len() { - incomplete_name = &path[index + 1..]; - } else { - incomplete_name = ""; - } + if let Some(index) = incomplete_path.rfind('/') { + dir = &incomplete_path[..=index]; + incomplete_name = &incomplete_path[index + 1..]; } else { - dir = "."; - incomplete_name = &path[..]; + incomplete_name = incomplete_path; } - match fs::read_dir(dir) { - Ok(read_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); + 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(); + if file_name.starts_with(incomplete_name) { + if file_name.contains(' ') { + file_name = format!("\'{}\'", file_name); } - } else { - for entry in read_dir { - let entry = entry.unwrap(); - let mut file_name = entry.file_name().into_string().unwrap(); - if file_name.starts_with(incomplete_name) { - if entry.file_type().unwrap().is_dir() { - file_name.push('/'); - } - candidates.push(file_name); - } + if entry.file_type().unwrap().is_dir() { + file_name.push('/'); } + candidates.push(file_name); } } - - Err(_) => {} + // } } - return candidates; + + return (dir, candidates); }