mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:55:40 +02:00
Add simple cd
This commit is contained in:
@ -16,6 +16,7 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
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));
|
||||
|
46
crates/nu-command/src/filesystem/cd.rs
Normal file
46
crates/nu-command/src/filesystem/cd.rs
Normal file
@ -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<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let path: Option<String> = 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 })
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
mod cd;
|
||||
mod ls;
|
||||
|
||||
pub use cd::Cd;
|
||||
pub use ls::Ls;
|
||||
|
Reference in New Issue
Block a user