From 3e99dc01b026d0ea5592f701fec663ebfbbfe064 Mon Sep 17 00:00:00 2001 From: JT Date: Sat, 22 May 2021 17:02:06 +1200 Subject: [PATCH] let date commands pull default date (#3463) --- crates/nu-command/src/commands/date/format.rs | 9 +++++++-- crates/nu-command/src/commands/date/now.rs | 15 +++++++++------ crates/nu-command/src/commands/date/to_table.rs | 6 +++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/nu-command/src/commands/date/format.rs b/crates/nu-command/src/commands/date/format.rs index 827350b73..fccf4fb33 100644 --- a/crates/nu-command/src/commands/date/format.rs +++ b/crates/nu-command/src/commands/date/format.rs @@ -49,8 +49,13 @@ pub fn format(args: CommandArgs) -> Result { let format: Tagged = args.req(0)?; let table: Option = args.get_flag("table")?; - Ok(args - .input + let input = if args.input.is_empty() { + InputStream::one(crate::commands::date::now::date_now(&tag)) + } else { + args.input + }; + + Ok(input .map(move |value| match value { Value { value: UntaggedValue::Primitive(Primitive::Date(dt)), diff --git a/crates/nu-command/src/commands/date/now.rs b/crates/nu-command/src/commands/date/now.rs index 063fe3f53..e97f13966 100644 --- a/crates/nu-command/src/commands/date/now.rs +++ b/crates/nu-command/src/commands/date/now.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use chrono::{DateTime, Local}; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, UntaggedValue}; +use nu_protocol::{Signature, UntaggedValue, Value}; pub struct Date; @@ -24,13 +24,16 @@ impl WholeStreamCommand for Date { } } -pub fn now(args: CommandArgs) -> Result { - let args = args.evaluate_once()?; - let tag = args.call_info.name_tag.clone(); - +pub fn date_now(tag: &Tag) -> Value { let now: DateTime = Local::now(); - let value = UntaggedValue::date(now.with_timezone(now.offset())).into_value(&tag); + UntaggedValue::date(now.with_timezone(now.offset())).into_value(tag) +} + +pub fn now(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; + + let value = date_now(&args.call_info.name_tag); Ok(OutputStream::one(value)) } diff --git a/crates/nu-command/src/commands/date/to_table.rs b/crates/nu-command/src/commands/date/to_table.rs index a7c5553a7..2261229d4 100644 --- a/crates/nu-command/src/commands/date/to_table.rs +++ b/crates/nu-command/src/commands/date/to_table.rs @@ -36,7 +36,11 @@ impl WholeStreamCommand for Date { fn to_table(args: CommandArgs) -> Result { let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); - let input = args.input; + let input = if args.input.is_empty() { + InputStream::one(crate::commands::date::now::date_now(&tag)) + } else { + args.input + }; Ok(input .map(move |value| match value {