summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
authorTom Hotston <Hotston.tom@Gmail.com>2019-09-24 20:03:10 +0100
committerKevin Song <chipbuster@users.noreply.github.com>2019-09-24 14:03:10 -0500
commitc5e971aef8b03b58cc0746942e88cbd8660fb271 (patch)
tree42bbfeed1fb20ffe7afe8bb2373f28766ae79e1d /src/modules
parentc9878cab69cca791f92544ffdc66f1b57c0c679a (diff)
feat: Add ability for command duration in milliseconds (#380)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/cmd_duration.rs38
1 files changed, 27 insertions, 11 deletions
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<Module<'a>> {
.value_of("cmd_duration")
.unwrap_or("invalid_time")
.parse::<u64>()
- .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<Module<'a>> {
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<String> = components
.iter()
@@ -75,19 +87,23 @@ 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")
}
}