Add unit parsing and eval support

This commit is contained in:
JT
2021-10-05 15:27:39 +13:00
parent 75ec0d123a
commit 535ece4e76
15 changed files with 598 additions and 38 deletions

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};
@ -56,25 +57,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,
});