Skip to content

Commit

Permalink
修复后台命令运行时工作路径不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShore committed Oct 22, 2024
1 parent 7bb802a commit 3915bef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::{

use regex::Regex;

use crate::env::EnvManager;

#[derive(Debug)]
pub enum Token {
Word(String), // 普通的命令或选项
Expand Down Expand Up @@ -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());
}
Expand Down
32 changes: 17 additions & 15 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
};

use crate::{
env::EnvManager,
keycode::{FunctionKeySuffix, SpecialKeycode},
parser::{Parser, Pipeline},
};
Expand All @@ -31,7 +32,7 @@ pub struct Shell {
history_commands: Vec<Rc<RefCell<Vec<u8>>>>,
history_path: String,
printer: Printer,
backend_thread: ThreadManager<Pipeline, Child>,
backend_thread: ThreadManager<(String, Vec<Pipeline>), Child>,
}

impl Shell {
Expand All @@ -50,15 +51,18 @@ impl Shell {
shell
}

fn create_backend_thread() -> ThreadManager<Pipeline, Child> {
fn create_backend_thread() -> ThreadManager<(String, Vec<Pipeline>), Child> {
ThreadManager::new(|| {
let (p_s, c_r) = std::sync::mpsc::channel::<Pipeline>();
let (p_s, c_r) = std::sync::mpsc::channel::<(String, Vec<Pipeline>)>();
let (c_s, p_r) = std::sync::mpsc::channel::<Child>();
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);
}
}
};
};
Expand Down Expand Up @@ -108,28 +112,26 @@ impl Shell {
}

fn exec_commands_in_line(&mut self, command_bytes: &Vec<u8>) {
// 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
Expand Down
14 changes: 4 additions & 10 deletions src/shell/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{

use colored::Colorize;

use crate::env::EnvManager;

pub struct Printer {
/// 提示语
pub prompt: Prompt,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 3915bef

Please sign in to comment.