This commit is contained in:
Kevin 2019-12-26 12:32:28 -05:00
commit c69bf9f46f
No known key found for this signature in database
GPG Key ID: 73A2AF10B0FD9D7D
9 changed files with 56 additions and 42 deletions

5
Cargo.lock generated
View File

@ -1911,7 +1911,7 @@ dependencies = [
[[package]]
name = "nu"
version = "0.7.1"
version = "0.7.2"
dependencies = [
"ansi_term 0.12.1",
"app_dirs",
@ -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",

View File

@ -1,6 +1,6 @@
[package]
name = "nu"
version = "0.7.1"
version = "0.7.2"
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
description = "A shell for the GitHub era"
license = "MIT"
@ -60,7 +60,7 @@ nu-macros = { version = "0.7.0", path = "./crates/nu-macros" }
query_interface = "0.3.5"
typetag = "0.1.4"
rustyline = "5.0.4"
rustyline = { git = "https://github.com/kkawakam/rustyline.git" }
chrono = { version = "0.4.10", features = ["serde"] }
derive-new = "0.5.8"
prettytable-rs = "0.8.0"

View File

@ -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),

View File

@ -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<Value> {
@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec<Value> {
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<Value> {
dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status)));
}
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());
}
}

View File

@ -170,29 +170,17 @@ pub(crate) async fn run_external_command(
let name_tag = command.name_tag.clone();
if let Ok(mut popen) = popen {
popen.detach();
match stream_next {
StreamNext::Last => {
popen.detach();
loop {
match popen.poll() {
None => {
std::thread::sleep(std::time::Duration::new(0, 100_000_000));
}
_ => {
let _ = popen.terminate();
break;
}
}
}
let _ = popen.wait();
Ok(ClassifiedInputStream::new())
}
StreamNext::External => {
popen.detach();
let stdout = popen.stdout.take().unwrap();
Ok(ClassifiedInputStream::from_stdout(stdout))
}
StreamNext::Internal => {
popen.detach();
let stdout = popen.stdout.take().unwrap();
let file = futures::io::AllowStdIo::new(stdout);
let stream = Framed::new(file, LinesCodec {});

View File

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

View File

@ -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;

View File

@ -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

View File

@ -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<Value> {
@ -43,7 +45,7 @@ async fn ps(tag: Tag) -> Vec<Value> {
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<Value> {
dict.insert_untagged("status", UntaggedValue::string(format!("{:?}", status)));
}
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());
}
}