mirror of
https://github.com/nushell/nushell.git
synced 2024-12-21 14:42:07 +01:00
add cd -
support (#787)
This commit is contained in:
parent
724cfaa890
commit
b4e61a056c
@ -1,5 +1,5 @@
|
|||||||
use nu_engine::{current_dir, CallExt};
|
use nu_engine::{current_dir, CallExt};
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::{Call, Expr, Expression};
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Value};
|
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Value};
|
||||||
|
|
||||||
@ -28,23 +28,55 @@ impl Command for Cd {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
_input: PipelineData,
|
_input: PipelineData,
|
||||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let raw_path = call.nth(0);
|
||||||
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
|
let path_val: Option<Value> = call.opt(engine_state, stack, 0)?;
|
||||||
let cwd = current_dir(engine_state, stack)?;
|
let cwd = current_dir(engine_state, stack)?;
|
||||||
|
|
||||||
let (path, span) = match path_val {
|
let (path, span) = match raw_path {
|
||||||
Some(v) => {
|
Some(v) => match &v {
|
||||||
let path = v.as_path()?;
|
Expression {
|
||||||
let path = match nu_path::canonicalize_with(path, &cwd) {
|
expr: Expr::Filepath(val),
|
||||||
Ok(p) => p,
|
span,
|
||||||
Err(e) => {
|
..
|
||||||
return Err(ShellError::DirectoryNotFoundHelp(
|
} if val == "-" => {
|
||||||
v.span()?,
|
let oldpwd = stack.get_env_var(engine_state, "OLDPWD");
|
||||||
format!("IO Error: {:?}", e),
|
|
||||||
))
|
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)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
(path.to_string_lossy().to_string(), v.span()?)
|
_ => 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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
None => {
|
None => {
|
||||||
let path = nu_path::expand_tilde("~");
|
let path = nu_path::expand_tilde("~");
|
||||||
(path.to_string_lossy().to_string(), call.head)
|
(path.to_string_lossy().to_string(), call.head)
|
||||||
@ -90,8 +122,13 @@ impl Command for Cd {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if let Some(oldpwd) = stack.get_env_var(engine_state, "PWD") {
|
||||||
|
stack.add_env_var("OLDPWD".into(), oldpwd)
|
||||||
|
}
|
||||||
|
|
||||||
//FIXME: this only changes the current scope, but instead this environment variable
|
//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
|
//should probably be a block that loads the information from the state in the overlay
|
||||||
|
|
||||||
stack.add_env_var("PWD".into(), path_value);
|
stack.add_env_var("PWD".into(), path_value);
|
||||||
Ok(PipelineData::new(call.head))
|
Ok(PipelineData::new(call.head))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user