From e7fb15be5992633da42bedd50e856ebb63ff0693 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 24 Dec 2019 14:26:47 +1300 Subject: [PATCH] Fix an assortment of issues --- Cargo.lock | 3 +-- crates/nu-protocol/src/value/primitive.rs | 2 +- crates/nu_plugin_ps/src/main.rs | 18 ++++++++++++++---- src/commands/command.rs | 11 +++++++---- src/data/base/shape.rs | 4 +++- src/evaluate/evaluator.rs | 20 +++++++++++--------- src/plugins/nu_plugin_core_ps.rs | 18 ++++++++++++++---- 7 files changed, 51 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3dddd3ed3c..d830210c5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2973,8 +2973,7 @@ dependencies = [ [[package]] name = "rustyline" version = "5.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae156e2a68be20a3ae95574089b65c188d86291cd5421cc647222b8d1864ffb" +source = "git+https://github.com/kkawakam/rustyline.git#55dcab54fbab0ba0610799291276785e5b31199c" dependencies = [ "cfg-if", "dirs 2.0.2", diff --git a/crates/nu-protocol/src/value/primitive.rs b/crates/nu-protocol/src/value/primitive.rs index 30bf7c5222..6176be9974 100644 --- a/crates/nu-protocol/src/value/primitive.rs +++ b/crates/nu-protocol/src/value/primitive.rs @@ -113,7 +113,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S } Primitive::Duration(sec) => format_duration(*sec), Primitive::Int(i) => i.to_string(), - Primitive::Decimal(decimal) => decimal.to_string(), + Primitive::Decimal(decimal) => format!("{:.4}", decimal), Primitive::Range(range) => format!( "{}..{}", format_primitive(&range.from.0.item, None), diff --git a/crates/nu_plugin_ps/src/main.rs b/crates/nu_plugin_ps/src/main.rs index 9cde0460c5..517d4e2b99 100644 --- a/crates/nu_plugin_ps/src/main.rs +++ b/crates/nu_plugin_ps/src/main.rs @@ -3,7 +3,7 @@ use futures::executor::block_on; use futures_util::{StreamExt, TryStreamExt}; use heim::process::{self as process, Process, ProcessResult}; -use heim::units::{ratio, Ratio}; +use heim::units::{information, ratio, Ratio}; use std::usize; use nu_errors::ShellError; @@ -22,12 +22,14 @@ impl Ps { } } -async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio)> { +async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio, process::Memory)> { let usage_1 = process.cpu_usage().await?; futures_timer::Delay::new(Duration::from_millis(100)).await; let usage_2 = process.cpu_usage().await?; - Ok((process, usage_2 - usage_1)) + let memory = process.memory().await?; + + Ok((process, usage_2 - usage_1, memory)) } async fn ps(tag: Tag) -> Vec { @@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec { let mut output = vec![]; while let Some(res) = processes.next().await { - if let Ok((process, usage)) = res { + if let Ok((process, usage, memory)) = res { let mut dict = TaggedDictBuilder::new(&tag); dict.insert_untagged("pid", UntaggedValue::int(process.pid())); if let Ok(name) = process.name().await { @@ -53,6 +55,14 @@ async fn ps(tag: Tag) -> Vec { dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status))); } dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::())); + dict.insert_untagged( + "mem", + UntaggedValue::bytes(memory.rss().get::()), + ); + dict.insert_untagged( + "virtual", + UntaggedValue::bytes(memory.vms().get::()), + ); output.push(dict.into_value()); } } diff --git a/src/commands/command.rs b/src/commands/command.rs index 5873d5ea20..4d4474444c 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -505,10 +505,13 @@ impl Command { let call_info = raw_args .clone() .call_info - .evaluate(®istry, &Scope::it_value(x.clone())) - .unwrap(); - match command.run(&call_info, ®istry, &raw_args, x) { - Ok(o) => o, + .evaluate(®istry, &Scope::it_value(x.clone())); + + match call_info { + Ok(call_info) => match command.run(&call_info, ®istry, &raw_args, x) { + Ok(o) => o, + Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(), + }, Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(), } }) diff --git a/src/data/base/shape.rs b/src/data/base/shape.rs index f6ffac0e7d..a6d1299570 100644 --- a/src/data/base/shape.rs +++ b/src/data/base/shape.rs @@ -140,7 +140,9 @@ impl PrettyDebug for FormatInlineShape { match &self.shape { InlineShape::Nothing => b::blank(), InlineShape::Int(int) => b::primitive(format!("{}", int)), - InlineShape::Decimal(decimal) => b::primitive(format!("{}", decimal)), + InlineShape::Decimal(decimal) => { + b::description(format_primitive(&Primitive::Decimal(decimal.clone()), None)) + } InlineShape::Range(range) => { let (left, left_inclusion) = &range.from; let (right, right_inclusion) = &range.to; diff --git a/src/evaluate/evaluator.rs b/src/evaluate/evaluator.rs index 03808f2a84..6b3f84bc2c 100644 --- a/src/evaluate/evaluator.rs +++ b/src/evaluate/evaluator.rs @@ -166,20 +166,20 @@ fn evaluate_reference( match name { hir::Variable::It(_) => Ok(scope.it.value.clone().into_value(tag)), hir::Variable::Other(inner) => match inner.slice(source) { - x if x == "nu:env" => { + x if x == "nu" => { + let mut nu_dict = TaggedDictBuilder::new(&tag); + let mut dict = TaggedDictBuilder::new(&tag); for v in std::env::vars() { if v.0 != "PATH" && v.0 != "Path" { dict.insert_untagged(v.0, UntaggedValue::string(v.1)); } } - Ok(dict.into_value()) - } - x if x == "nu:config" => { - let config = crate::data::config::read(tag.clone(), &None)?; - Ok(UntaggedValue::row(config).into_value(tag)) - } - x if x == "nu:path" => { + nu_dict.insert_value("env", dict.into_value()); + + let config = crate::data::config::read(&tag, &None)?; + nu_dict.insert_value("config", UntaggedValue::row(config).into_value(&tag)); + let mut table = vec![]; let path = std::env::var_os("PATH"); if let Some(paths) = path { @@ -187,7 +187,9 @@ fn evaluate_reference( table.push(UntaggedValue::path(path).into_value(&tag)); } } - Ok(UntaggedValue::table(&table).into_value(tag)) + nu_dict.insert_value("path", UntaggedValue::table(&table).into_value(&tag)); + + Ok(nu_dict.into_value()) } x => Ok(scope .vars diff --git a/src/plugins/nu_plugin_core_ps.rs b/src/plugins/nu_plugin_core_ps.rs index 9cde0460c5..517d4e2b99 100644 --- a/src/plugins/nu_plugin_core_ps.rs +++ b/src/plugins/nu_plugin_core_ps.rs @@ -3,7 +3,7 @@ use futures::executor::block_on; use futures_util::{StreamExt, TryStreamExt}; use heim::process::{self as process, Process, ProcessResult}; -use heim::units::{ratio, Ratio}; +use heim::units::{information, ratio, Ratio}; use std::usize; use nu_errors::ShellError; @@ -22,12 +22,14 @@ impl Ps { } } -async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio)> { +async fn usage(process: Process) -> ProcessResult<(process::Process, Ratio, process::Memory)> { let usage_1 = process.cpu_usage().await?; futures_timer::Delay::new(Duration::from_millis(100)).await; let usage_2 = process.cpu_usage().await?; - Ok((process, usage_2 - usage_1)) + let memory = process.memory().await?; + + Ok((process, usage_2 - usage_1, memory)) } async fn ps(tag: Tag) -> Vec { @@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec { let mut output = vec![]; while let Some(res) = processes.next().await { - if let Ok((process, usage)) = res { + if let Ok((process, usage, memory)) = res { let mut dict = TaggedDictBuilder::new(&tag); dict.insert_untagged("pid", UntaggedValue::int(process.pid())); if let Ok(name) = process.name().await { @@ -53,6 +55,14 @@ async fn ps(tag: Tag) -> Vec { dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status))); } dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::())); + dict.insert_untagged( + "mem", + UntaggedValue::bytes(memory.rss().get::()), + ); + dict.insert_untagged( + "virtual", + UntaggedValue::bytes(memory.vms().get::()), + ); output.push(dict.into_value()); } }