From 3915bef7d561d62f8bc9f6fc5a473776387f4edf Mon Sep 17 00:00:00 2001 From: MemoryShore <1353318529@qq.com> Date: Wed, 23 Oct 2024 02:32:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8E=E5=8F=B0=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=BF=90=E8=A1=8C=E6=97=B6=E5=B7=A5=E4=BD=9C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser.rs | 4 +++- src/shell/mod.rs | 32 +++++++++++++++++--------------- src/shell/printer.rs | 14 ++++---------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index cb8750d..2c0bf6b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,6 +8,8 @@ use std::{ use regex::Regex; +use crate::env::EnvManager; + #[derive(Debug)] pub enum Token { Word(String), // 普通的命令或选项 @@ -533,7 +535,7 @@ impl Pipeline { Ok(real_path) => { let mut child_command = std::process::Command::new(real_path); child_command.args(cmd.args.clone()); - child_command.current_dir(std::env::current_dir().unwrap()); + child_command.current_dir(EnvManager::current_dir()); if stdout.is_some() { child_command.stdin(stdout.take().unwrap().as_std()); } diff --git a/src/shell/mod.rs b/src/shell/mod.rs index bd9d781..8a8ba28 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -9,6 +9,7 @@ use std::{ }; use crate::{ + env::EnvManager, keycode::{FunctionKeySuffix, SpecialKeycode}, parser::{Parser, Pipeline}, }; @@ -31,7 +32,7 @@ pub struct Shell { history_commands: Vec>>>, history_path: String, printer: Printer, - backend_thread: ThreadManager, + backend_thread: ThreadManager<(String, Vec), Child>, } impl Shell { @@ -50,15 +51,18 @@ impl Shell { shell } - fn create_backend_thread() -> ThreadManager { + fn create_backend_thread() -> ThreadManager<(String, Vec), Child> { ThreadManager::new(|| { - let (p_s, c_r) = std::sync::mpsc::channel::(); + let (p_s, c_r) = std::sync::mpsc::channel::<(String, Vec)>(); let (c_s, p_r) = std::sync::mpsc::channel::(); let map = BuildInCmd::map(); let func = move || loop { - if let Ok(pipeline) = c_r.recv() { - for child in pipeline.execute(map.clone()) { - let _ = c_s.send(child); + if let Ok((dir, pipelines)) = c_r.recv() { + std::env::set_current_dir(dir).expect("set current dir failed"); + for pipeline in pipelines { + for child in pipeline.execute(map.clone()) { + let _ = c_s.send(child); + } } }; }; @@ -108,28 +112,26 @@ impl Shell { } fn exec_commands_in_line(&mut self, command_bytes: &Vec) { - // match Command::parse(String::from_utf8(command_bytes.clone()).unwrap()) { - // Ok(commands) => commands - // .into_iter() - // .for_each(|command| self.exec_command(command)), - // Err(e) => CommandError::handle(e), - // } - // 解析命令 let input_command = String::from_utf8(command_bytes.clone()).unwrap(); let pipelines = Parser::parse(&input_command).unwrap(); let mut foreground_pipelines = Vec::new(); + let mut backend_pipelines = Vec::new(); - // 后台pipeline发送给子线程执行 for pipeline in pipelines { if pipeline.backend() { - let _ = self.backend_thread.send(pipeline); + backend_pipelines.push(pipeline); } else { foreground_pipelines.push(pipeline); } } + // 后台pipeline发送给子线程执行 + let _ = self + .backend_thread + .send((EnvManager::current_dir(), backend_pipelines)); + crossterm::terminal::disable_raw_mode().expect("failed to disable raw mode"); // 顺序执行所有前台pipeline diff --git a/src/shell/printer.rs b/src/shell/printer.rs index 73c5097..5211f71 100644 --- a/src/shell/printer.rs +++ b/src/shell/printer.rs @@ -9,6 +9,8 @@ use std::{ use colored::Colorize; +use crate::env::EnvManager; + pub struct Printer { /// 提示语 pub prompt: Prompt, @@ -24,11 +26,7 @@ impl Printer { prompt: Prompt { computer_name: "DragonOS".to_string(), user_name: "root".to_string(), - path: std::env::current_dir() - .expect("Error getting current directory") - .to_str() - .unwrap() - .to_string(), + path: EnvManager::current_dir(), }, buf: Rc::clone(bytes), cursor: 0, @@ -186,11 +184,7 @@ impl Prompt { } pub fn update_path(&mut self) { - self.path = std::env::current_dir() - .unwrap() - .to_str() - .unwrap() - .to_string(); + self.path = EnvManager::current_dir() } }