From 91090e1db1cad7bc74d07e42e15a820c60d13368 Mon Sep 17 00:00:00 2001 From: JT Date: Sun, 3 Oct 2021 09:16:37 +1300 Subject: [PATCH] Add simple cd --- Cargo.lock | 1 + crates/nu-command/Cargo.toml | 1 + crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/filesystem/cd.rs | 46 ++++++++++++++++++++++++ crates/nu-command/src/filesystem/mod.rs | 2 ++ crates/nu-protocol/src/syntax_shape.rs | 2 +- 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 crates/nu-command/src/filesystem/cd.rs diff --git a/Cargo.lock b/Cargo.lock index c66b470877..cf1052648a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -499,6 +499,7 @@ dependencies = [ "glob", "nu-engine", "nu-json", + "nu-path", "nu-protocol", "nu-table", "sysinfo", diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 2c8ddfee89..c6bcd32f9b 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] nu-engine = { path = "../nu-engine" } nu-json = { path = "../nu-json" } +nu-path = { path = "../nu-path" } nu-protocol = { path = "../nu-protocol" } nu-table = { path = "../nu-table" } diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index dc119edc5d..34763d4f98 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -16,6 +16,7 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Alias)); working_set.add_decl(Box::new(Benchmark)); working_set.add_decl(Box::new(BuildString)); + working_set.add_decl(Box::new(Cd)); working_set.add_decl(Box::new(Def)); working_set.add_decl(Box::new(Do)); working_set.add_decl(Box::new(Each)); diff --git a/crates/nu-command/src/filesystem/cd.rs b/crates/nu-command/src/filesystem/cd.rs new file mode 100644 index 0000000000..94282c75e2 --- /dev/null +++ b/crates/nu-command/src/filesystem/cd.rs @@ -0,0 +1,46 @@ +use nu_engine::CallExt; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{Signature, SyntaxShape, Value}; + +pub struct Cd; + +impl Command for Cd { + fn name(&self) -> &str { + "cd" + } + + fn usage(&self) -> &str { + "Change directory." + } + + fn signature(&self) -> nu_protocol::Signature { + Signature::build("cd").optional("path", SyntaxShape::FilePath, "the path to change to") + } + + fn run( + &self, + context: &EvaluationContext, + call: &Call, + _input: Value, + ) -> Result { + let path: Option = call.opt(context, 0)?; + + let path = match path { + Some(path) => { + let path = nu_path::expand_tilde(path); + path.to_string_lossy().to_string() + } + None => { + let path = nu_path::expand_tilde("~"); + path.to_string_lossy().to_string() + } + }; + let _ = std::env::set_current_dir(&path); + + //FIXME: this only changes the current scope, but instead this environment variable + //should probably be a block that loads the information from the state in the overlay + context.add_env_var("PWD".into(), path); + Ok(Value::Nothing { span: call.head }) + } +} diff --git a/crates/nu-command/src/filesystem/mod.rs b/crates/nu-command/src/filesystem/mod.rs index d7d2f30464..13148b5358 100644 --- a/crates/nu-command/src/filesystem/mod.rs +++ b/crates/nu-command/src/filesystem/mod.rs @@ -1,3 +1,5 @@ +mod cd; mod ls; +pub use cd::Cd; pub use ls::Ls; diff --git a/crates/nu-protocol/src/syntax_shape.rs b/crates/nu-protocol/src/syntax_shape.rs index daed48c0c3..8b752461e7 100644 --- a/crates/nu-protocol/src/syntax_shape.rs +++ b/crates/nu-protocol/src/syntax_shape.rs @@ -86,7 +86,7 @@ impl SyntaxShape { SyntaxShape::Custom(custom, _) => custom.to_type(), SyntaxShape::Duration => Type::Duration, SyntaxShape::Expression => Type::Unknown, - SyntaxShape::FilePath => Type::FilePath, + SyntaxShape::FilePath => Type::String, SyntaxShape::Filesize => Type::Filesize, SyntaxShape::FullCellPath => Type::Unknown, SyntaxShape::GlobPattern => Type::String,