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 52109ab5f7.

* 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
This commit is contained in:
Lyndon Sanche 2022-01-29 14:56:55 -07:00 committed by GitHub
parent 6ca911b9fe
commit e680540cfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 10 deletions

View File

@ -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

View File

@ -109,6 +109,23 @@ impl<'a> ModuleConfig<'a> for f64 {
}
}
impl<'a> ModuleConfig<'a> for u32 {
fn from_config(config: &Value) -> Option<Self> {
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::<Self>().ok(),
_ => None,
}
}
}
impl<'a> ModuleConfig<'a> for usize {
fn from_config(config: &Value) -> Option<Self> {
match config {

View File

@ -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<u32>,
}
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,
}
}
}

View File

@ -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);