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,
}
}

View File

@ -86,13 +86,13 @@ fn run_ps(call: &Call) -> Result<Value, ShellError> {
cols.push("mem".into());
vals.push(Value::Filesize {
val: result.memory() * 1000,
val: result.memory() as i64 * 1000,
span,
});
cols.push("virtual".into());
vals.push(Value::Filesize {
val: result.virtual_memory() * 1000,
val: result.virtual_memory() as i64 * 1000,
span,
});

View File

@ -112,13 +112,13 @@ pub fn disks(sys: &mut System, span: Span) -> Option<Value> {
cols.push("total".into());
vals.push(Value::Filesize {
val: disk.total_space(),
val: disk.total_space() as i64,
span,
});
cols.push("free".into());
vals.push(Value::Filesize {
val: disk.available_space(),
val: disk.available_space() as i64,
span,
});
@ -148,13 +148,13 @@ pub fn net(sys: &mut System, span: Span) -> Option<Value> {
cols.push("sent".into());
vals.push(Value::Filesize {
val: data.total_transmitted(),
val: data.total_transmitted() as i64,
span,
});
cols.push("recv".into());
vals.push(Value::Filesize {
val: data.total_received(),
val: data.total_received() as i64,
span,
});
@ -215,25 +215,25 @@ pub fn mem(sys: &mut System, span: Span) -> Option<Value> {
cols.push("total".into());
vals.push(Value::Filesize {
val: total_mem * 1000,
val: total_mem as i64 * 1000,
span,
});
cols.push("free".into());
vals.push(Value::Filesize {
val: free_mem * 1000,
val: free_mem as i64 * 1000,
span,
});
cols.push("swap total".into());
vals.push(Value::Filesize {
val: total_swap * 1000,
val: total_swap as i64 * 1000,
span,
});
cols.push("swap free".into());
vals.push(Value::Filesize {
val: free_swap * 1000,
val: free_swap as i64 * 1000,
span,
});
@ -276,7 +276,7 @@ pub fn host(sys: &mut System, span: Span) -> Option<Value> {
}
cols.push("uptime".into());
vals.push(Value::Duration {
val: 1000000000 * sys.uptime() as u64,
val: 1000000000 * sys.uptime() as i64,
span,
});