Merge branch 'nushell:main' into main

This commit is contained in:
Justin
2021-10-04 22:02:43 -07:00
committed by GitHub
24 changed files with 967 additions and 74 deletions

View File

@ -15,7 +15,7 @@ impl Command for Cd {
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("cd").optional("path", SyntaxShape::FilePath, "the path to change to")
Signature::build("cd").optional("path", SyntaxShape::Filepath, "the path to change to")
}
fn run(
@ -28,7 +28,7 @@ impl Command for Cd {
let path = match path {
Some(path) => {
let path = nu_path::expand_tilde(path);
let path = nu_path::expand_path(path);
path.to_string_lossy().to_string()
}
None => {

View File

@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use nu_engine::eval_expression;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
@ -31,7 +32,17 @@ impl Command for Ls {
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let pattern = if let Some(expr) = call.positional.get(0) {
let result = eval_expression(context, expr)?;
result.as_string()?
let mut result = result.as_string()?;
let path = std::path::Path::new(&result);
if path.is_dir() {
if !result.ends_with(std::path::MAIN_SEPARATOR) {
result.push(std::path::MAIN_SEPARATOR);
}
result.push('*');
}
result
} else {
"*".into()
};
@ -49,25 +60,39 @@ impl Command for Ls {
let is_dir = metadata.is_dir();
let filesize = metadata.len();
let mut cols = vec!["name".into(), "type".into(), "size".into()];
let mut vals = vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
},
if is_file {
Value::string("File", call_span)
} else if is_dir {
Value::string("Dir", call_span)
} else {
Value::Nothing { span: call_span }
},
Value::Filesize {
val: filesize as i64,
span: call_span,
},
];
if let Ok(date) = metadata.modified() {
let utc: DateTime<Utc> = date.into();
cols.push("modified".into());
vals.push(Value::Date {
val: utc.into(),
span: call_span,
});
}
Value::Record {
cols: vec!["name".into(), "type".into(), "size".into()],
vals: vec![
Value::String {
val: path.to_string_lossy().to_string(),
span: call_span,
},
if is_file {
Value::string("file", call_span)
} else if is_dir {
Value::string("dir", call_span)
} else {
Value::Nothing { span: call_span }
},
Value::Filesize {
val: filesize,
span: call_span,
},
],
cols,
vals,
span: call_span,
}
}