diff options
author | Luca Rinaldi <lucarin@protonmail.com> | 2019-12-19 14:38:06 -0800 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-12-19 17:38:06 -0500 |
commit | 6a2b0a67b0ad8143f223f7abc6562ac839875b88 (patch) | |
tree | 4d11fe667909296448c95dcd82fe33d917d1abec /src/modules | |
parent | d6094d7b9d300462d0a9de21da5d1cdbb07794a4 (diff) |
feat: cmd_duration module optionally reports milliseconds (#696)
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/cmd_duration.rs | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 938549f64..6c13798dd 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -15,7 +15,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let elapsed = props .get("cmd_duration") .unwrap_or(&"invalid_time".into()) - .parse::<u64>() + .parse::<u128>() .ok()?; /* TODO: Once error handling is implemented, warn the user if their config @@ -28,7 +28,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { return None; } - let config_min = config.min_time as u64; + let config_min = config.min_time as u128; let module_color = match elapsed { time if time < config_min => return None, @@ -36,7 +36,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { }; module.set_style(module_color); - let cmd_duration_stacked = &format!("{}{}", config.prefix, render_time(elapsed)); + let cmd_duration_stacked = &format!( + "{}{}", + config.prefix, + render_time(elapsed, config.show_milliseconds) + ); module.create_segment("cmd_duration", &SegmentConfig::new(&cmd_duration_stacked)); module.get_prefix().set_value(""); @@ -44,8 +48,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { } // Render the time into a nice human-readable string -fn render_time(raw_seconds: u64) -> String { - // Calculate a simple breakdown into days/hours/minutes/seconds +fn render_time(raw_millis: u128, show_millis: bool) -> String { + // Calculate a simple breakdown into days/hours/minutes/seconds/milliseconds + let (millis, raw_seconds) = (raw_millis % 1000, raw_millis / 1000); let (seconds, raw_minutes) = (raw_seconds % 60, raw_seconds / 60); let (minutes, raw_hours) = (raw_minutes % 60, raw_minutes / 60); let (hours, days) = (raw_hours % 24, raw_hours / 24); @@ -53,16 +58,19 @@ fn render_time(raw_seconds: u64) -> String { let components = [days, hours, minutes, seconds]; let suffixes = ["d", "h", "m", "s"]; - let rendered_components: Vec<String> = components + let mut rendered_components: Vec<String> = components .iter() .zip(&suffixes) .map(render_time_component) .collect(); + if show_millis || raw_millis < 1000 { + rendered_components.push(render_time_component((&millis, &"ms"))); + } rendered_components.join("") } /// Render a single component of the time string, giving an empty string if component is zero -fn render_time_component((component, suffix): (&u64, &&str)) -> String { +fn render_time_component((component, suffix): (&u128, &&str)) -> String { match component { 0 => String::new(), n => format!("{}{}", n, suffix), @@ -74,19 +82,23 @@ mod tests { use super::*; #[test] + fn test_500ms() { + assert_eq!(render_time(500 as u128, true), "500ms") + } + #[test] fn test_10s() { - assert_eq!(render_time(10 as u64), "10s") + assert_eq!(render_time(10_000 as u128, true), "10s") } #[test] fn test_90s() { - assert_eq!(render_time(90 as u64), "1m30s") + assert_eq!(render_time(90_000 as u128, true), "1m30s") } #[test] fn test_10110s() { - assert_eq!(render_time(10110 as u64), "2h48m30s") + assert_eq!(render_time(10_110_000 as u128, true), "2h48m30s") } #[test] fn test_1d() { - assert_eq!(render_time(86400 as u64), "1d") + assert_eq!(render_time(86_400_000 as u128, true), "1d") } } |