|
1 | 1 | use boxutils::commands::Command; |
2 | 2 | use std::env; |
3 | | -use std::io::{self, ErrorKind, Write}; |
4 | | -use std::path::Path; |
5 | | -use std::process::Command as stdCommand; |
| 3 | +use std::io::{self, Write}; |
| 4 | +use std::process; |
| 5 | + |
| 6 | +use crate::built_in::{Action, run_if_exists}; |
6 | 7 |
|
7 | 8 | pub struct Ash; |
8 | 9 |
|
9 | 10 | impl Command for Ash { |
10 | 11 | fn execute(&self) { |
| 12 | + let mut path = env::current_dir().unwrap().display().to_string(); |
| 13 | + |
11 | 14 | loop { |
12 | | - let path = env::current_dir(); |
13 | 15 | let userchar = if boxutils::cross::user::is_admin() { |
14 | 16 | '#' |
15 | 17 | } else { |
16 | 18 | '$' |
17 | 19 | }; |
18 | | - print!("{} {} ", path.expect("unknown").display(), userchar); |
| 20 | + print!("{} {} ", path.clone(), userchar); |
19 | 21 | let _ = io::stdout().flush(); |
20 | 22 | let mut input = String::new(); |
21 | 23 | io::stdin().read_line(&mut input).unwrap(); |
22 | 24 | let mut full_cmd = input.trim().split_whitespace(); |
23 | | - let command = full_cmd.next().unwrap(); |
24 | | - let mut arguments = full_cmd; |
25 | | - match command { |
26 | | - "exit" => return, |
27 | | - "cd" => { |
28 | | - let new_path = arguments.next(); |
29 | | - let new_path = Path::new(new_path.unwrap()); |
30 | | - let _ = env::set_current_dir(&new_path).is_ok(); |
| 25 | + let command = full_cmd.next().unwrap_or(" "); |
| 26 | + let arguments: Vec<&str> = full_cmd.collect(); |
| 27 | + |
| 28 | + if let Some(action) = run_if_exists(command.to_string(), arguments.clone()) { |
| 29 | + match action { |
| 30 | + Action::Exit => { |
| 31 | + break; |
| 32 | + } |
| 33 | + |
| 34 | + Action::ChangeDirectory(directory) => { |
| 35 | + path = directory; |
| 36 | + env::set_current_dir(&path).unwrap(); |
| 37 | + } |
31 | 38 | } |
32 | | - command => { |
33 | | - let out = stdCommand::new(command).args(arguments).spawn(); |
| 39 | + } else { |
| 40 | + let out = process::Command::new(command) |
| 41 | + .args(arguments.clone()) |
| 42 | + .spawn(); |
34 | 43 |
|
35 | | - match out { |
36 | | - Ok(mut out) => { |
37 | | - let _ = out.wait(); |
38 | | - } |
39 | | - Err(err) => match err.kind() { |
40 | | - ErrorKind::NotFound => { |
41 | | - eprintln!("ash: {}: not found", command); |
42 | | - } |
43 | | - ErrorKind::PermissionDenied => { |
44 | | - eprintln!("ash: {}: permission denied", command); |
45 | | - } |
46 | | - _ => { |
47 | | - eprintln!("ash: uncaught error: {}", err); |
48 | | - } |
49 | | - }, |
| 44 | + match out { |
| 45 | + Ok(mut out) => { |
| 46 | + let _ = out.wait(); |
50 | 47 | } |
| 48 | + Err(err) => println!("{:?}", err), |
51 | 49 | } |
52 | 50 | } |
53 | 51 | } |
|
0 commit comments