nushell/crates/nu-command/src/filesystem/cd.rs

136 lines
4.6 KiB
Rust
Raw Normal View History

2022-01-05 07:36:42 +01:00
use nu_engine::{current_dir, CallExt};
2022-01-20 21:51:44 +01:00
use nu_protocol::ast::{Call, Expr, Expression};
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
2022-01-05 07:36:42 +01:00
use nu_protocol::{Category, PipelineData, ShellError, 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 {
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> {
2022-01-20 21:51:44 +01:00
let raw_path = call.nth(0);
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
2022-01-05 07:36:42 +01:00
let cwd = current_dir(engine_state, stack)?;
2021-10-02 22:16:37 +02:00
2022-01-20 21:51:44 +01:00
let (path, span) = match raw_path {
Some(v) => match &v {
Expression {
expr: Expr::Filepath(val),
span,
..
} if val == "-" => {
let oldpwd = stack.get_env_var(engine_state, "OLDPWD");
if let Some(oldpwd) = oldpwd {
let path = oldpwd.as_path()?;
let path = match nu_path::canonicalize_with(path, &cwd) {
Ok(p) => p,
Err(e) => {
return Err(ShellError::DirectoryNotFoundHelp(
*span,
format!("IO Error: {:?}", e),
))
}
};
(path.to_string_lossy().to_string(), *span)
} else {
(cwd.to_string_lossy().to_string(), *span)
}
2022-01-20 21:51:44 +01:00
}
_ => match path_val {
Some(v) => {
let path = v.as_path()?;
let path = match nu_path::canonicalize_with(path, &cwd) {
Ok(p) => p,
Err(e) => {
return Err(ShellError::DirectoryNotFoundHelp(
v.span()?,
format!("IO Error: {:?}", e),
))
}
};
(path.to_string_lossy().to_string(), v.span()?)
}
None => {
let path = nu_path::expand_tilde("~");
(path.to_string_lossy().to_string(), call.head)
}
},
},
2021-10-02 22:16:37 +02:00
None => {
let path = nu_path::expand_tilde("~");
(path.to_string_lossy().to_string(), call.head)
2021-10-02 22:16:37 +02:00
}
};
2022-01-05 07:36:42 +01:00
let path_value = Value::String { val: path, span };
let cwd = Value::String {
val: cwd.to_string_lossy().to_string(),
span: call.head,
};
let shells = stack.get_env_var(engine_state, "NUSHELL_SHELLS");
let mut shells = if let Some(v) = shells {
v.as_list()
.map(|x| x.to_vec())
.unwrap_or_else(|_| vec![cwd])
} else {
vec![cwd]
};
let current_shell = stack.get_env_var(engine_state, "NUSHELL_CURRENT_SHELL");
let current_shell = if let Some(v) = current_shell {
v.as_integer().unwrap_or_default() as usize
} else {
0
};
shells[current_shell] = path_value.clone();
stack.add_env_var(
"NUSHELL_SHELLS".into(),
Value::List {
vals: shells,
span: call.head,
},
);
stack.add_env_var(
"NUSHELL_CURRENT_SHELL".into(),
Value::Int {
val: current_shell as i64,
span: call.head,
},
);
2022-01-20 21:51:44 +01:00
if let Some(oldpwd) = stack.get_env_var(engine_state, "PWD") {
stack.add_env_var("OLDPWD".into(), oldpwd)
}
2021-10-02 22:16:37 +02:00
//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
2022-01-20 21:51:44 +01:00
2022-01-05 07:36:42 +01:00
stack.add_env_var("PWD".into(), path_value);
Ok(PipelineData::new(call.head))
2021-10-02 22:16:37 +02:00
}
}