From e680540cfc4c783266183a589a26a33605bead43 Mon Sep 17 00:00:00 2001 From: Lyndon Sanche Date: Sat, 29 Jan 2022 14:56:55 -0700 Subject: [PATCH] feat(cmd_duration): Make notification timeout configurable (#3515) * Allow customization of notification timeout * Document new notification duration option * Check for out-of-bounds timeout and correct it * Implement ModuleConfig for u32 * Revert "Check for out-of-bounds timeout and correct it" This reverts commit 52109ab5f7c336b55c81bccafb3adbfc81514553. * Switch notification_timeout to u32 * Note notification_daemons might not honor timout * Notification timeout defaults to daemon timeout * Leave default value of notification_timeout blank in docs --- docs/config/README.md | 19 ++++++++++--------- src/config.rs | 17 +++++++++++++++++ src/configs/cmd_duration.rs | 4 ++++ src/modules/cmd_duration.rs | 7 ++++++- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 8dc1f6cb..3bf0ec9c 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -597,15 +597,16 @@ running `eval $(starship init $0)`, and then proceed as normal. ### Options -| Option | Default | Description | -| -------------------- | ----------------------------- | ---------------------------------------------------------- | -| `min_time` | `2_000` | Shortest duration to show time for (in milliseconds). | -| `show_milliseconds` | `false` | Show milliseconds in addition to seconds for the duration. | -| `format` | `"took [$duration]($style) "` | The format for the module. | -| `style` | `"bold yellow"` | The style for the module. | -| `disabled` | `false` | Disables the `cmd_duration` module. | -| `show_notifications` | `false` | Show desktop notifications when command completes. | -| `min_time_to_notify` | `45_000` | Shortest duration for notification (in milliseconds). | +| Option | Default | Description | +| ---------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `min_time` | `2_000` | Shortest duration to show time for (in milliseconds). | +| `show_milliseconds` | `false` | Show milliseconds in addition to seconds for the duration. | +| `format` | `"took [$duration]($style) "` | The format for the module. | +| `style` | `"bold yellow"` | The style for the module. | +| `disabled` | `false` | Disables the `cmd_duration` module. | +| `show_notifications` | `false` | Show desktop notifications when command completes. | +| `min_time_to_notify` | `45_000` | Shortest duration for notification (in milliseconds). | +| `notification_timeout` | | Duration to show notification for (in milliseconds). If unset, notification timeout will be determined by daemon. Not all notification daemons honor this option. | ::: tip diff --git a/src/config.rs b/src/config.rs index aa86e5ba..83bccb7e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -109,6 +109,23 @@ impl<'a> ModuleConfig<'a> for f64 { } } +impl<'a> ModuleConfig<'a> for u32 { + fn from_config(config: &Value) -> Option { + match config { + Value::Integer(value) => { + // Converting i64 to u32 + if *value > 0 && *value <= u32::MAX.into() { + Some(*value as Self) + } else { + None + } + } + Value::String(value) => value.parse::().ok(), + _ => None, + } + } +} + impl<'a> ModuleConfig<'a> for usize { fn from_config(config: &Value) -> Option { match config { diff --git a/src/configs/cmd_duration.rs b/src/configs/cmd_duration.rs index be5d2f73..44c0c699 100644 --- a/src/configs/cmd_duration.rs +++ b/src/configs/cmd_duration.rs @@ -12,6 +12,9 @@ pub struct CmdDurationConfig<'a> { pub disabled: bool, pub show_notifications: bool, pub min_time_to_notify: i64, + + #[serde(skip_serializing_if = "Option::is_none")] + pub notification_timeout: Option, } impl<'a> Default for CmdDurationConfig<'a> { @@ -24,6 +27,7 @@ impl<'a> Default for CmdDurationConfig<'a> { disabled: false, show_notifications: false, min_time_to_notify: 45_000, + notification_timeout: None, } } } diff --git a/src/modules/cmd_duration.rs b/src/modules/cmd_duration.rs index f15d349d..133db31e 100644 --- a/src/modules/cmd_duration.rs +++ b/src/modules/cmd_duration.rs @@ -79,12 +79,17 @@ fn undistract_me<'a, 'b>( unstyle(&ANSIStrings(&module.ansi_strings())) ); + let timeout = match config.notification_timeout { + Some(v) => Timeout::Milliseconds(v), + None => Timeout::Default, + }; + let mut notification = Notification::new(); notification .summary("Command finished") .body(&body) .icon("utilities-terminal") - .timeout(Timeout::Milliseconds(750)); + .timeout(timeout); if let Err(err) = notification.show() { log::trace!("Cannot show notification: {}", err);