diff options
| author | Jonas Maier <> | 2026-03-04 17:59:59 +0100 |
|---|---|---|
| committer | Jonas Maier <> | 2026-03-04 17:59:59 +0100 |
| commit | 9e8d6c77fe768423340beab8ed4ef4e4dd7187df (patch) | |
| tree | 32fb6e6f57bcf801762e70c7d397fc0803d03d05 | |
| parent | 7c46116456ebddba482d9f5c25d0e5d9c38e5c37 (diff) | |
| download | pish-9e8d6c77fe768423340beab8ed4ef4e4dd7187df.tar.gz | |
cd
| -rw-r--r-- | src/main.rs | 75 |
1 files changed, 51 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs index 8747d85..cd934d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::os::unix::io::AsRawFd; use std::process::Command; use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; +use std::path::Path; use termios::*; struct ScopedRawMode { @@ -126,6 +127,54 @@ pub struct CursorPos { pub col: usize, } +const PROMPT: &str = "> "; + +fn run_command(raw: &ScopedRawMode, line: Vec<u8>) { + let words: Vec<&[u8]> = line.split(|x| *x == b' ').collect(); + if words.is_empty() { + print!("{PROMPT}"); + return; + } + + let cmd = words[0]; + + match cmd { + b"cd" => { + let target: &Path = match words.get(1) { + Some(&b"-") => todo!("prev"), + Some(path) => OsStr::from_bytes(path).as_ref(), + None => todo!("homedir"), + }; + + if let Err(e) = std::env::set_current_dir(target) { + print!("ERR {PROMPT}"); + } else { + print!("{PROMPT}"); + } + + return; + }, + _ => (), + } + + let mut cmd = Command::new(OsStr::from_bytes(cmd)); + for arg in words[1..].iter() { + cmd.arg(OsStr::from_bytes(arg)); + } + + raw.disable(); + let status = cmd.status(); + raw.enable(); + + let status_string = match status { + Ok(ec) if ec.success() => String::new(), + Ok(ec) => format!("{ec} "), + Err(_) => String::from("IO ERROR "), + }; + + print!("{status_string}{PROMPT}"); +} + fn main() { let stdin = io::stdin(); let stdout = io::stdout(); @@ -144,9 +193,8 @@ fn main() { let mut buffer = [0u8; 1]; let mut line = LineBuffer::new(); - let prompt = "> "; - print!("{prompt}"); + print!("{PROMPT}"); loop { let Ok(_) = stdin.read_exact(&mut buffer) else { @@ -163,28 +211,7 @@ fn main() { b'\r' => { print!("\r\n"); let line = line.dump(); - let words: Vec<&[u8]> = line.split(|x| *x == b' ').collect(); - if words.is_empty() { - print!("{prompt}"); - continue; - } - - let mut cmd = Command::new(OsStr::from_bytes(words[0])); - for arg in words[1..].iter() { - cmd.arg(OsStr::from_bytes(arg)); - } - - raw.disable(); - let status = cmd.status(); - raw.enable(); - - let status_string = match status { - Ok(ec) if ec.success() => String::new(), - Ok(ec) => format!("{ec} "), - Err(_) => String::from("IO ERROR "), - }; - //ensure_newline(); - print!("{status_string}{prompt}"); + run_command(&raw, line); } // Backspace (127 on most systems) |
