From b19d0ea31817928655f84addb933cf4ba3187cb8 Mon Sep 17 00:00:00 2001 From: Jonas Maier Date: Fri, 6 Mar 2026 13:03:26 +0100 Subject: support `cd -` --- src/main.rs | 2 ++ src/run/builtin.rs | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 19e7743..c4113f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,7 @@ pub struct Session { line: LineBuf, history: Vec, dispatch: CommandDispatch, + prev_path: BString, } /// relative path -- in case it is a proper subpath the result starts with a slash `/` @@ -138,6 +139,7 @@ fn event_loop() { line: LineBuf::new(), history: Vec::new(), dispatch: CommandDispatch::new(), + prev_path: vec![b'.'], }; print!("{}", se.prompt()); diff --git a/src/run/builtin.rs b/src/run/builtin.rs index b342dd0..e715bd7 100644 --- a/src/run/builtin.rs +++ b/src/run/builtin.rs @@ -1,6 +1,6 @@ #![allow(non_camel_case_types)] -use std::{fs::OpenOptions, path::PathBuf}; +use std::{env::*, fs::OpenOptions, path::PathBuf}; use super::Builtin; use crate::*; @@ -10,15 +10,29 @@ impl Builtin for cd { fn name(&self) -> &str { "cd" } - fn mod_session(&self, _: &mut Session, args: &[BString]) { - let target: &Path = match args.get(0).map(|v| &v[..]) { - Some(b"-") => todo!("prev"), - Some(path) => OsStr::from_bytes(path).as_ref(), - None => todo!("homedir"), + fn mod_session(&self, se: &mut Session, args: &[BString]) { + let mut dir = match current_dir() { + Ok(path) => path.as_os_str().as_bytes().to_vec(), + Err(_) => vec![b'.'], }; + std::mem::swap(&mut dir, &mut se.prev_path); + + match args.get(0).map(|v| &v[..]) { + Some(b"-") => { + let _ = set_current_dir(OsStr::from_bytes(&dir)); + } + Some(path) => { + let _ = set_current_dir(OsStr::from_bytes(path)); + } + None => { + if let Some(home) = std::env::var_os("HOME") { + let _ = set_current_dir(home); + } + } + } + // TODO: let mod_session builtins return nonzero exit code - let _ = std::env::set_current_dir(target); } } -- cgit v1.2.3