mirror of
https://github.com/nushell/nushell
synced 2024-12-26 13:03:07 +00:00
Improve duration math and printing (#1748)
* Improve duration math and printing * Fix test
This commit is contained in:
parent
8951a01e58
commit
a2e9bbd358
3 changed files with 35 additions and 0 deletions
|
@ -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 {
|
||||
|
|
|
@ -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())),
|
||||
|
|
|
@ -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!(
|
||||
|
|
Loading…
Reference in a new issue