From a2e9bbd3587508e8c8e6ae0f2ac24a5f2dcb295d Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 11 May 2020 13:44:49 +1200 Subject: [PATCH] Improve duration math and printing (#1748) * Improve duration math and printing * Fix test --- crates/nu-cli/src/commands/autoview.rs | 14 ++++++++++++++ crates/nu-cli/src/data/value.rs | 9 +++++++++ crates/nu-cli/tests/commands/math.rs | 12 ++++++++++++ 3 files changed, 35 insertions(+) diff --git a/crates/nu-cli/src/commands/autoview.rs b/crates/nu-cli/src/commands/autoview.rs index 1c49c90779..cf1653d011 100644 --- a/crates/nu-cli/src/commands/autoview.rs +++ b/crates/nu-cli/src/commands/autoview.rs @@ -1,5 +1,6 @@ use crate::commands::UnevaluatedCallInfo; use crate::commands::WholeStreamCommand; +use crate::data::value::format_leaf; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{hir, hir::Expression, hir::Literal, hir::SpannedExpression}; @@ -162,6 +163,19 @@ pub fn autoview(context: RunnableContext) -> Result { } => { out!("{}", b); } + Value { + value: UntaggedValue::Primitive(Primitive::Duration(d)), + .. + } => { + let output = format_leaf(&x).plain_string(100_000); + out!("{}", output); + } + Value { + value: UntaggedValue::Primitive(Primitive::Date(d)), + .. + } => { + out!("{}", d); + } Value { value: UntaggedValue::Primitive(Primitive::Binary(ref b)), .. } => { if let Some(binary) = binary { diff --git a/crates/nu-cli/src/data/value.rs b/crates/nu-cli/src/data/value.rs index 6b35108c6d..18e666862a 100644 --- a/crates/nu-cli/src/data/value.rs +++ b/crates/nu-cli/src/data/value.rs @@ -111,6 +111,15 @@ pub fn compute_values( }?; Ok(UntaggedValue::Primitive(Primitive::Date(result))) } + (Primitive::Duration(x), Primitive::Duration(y)) => { + let result = match operator { + Operator::Plus => Ok(x + y), + Operator::Minus => Ok(x - y), + _ => Err((left.type_name(), right.type_name())), + }?; + + Ok(UntaggedValue::Primitive(Primitive::Duration(result))) + } _ => Err((left.type_name(), right.type_name())), }, _ => Err((left.type_name(), right.type_name())), diff --git a/crates/nu-cli/tests/commands/math.rs b/crates/nu-cli/tests/commands/math.rs index 23ed9b69d2..ee513fd312 100644 --- a/crates/nu-cli/tests/commands/math.rs +++ b/crates/nu-cli/tests/commands/math.rs @@ -96,6 +96,18 @@ fn parens_precedence() { assert_eq!(actual.out, "12"); } +#[test] +fn duration_math() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + = 1w + 1d + "# + )); + + assert_eq!(actual.out, "8:00:00:00"); +} + #[test] fn compound_comparison() { let actual = nu!(