Browse Source

feat: change directory to home if no argument is given to cd

main
fdai7451 2 years ago
parent
commit
f9206e16de
  1. 21
      Cargo.lock
  2. 1
      Cargo.toml
  3. 13
      src/builtins/cd.rs

21
Cargo.lock

@ -60,6 +60,15 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "dirs"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059"
dependencies = [
"dirs-sys",
]
[[package]] [[package]]
name = "dirs-next" name = "dirs-next"
version = "2.0.0" version = "2.0.0"
@ -70,6 +79,17 @@ dependencies = [
"dirs-sys-next", "dirs-sys-next",
] ]
[[package]]
name = "dirs-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "dirs-sys-next" name = "dirs-sys-next"
version = "0.1.2" version = "0.1.2"
@ -210,6 +230,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"ctrlc", "ctrlc",
"dirs",
"once_cell", "once_cell",
"rustyline", "rustyline",
"shlex", "shlex",

1
Cargo.toml

@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
ctrlc = "3.2" ctrlc = "3.2"
dirs = "4.0"
once_cell = "1.17" once_cell = "1.17"
rustyline = "10.1.0" rustyline = "10.1.0"
shlex = "1.1" shlex = "1.1"

13
src/builtins/cd.rs

@ -1,5 +1,6 @@
use crate::builtins::{Builtin, BuiltinConfig}; use crate::builtins::{Builtin, BuiltinConfig};
use std::env::set_current_dir; use std::env::set_current_dir;
use std::path::Path;
use crate::error::ShellError; use crate::error::ShellError;
@ -7,11 +8,17 @@ pub struct Cd;
impl Builtin for Cd { impl Builtin for Cd {
fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> { fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
let Some(first) = args.get(0) else {
return Err(ShellError::ExecuteFailure("no argument given".to_string()))
let dir = match args.get(0) {
Some(first) => Path::new(first).to_path_buf(),
None => {
let Some(home) = dirs::home_dir() else {
return Ok(())
};
home
}
}; };
set_current_dir(first).map_err(|e| ShellError::ExecuteFailure(e.to_string()))
set_current_dir(dir).map_err(|e| ShellError::ExecuteFailure(e.to_string()))
} }
} }

Loading…
Cancel
Save