2021-10-02 22:16:37 +02:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
2021-10-25 18:58:58 +02:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2021-12-17 02:04:54 +01:00
|
|
|
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape, Value};
|
2021-10-02 22:16:37 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-10-02 22:16:37 +02:00
|
|
|
pub struct Cd;
|
|
|
|
|
|
|
|
impl Command for Cd {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"cd"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Change directory."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("cd")
|
|
|
|
.optional("path", SyntaxShape::Filepath, "the path to change to")
|
|
|
|
.category(Category::FileSystem)
|
2021-10-02 22:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-02 22:16:37 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-12-17 02:04:54 +01:00
|
|
|
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
|
2021-10-02 22:16:37 +02:00
|
|
|
|
2021-12-17 02:04:54 +01:00
|
|
|
let (path, span) = match path_val {
|
|
|
|
Some(v) => {
|
|
|
|
let path = nu_path::expand_path(v.as_string()?);
|
|
|
|
(path.to_string_lossy().to_string(), v.span()?)
|
2021-10-02 22:16:37 +02:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let path = nu_path::expand_tilde("~");
|
2021-12-17 02:04:54 +01:00
|
|
|
(path.to_string_lossy().to_string(), call.head)
|
2021-10-02 22:16:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
2021-12-17 02:04:54 +01:00
|
|
|
stack.add_env_var("PWD".into(), Value::String { val: path, span });
|
2021-11-06 06:50:33 +01:00
|
|
|
Ok(PipelineData::new(call.head))
|
2021-10-02 22:16:37 +02:00
|
|
|
}
|
|
|
|
}
|