forked from extern/nushell
Fix cd - (#5301)
This commit is contained in:
parent
5ff2ae628b
commit
cc78446ffd
@ -1,7 +1,9 @@
|
|||||||
use nu_engine::{current_dir, CallExt};
|
use nu_engine::{current_dir, CallExt};
|
||||||
use nu_protocol::ast::{Call, Expr, Expression};
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
|
use nu_protocol::{
|
||||||
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Cd;
|
pub struct Cd;
|
||||||
@ -28,17 +30,12 @@ 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.positional_nth(0);
|
let path_val: Option<Spanned<String>> = 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 raw_path {
|
let (path, span) = match path_val {
|
||||||
Some(v) => match &v {
|
Some(v) => {
|
||||||
Expression {
|
if v.item == "-" {
|
||||||
expr: Expr::Filepath(val),
|
|
||||||
span,
|
|
||||||
..
|
|
||||||
} if val == "-" => {
|
|
||||||
let oldpwd = stack.get_env_var(engine_state, "OLDPWD");
|
let oldpwd = stack.get_env_var(engine_state, "OLDPWD");
|
||||||
|
|
||||||
if let Some(oldpwd) = oldpwd {
|
if let Some(oldpwd) = oldpwd {
|
||||||
@ -47,42 +44,34 @@ impl Command for Cd {
|
|||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(ShellError::DirectoryNotFound(
|
return Err(ShellError::DirectoryNotFound(
|
||||||
*span,
|
v.span,
|
||||||
Some(format!("IO Error: {:?}", e)),
|
Some(format!("IO Error: {:?}", e)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(path.to_string_lossy().to_string(), *span)
|
(path.to_string_lossy().to_string(), v.span)
|
||||||
} else {
|
} else {
|
||||||
(cwd.to_string_lossy().to_string(), *span)
|
(cwd.to_string_lossy().to_string(), v.span)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
_ => match path_val {
|
let path = match nu_path::canonicalize_with(&v.item, &cwd) {
|
||||||
Some(v) => {
|
Ok(p) => {
|
||||||
let path = v.as_path()?;
|
if !p.is_dir() {
|
||||||
let path = match nu_path::canonicalize_with(path, &cwd) {
|
return Err(ShellError::NotADirectory(v.span));
|
||||||
Ok(p) => {
|
|
||||||
if !p.is_dir() {
|
|
||||||
return Err(ShellError::NotADirectory(v.span()?));
|
|
||||||
}
|
|
||||||
p
|
|
||||||
}
|
}
|
||||||
|
p
|
||||||
|
}
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(ShellError::DirectoryNotFound(
|
return Err(ShellError::DirectoryNotFound(
|
||||||
v.span()?,
|
v.span,
|
||||||
Some(format!("IO Error: {:?}", e)),
|
Some(format!("IO Error: {:?}", e)),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(path.to_string_lossy().to_string(), v.span()?)
|
(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)
|
||||||
|
@ -531,13 +531,20 @@ pub fn eval_expression(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
Expr::Directory(s) => {
|
Expr::Directory(s) => {
|
||||||
let cwd = current_dir_str(engine_state, stack)?;
|
if s == "-" {
|
||||||
let path = expand_path_with(s, cwd);
|
Ok(Value::String {
|
||||||
|
val: "-".to_string(),
|
||||||
|
span: expr.span,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
let cwd = current_dir_str(engine_state, stack)?;
|
||||||
|
let path = expand_path_with(s, cwd);
|
||||||
|
|
||||||
Ok(Value::String {
|
Ok(Value::String {
|
||||||
val: path.to_string_lossy().to_string(),
|
val: path.to_string_lossy().to_string(),
|
||||||
span: expr.span,
|
span: expr.span,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Expr::GlobPattern(s) => {
|
Expr::GlobPattern(s) => {
|
||||||
let cwd = current_dir_str(engine_state, stack)?;
|
let cwd = current_dir_str(engine_state, stack)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user