From 8ff15c46c141f278d3c288c148e8979f5048534b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= <andres@androbtech.com> Date: Wed, 12 Aug 2020 04:21:28 -0500 Subject: [PATCH] histogram gives back percentage column. (#2340) --- crates/nu-cli/src/commands/histogram.rs | 9 +++++++++ crates/nu-cli/src/commands/str_/from.rs | 2 +- crates/nu-cli/src/commands/str_/mod.rs | 2 +- crates/nu-cli/tests/commands/histogram.rs | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/commands/histogram.rs b/crates/nu-cli/src/commands/histogram.rs index abbe26df33..a263e8a7a3 100644 --- a/crates/nu-cli/src/commands/histogram.rs +++ b/crates/nu-cli/src/commands/histogram.rs @@ -130,6 +130,15 @@ pub async fn histogram( fact.insert_value(&column.item, column_value); fact.insert_untagged("count", UntaggedValue::int(count)); + let percentage = format!( + "{}%", + // Some(2) < the number of digits + // true < group the digits + crate::commands::str_::from::action(&value, &name, Some(2), true)? + .as_string()? + ); + fact.insert_untagged("percentage", UntaggedValue::string(percentage)); + let string = std::iter::repeat("*") .take(value.as_u64().map_err(|_| { ShellError::labeled_error("expected a number", "expected a number", &name) diff --git a/crates/nu-cli/src/commands/str_/from.rs b/crates/nu-cli/src/commands/str_/from.rs index 12e4e73a40..fc2c5783d6 100644 --- a/crates/nu-cli/src/commands/str_/from.rs +++ b/crates/nu-cli/src/commands/str_/from.rs @@ -112,7 +112,7 @@ async fn operate( } // TODO If you're using the with-system-locale feature and you're on Windows, Clang 3.9 or higher is also required. -fn action( +pub fn action( input: &Value, tag: impl Into<Tag>, digits: Option<u64>, diff --git a/crates/nu-cli/src/commands/str_/mod.rs b/crates/nu-cli/src/commands/str_/mod.rs index 38b7abaa60..6926bd2e60 100644 --- a/crates/nu-cli/src/commands/str_/mod.rs +++ b/crates/nu-cli/src/commands/str_/mod.rs @@ -5,7 +5,7 @@ mod contains; mod downcase; mod ends_with; mod find_replace; -mod from; +pub mod from; mod index_of; mod length; mod reverse; diff --git a/crates/nu-cli/tests/commands/histogram.rs b/crates/nu-cli/tests/commands/histogram.rs index c0a23dcec4..34c41d3693 100644 --- a/crates/nu-cli/tests/commands/histogram.rs +++ b/crates/nu-cli/tests/commands/histogram.rs @@ -106,7 +106,7 @@ fn count() { "# )); - let bit_json = r#"[{"bit":"1","count":2},{"bit":"0","count":6}]"#; + let bit_json = r#"[{"bit":"1","count":2,"percentage":"33.33%"},{"bit":"0","count":6,"percentage":"100.00%"}]"#; assert_eq!(actual.out, bit_json); }