From fcd624a722224c4a2070dc3a33351835d395a804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 24 Jul 2021 22:33:31 -0700 Subject: [PATCH] add `date humanize` command (#3833) * add `date humanize` command * add docs --- .../src/commands/generators/date/humanize.rs | 81 +++++++++++++++++++ .../src/commands/generators/date/mod.rs | 2 + crates/nu-command/src/default_context.rs | 1 + docs/commands/date-humanize.md | 24 ++++++ 4 files changed, 108 insertions(+) create mode 100644 crates/nu-command/src/commands/generators/date/humanize.rs create mode 100644 docs/commands/date-humanize.md diff --git a/crates/nu-command/src/commands/generators/date/humanize.rs b/crates/nu-command/src/commands/generators/date/humanize.rs new file mode 100644 index 0000000000..af440f9819 --- /dev/null +++ b/crates/nu-command/src/commands/generators/date/humanize.rs @@ -0,0 +1,81 @@ +use crate::prelude::*; +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{Dictionary, Primitive, Signature, UntaggedValue, Value}; + +pub struct Date; + +impl WholeStreamCommand for Date { + fn name(&self) -> &str { + "date humanize" + } + + fn signature(&self) -> Signature { + Signature::build("date humanize").switch("table", "print date in a table", Some('t')) + } + + fn usage(&self) -> &str { + "Print a 'humanized' format for the date, relative to now." + } + + fn run(&self, args: CommandArgs) -> Result { + humanize(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Humanize the current date", + example: "date now | date humanize", + result: None, + }] + } +} + +pub fn humanize(args: CommandArgs) -> Result { + let tag = args.call_info.name_tag.clone(); + + let table: Option = args.get_flag("table")?; + let input = args.input; + + Ok(input + .map(move |value| match value { + Value { + value: UntaggedValue::Primitive(dt @ Primitive::Date(_)), + .. + } => { + let output = nu_protocol::format_primitive(&dt, None); + let value = if table.is_some() { + let mut indexmap = IndexMap::new(); + indexmap.insert( + "formatted".to_string(), + UntaggedValue::string(&output).into_value(&tag), + ); + + UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag) + } else { + UntaggedValue::string(&output).into_value(&tag) + }; + + Ok(value) + } + _ => Err(ShellError::labeled_error( + "Expected a date from pipeline", + "requires date input", + &tag, + )), + }) + .into_input_stream()) +} + +#[cfg(test)] +mod tests { + use super::Date; + use super::ShellError; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + test_examples(Date {}) + } +} diff --git a/crates/nu-command/src/commands/generators/date/mod.rs b/crates/nu-command/src/commands/generators/date/mod.rs index fb245128c1..c98e62ef29 100644 --- a/crates/nu-command/src/commands/generators/date/mod.rs +++ b/crates/nu-command/src/commands/generators/date/mod.rs @@ -1,5 +1,6 @@ pub mod command; pub mod format; +pub mod humanize; pub mod list_timezone; pub mod now; pub mod to_table; @@ -9,6 +10,7 @@ mod parser; pub use command::Command as Date; pub use format::Date as DateFormat; +pub use humanize::Date as DateHumanize; pub use list_timezone::Date as DateListTimeZone; pub use now::Date as DateNow; pub use to_table::Date as DateToTable; diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 00d91c0dab..19a921cc70 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -51,6 +51,7 @@ pub fn create_default_context(interactive: bool) -> Result date humanize + ``` + +## Flags +* -h, --help: Display this help message +* -t, --table: print date in a table + +## Examples + Format the current date +```shell +> date now | date humanize + ``` + + Format the current date and print in a table +```shell +> date now | date humanize -t + ``` + +