Fix an assortment of issues

This commit is contained in:
Jonathan Turner 2019-12-24 14:26:47 +13:00
parent be7550822c
commit e7fb15be59
7 changed files with 51 additions and 25 deletions

3
Cargo.lock generated
View file

@ -2973,8 +2973,7 @@ dependencies = [
[[package]] [[package]]
name = "rustyline" name = "rustyline"
version = "5.0.5" version = "5.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/kkawakam/rustyline.git#55dcab54fbab0ba0610799291276785e5b31199c"
checksum = "3ae156e2a68be20a3ae95574089b65c188d86291cd5421cc647222b8d1864ffb"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"dirs 2.0.2", "dirs 2.0.2",

View file

@ -113,7 +113,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
} }
Primitive::Duration(sec) => format_duration(*sec), Primitive::Duration(sec) => format_duration(*sec),
Primitive::Int(i) => i.to_string(), Primitive::Int(i) => i.to_string(),
Primitive::Decimal(decimal) => decimal.to_string(), Primitive::Decimal(decimal) => format!("{:.4}", decimal),
Primitive::Range(range) => format!( Primitive::Range(range) => format!(
"{}..{}", "{}..{}",
format_primitive(&range.from.0.item, None), format_primitive(&range.from.0.item, None),

View file

@ -3,7 +3,7 @@ use futures::executor::block_on;
use futures_util::{StreamExt, TryStreamExt}; use futures_util::{StreamExt, TryStreamExt};
use heim::process::{self as process, Process, ProcessResult}; use heim::process::{self as process, Process, ProcessResult};
use heim::units::{ratio, Ratio}; use heim::units::{information, ratio, Ratio};
use std::usize; use std::usize;
use nu_errors::ShellError; 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?; let usage_1 = process.cpu_usage().await?;
futures_timer::Delay::new(Duration::from_millis(100)).await; futures_timer::Delay::new(Duration::from_millis(100)).await;
let usage_2 = process.cpu_usage().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<Value> { async fn ps(tag: Tag) -> Vec<Value> {
@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec<Value> {
let mut output = vec![]; let mut output = vec![];
while let Some(res) = processes.next().await { 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); let mut dict = TaggedDictBuilder::new(&tag);
dict.insert_untagged("pid", UntaggedValue::int(process.pid())); dict.insert_untagged("pid", UntaggedValue::int(process.pid()));
if let Ok(name) = process.name().await { if let Ok(name) = process.name().await {
@ -53,6 +55,14 @@ async fn ps(tag: Tag) -> Vec<Value> {
dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status))); dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status)));
} }
dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::<ratio::percent>())); dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::<ratio::percent>()));
dict.insert_untagged(
"mem",
UntaggedValue::bytes(memory.rss().get::<information::byte>()),
);
dict.insert_untagged(
"virtual",
UntaggedValue::bytes(memory.vms().get::<information::byte>()),
);
output.push(dict.into_value()); output.push(dict.into_value());
} }
} }

View file

@ -505,10 +505,13 @@ impl Command {
let call_info = raw_args let call_info = raw_args
.clone() .clone()
.call_info .call_info
.evaluate(&registry, &Scope::it_value(x.clone())) .evaluate(&registry, &Scope::it_value(x.clone()));
.unwrap();
match command.run(&call_info, &registry, &raw_args, x) { match call_info {
Ok(o) => o, Ok(call_info) => match command.run(&call_info, &registry, &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(), Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(),
} }
}) })

View file

@ -140,7 +140,9 @@ impl PrettyDebug for FormatInlineShape {
match &self.shape { match &self.shape {
InlineShape::Nothing => b::blank(), InlineShape::Nothing => b::blank(),
InlineShape::Int(int) => b::primitive(format!("{}", int)), 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) => { InlineShape::Range(range) => {
let (left, left_inclusion) = &range.from; let (left, left_inclusion) = &range.from;
let (right, right_inclusion) = &range.to; let (right, right_inclusion) = &range.to;

View file

@ -166,20 +166,20 @@ fn evaluate_reference(
match name { match name {
hir::Variable::It(_) => Ok(scope.it.value.clone().into_value(tag)), hir::Variable::It(_) => Ok(scope.it.value.clone().into_value(tag)),
hir::Variable::Other(inner) => match inner.slice(source) { 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); let mut dict = TaggedDictBuilder::new(&tag);
for v in std::env::vars() { for v in std::env::vars() {
if v.0 != "PATH" && v.0 != "Path" { if v.0 != "PATH" && v.0 != "Path" {
dict.insert_untagged(v.0, UntaggedValue::string(v.1)); dict.insert_untagged(v.0, UntaggedValue::string(v.1));
} }
} }
Ok(dict.into_value()) nu_dict.insert_value("env", dict.into_value());
}
x if x == "nu:config" => { let config = crate::data::config::read(&tag, &None)?;
let config = crate::data::config::read(tag.clone(), &None)?; nu_dict.insert_value("config", UntaggedValue::row(config).into_value(&tag));
Ok(UntaggedValue::row(config).into_value(tag))
}
x if x == "nu:path" => {
let mut table = vec![]; let mut table = vec![];
let path = std::env::var_os("PATH"); let path = std::env::var_os("PATH");
if let Some(paths) = path { if let Some(paths) = path {
@ -187,7 +187,9 @@ fn evaluate_reference(
table.push(UntaggedValue::path(path).into_value(&tag)); 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 x => Ok(scope
.vars .vars

View file

@ -3,7 +3,7 @@ use futures::executor::block_on;
use futures_util::{StreamExt, TryStreamExt}; use futures_util::{StreamExt, TryStreamExt};
use heim::process::{self as process, Process, ProcessResult}; use heim::process::{self as process, Process, ProcessResult};
use heim::units::{ratio, Ratio}; use heim::units::{information, ratio, Ratio};
use std::usize; use std::usize;
use nu_errors::ShellError; 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?; let usage_1 = process.cpu_usage().await?;
futures_timer::Delay::new(Duration::from_millis(100)).await; futures_timer::Delay::new(Duration::from_millis(100)).await;
let usage_2 = process.cpu_usage().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<Value> { async fn ps(tag: Tag) -> Vec<Value> {
@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec<Value> {
let mut output = vec![]; let mut output = vec![];
while let Some(res) = processes.next().await { 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); let mut dict = TaggedDictBuilder::new(&tag);
dict.insert_untagged("pid", UntaggedValue::int(process.pid())); dict.insert_untagged("pid", UntaggedValue::int(process.pid()));
if let Ok(name) = process.name().await { if let Ok(name) = process.name().await {
@ -53,6 +55,14 @@ async fn ps(tag: Tag) -> Vec<Value> {
dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status))); dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status)));
} }
dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::<ratio::percent>())); dict.insert_untagged("cpu", UntaggedValue::decimal(usage.get::<ratio::percent>()));
dict.insert_untagged(
"mem",
UntaggedValue::bytes(memory.rss().get::<information::byte>()),
);
dict.insert_untagged(
"virtual",
UntaggedValue::bytes(memory.vms().get::<information::byte>()),
);
output.push(dict.into_value()); output.push(dict.into_value());
} }
} }