Skip to content

Commit

Permalink
feat(cmd_duration): Make notification timeout configurable (starship#…
Browse files Browse the repository at this point in the history
…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 52109ab.

* 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
  • Loading branch information
Lyndeno authored and Perniciosius committed Feb 21, 2022
1 parent 2ffa137 commit b5db076
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
19 changes: 10 additions & 9 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,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

Expand Down
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/configs/cmd_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -24,6 +27,7 @@ impl<'a> Default for CmdDurationConfig<'a> {
disabled: false,
show_notifications: false,
min_time_to_notify: 45_000,
notification_timeout: None,
}
}
}
7 changes: 6 additions & 1 deletion src/modules/cmd_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b5db076

Please sign in to comment.