From cd53c35ff8133166119d98e727ffbccc59f6dee6 Mon Sep 17 00:00:00 2001 From: Lucas Garron Date: Fri, 6 Oct 2023 14:32:56 -0700 Subject: [PATCH 1/2] Display milliseconds for low `elapsed` times. Addresses https://github.com/console-rs/indicatif/issues/597 --- src/format.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/format.rs b/src/format.rs index 7475a251..c02cdb11 100644 --- a/src/format.rs +++ b/src/format.rs @@ -3,6 +3,7 @@ use std::time::Duration; use number_prefix::NumberPrefix; +const MILLISECOND: Duration = Duration::from_millis(1); const SECOND: Duration = Duration::from_secs(1); const MINUTE: Duration = Duration::from_secs(60); const HOUR: Duration = Duration::from_secs(60 * 60); @@ -104,6 +105,7 @@ const UNITS: &[(Duration, &str, &str)] = &[ (HOUR, "hour", "h"), (MINUTE, "minute", "m"), (SECOND, "second", "s"), + (MILLISECOND, "millisecond", "ms"), ]; impl fmt::Display for HumanBytes { From d79d649f8972942262ab93d53457489164a1103b Mon Sep 17 00:00:00 2001 From: Lucas Garron Date: Sat, 7 Oct 2023 14:45:44 -0700 Subject: [PATCH 2/2] Update tests. --- src/format.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/format.rs b/src/format.rs index c02cdb11..a26a90ae 100644 --- a/src/format.rs +++ b/src/format.rs @@ -194,25 +194,39 @@ mod tests { #[test] fn human_duration_less_than_one_second() { assert_eq!( - "0 seconds", + "0 milliseconds", format!("{}", HumanDuration(Duration::from_secs(0))) ); - assert_eq!("0 seconds", format!("{}", HumanDuration(MILLI))); - assert_eq!("0 seconds", format!("{}", HumanDuration(499 * MILLI))); - assert_eq!("1 second", format!("{}", HumanDuration(500 * MILLI))); - assert_eq!("1 second", format!("{}", HumanDuration(999 * MILLI))); + assert_eq!("1 millisecond", format!("{}", HumanDuration(MILLI))); + assert_eq!("2 milliseconds", format!("{}", HumanDuration(2 * MILLI))); + assert_eq!( + "499 milliseconds", + format!("{}", HumanDuration(499 * MILLI)) + ); + assert_eq!( + "500 milliseconds", + format!("{}", HumanDuration(500 * MILLI)) + ); + assert_eq!( + "999 milliseconds", + format!("{}", HumanDuration(999 * MILLI)) + ); } #[test] fn human_duration_less_than_two_seconds() { - assert_eq!("1 second", format!("{}", HumanDuration(1499 * MILLI))); + assert_eq!( + "1499 milliseconds", + format!("{}", HumanDuration(1499 * MILLI)) + ); assert_eq!("2 seconds", format!("{}", HumanDuration(1500 * MILLI))); assert_eq!("2 seconds", format!("{}", HumanDuration(1999 * MILLI))); } #[test] fn human_duration_one_unit() { - assert_eq!("1 second", format!("{}", HumanDuration(SECOND))); + assert_eq!("1 millisecond", format!("{}", HumanDuration(MILLI))); + assert_eq!("1000 milliseconds", format!("{}", HumanDuration(SECOND))); assert_eq!("60 seconds", format!("{}", HumanDuration(MINUTE))); assert_eq!("60 minutes", format!("{}", HumanDuration(HOUR))); assert_eq!("24 hours", format!("{}", HumanDuration(DAY)));