From eaea00366bbc6f7c7f37d7e7296a67fc33c9d960 Mon Sep 17 00:00:00 2001 From: Kelvin Samuel <44570273+kks110@users.noreply.github.com> Date: Fri, 31 Mar 2023 22:01:48 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20a=20bug=20with=20`us`=20not=20outputting?= =?UTF-8?q?=20as=20`=C2=B5s`=20with=20the=20`into=20duration`=20command=20?= =?UTF-8?q?(#8691)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Whilst working on [Allow parsing of mu (µ) character for durations](https://github.com/nushell/nushell/pull/8647), I found a bug where, if you use `into duration --convert us`, it outputs with the unit as `us` rather than `µs` ![image](https://user-images.githubusercontent.com/44570273/229141818-37f97071-7f8e-451c-9baa-3c292290e6e7.png) After this change, it now outputs the correct symbol: ![image](https://user-images.githubusercontent.com/44570273/229142720-6e67d49a-e88f-44a8-a742-92fa5220e54b.png) # User-Facing Changes User will now see correct unit when converting into microseconds. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-utils/standard_library/tests.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --- .../src/conversions/into/duration.rs | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/conversions/into/duration.rs b/crates/nu-command/src/conversions/into/duration.rs index 6a3a6b64bb..f7279dc1a6 100644 --- a/crates/nu-command/src/conversions/into/duration.rs +++ b/crates/nu-command/src/conversions/into/duration.rs @@ -161,6 +161,14 @@ impl Command for SubCommand { span, }), }, + Example { + description: "Convert duration to µs as a string if unit asked for was us", + example: "1sec | into duration --convert us", + result: Some(Value::String { + val: "1000000 µs".to_string(), + span, + }), + }, ] } } @@ -439,7 +447,7 @@ fn string_to_unit_duration( if let Expr::Int(x) = value.expr { match unit.item { Unit::Nanosecond => return Ok(("ns", x)), - Unit::Microsecond => return Ok(("us", x)), + Unit::Microsecond => return Ok(("µs", x)), Unit::Millisecond => return Ok(("ms", x)), Unit::Second => return Ok(("sec", x)), Unit::Minute => return Ok(("min", x)), @@ -488,14 +496,19 @@ fn action( *value_span, ) { Ok(d) => { + let unit = if &to_unit.item == "us" { + "µs" + } else { + &to_unit.item + }; if d.fract() == 0.0 { Value::String { - val: format!("{} {}", d, &to_unit.item), + val: format!("{} {}", d, unit), span: *value_span, } } else { Value::String { - val: format!("{:.float_precision$} {}", d, &to_unit.item), + val: format!("{:.float_precision$} {}", d, unit), span: *value_span, } } @@ -522,14 +535,19 @@ fn action( *value_span, ) { Ok(d) => { + let unit = if &to_unit.item == "us" { + "µs" + } else { + &to_unit.item + }; if d.fract() == 0.0 { Value::String { - val: format!("{} {}", d, &to_unit.item), + val: format!("{} {}", d, unit), span: *value_span, } } else { Value::String { - val: format!("{:.float_precision$} {}", d, &to_unit.item), + val: format!("{:.float_precision$} {}", d, unit), span: *value_span, } }