From c5e971aef8b03b58cc0746942e88cbd8660fb271 Mon Sep 17 00:00:00 2001 From: Tom Hotston Date: Tue, 24 Sep 2019 20:03:10 +0100 Subject: feat: Add ability for command duration in milliseconds (#380) --- src/modules/cmd_duration.rs | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/modules') diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index 35a7e6858..fcbce5626 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -14,9 +14,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { .value_of("cmd_duration") .unwrap_or("invalid_time") .parse::() - .ok()?; + .ok()? + / 1_000_000; - let signed_config_min = module.config_value_i64("min_time").unwrap_or(2); + let signed_config_min = module.config_value_i64("min_time").unwrap_or(2000); /* TODO: Once error handling is implemented, warn the user if their config min time is nonsensical */ @@ -31,28 +32,39 @@ pub fn module<'a>(context: &'a Context) -> Option> { let config_min = signed_config_min as u64; let module_color = match elapsed { - time if time < config_min => return None, + time if time < config_min => module + .config_value_style("style") + .unwrap_or_else(|| Color::RGB(80, 80, 80).bold()), _ => module .config_value_style("style") .unwrap_or_else(|| Color::Yellow.bold()), }; module.set_style(module_color); - module.new_segment("cmd_duration", &format!("took {}", render_time(elapsed))); + module.new_segment( + "cmd_duration", + &format!("took {}", render_time(elapsed, config_min)), + ); module.get_prefix().set_value(""); Some(module) } // Render the time into a nice human-readable string -fn render_time(raw_seconds: u64) -> String { +fn render_time(raw_milliseconds: u64, config_min: u64) -> String { // Calculate a simple breakdown into days/hours/minutes/seconds + let (mut milliseconds, raw_seconds) = (raw_milliseconds % 1000, raw_milliseconds / 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); - let components = [days, hours, minutes, seconds]; - let suffixes = ["d", "h", "m", "s"]; + // Do not display milliseconds if command duration is less than config_min + if raw_milliseconds > config_min { + milliseconds = 0; + } + + let components = [days, hours, minutes, seconds, milliseconds]; + let suffixes = ["d", "h", "m", "s", "ms"]; let rendered_components: Vec = components .iter() @@ -74,20 +86,24 @@ fn render_time_component((component, suffix): (&u64, &&str)) -> String { mod tests { use super::*; + #[test] + fn test_100ms() { + assert_eq!(render_time(100 as u64, 2000), "100ms") + } #[test] fn test_10s() { - assert_eq!(render_time(10 as u64), "10s") + assert_eq!(render_time(10 * 1000 as u64, 2000), "10s") } #[test] fn test_90s() { - assert_eq!(render_time(90 as u64), "1m30s") + assert_eq!(render_time(90 * 1_000 as u64, 2000), "1m30s") } #[test] fn test_10110s() { - assert_eq!(render_time(10110 as u64), "2h48m30s") + assert_eq!(render_time(10110 * 1_000 as u64, 2000), "2h48m30s") } #[test] fn test_1d() { - assert_eq!(render_time(86400 as u64), "1d") + assert_eq!(render_time(86400 * 1_000 as u64, 2000), "1d") } } -- cgit v1.2.3