Improve duration math and printing (#1748)

* Improve duration math and printing

* Fix test
This commit is contained in:
Jonathan Turner 2020-05-11 13:44:49 +12:00 committed by GitHub
parent 8951a01e58
commit a2e9bbd358
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View file

@ -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<OutputStream, ShellError> {
} => {
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 {

View file

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

View file

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