aboutsummaryrefslogtreecommitdiffstats
path: root/src/run/builtin.rs
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-03-06 13:03:26 +0100
committerJonas Maier <jonas@x77.dev>2026-03-06 13:03:26 +0100
commitb19d0ea31817928655f84addb933cf4ba3187cb8 (patch)
tree80d6f09186e99804830146e7c95dd41af54348e4 /src/run/builtin.rs
parente568ccb94011146288b4bd63952be741b7500df5 (diff)
downloadpish-b19d0ea31817928655f84addb933cf4ba3187cb8.tar.gz
support `cd -`
Diffstat (limited to 'src/run/builtin.rs')
-rw-r--r--src/run/builtin.rs28
1 files changed, 21 insertions, 7 deletions
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);
}
}