diff options
| author | Daniel Jankowski <Daniel.Jankowski@rub.de> | 2024-04-16 10:59:15 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-16 10:59:15 +0200 |
| commit | 072392300409c11930cb07f56fab03eda2c24d4a (patch) | |
| tree | a009352ec85fa705d239166873d14ef469b3fbea /src/widgets | |
| parent | fix: toggle pane frames with floating panes (diff) | |
| download | sl0pstatus-072392300409c11930cb07f56fab03eda2c24d4a.tar.gz sl0pstatus-072392300409c11930cb07f56fab03eda2c24d4a.tar.xz sl0pstatus-072392300409c11930cb07f56fab03eda2c24d4a.zip | |
feat: remote control zjstatus with pipes (#48)
* chore: update to latest version of zellij
* fix: deprecated functions in chrono
* chore: update nix deps
* chore: remove deprecated chrono functions
* feat: add refresh via pipes with line protocol
* fix: handle multiple lines in pipe
* fix(clippy): char instead of str
* feat: add notification widget
* chore: update nix flake
* chore: update cargo deps
* chore: update cargo deps
* chore: update nix flake
* chore: clean up imports for tracing
* chore: update nix flake
* chore: update cargo deps
* chore: update nix flake
* chore: update cargo deps
* chore: remove pinning of chrono
Since chrono-tz now fixed the deprecations from chrono
we can finally remove the pinning.
* chore: update nix flake
* chore: update cargo deps
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/command.rs | 6 | ||||
| -rw-r--r-- | src/widgets/mod.rs | 1 | ||||
| -rw-r--r-- | src/widgets/notification.rs | 78 |
3 files changed, 82 insertions, 3 deletions
diff --git a/src/widgets/command.rs b/src/widgets/command.rs index 05bc94a..9e095c9 100644 --- a/src/widgets/command.rs +++ b/src/widgets/command.rs @@ -210,13 +210,13 @@ fn get_timestamp_from_event_or_default( return Local::now(); } - return Sub::<Duration>::sub(Local::now(), Duration::days(1)); + return Sub::<Duration>::sub(Local::now(), Duration::try_days(1).unwrap()); } let command_result = command_result.unwrap(); let ts_context = command_result.context.get("timestamp"); if ts_context.is_none() { - return Sub::<Duration>::sub(Local::now(), Duration::days(1)); + return Sub::<Duration>::sub(Local::now(), Duration::try_days(1).unwrap()); } let ts_context = ts_context.unwrap(); @@ -226,7 +226,7 @@ fn get_timestamp_from_event_or_default( match DateTime::parse_from_str(ts_context, TIMESTAMP_FORMAT) { Ok(ts) => ts.into(), - Err(_) => Sub::<Duration>::sub(Local::now(), Duration::days(1)), + Err(_) => Sub::<Duration>::sub(Local::now(), Duration::try_days(1).unwrap()), } } diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 9fcd130..1b20e48 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -1,6 +1,7 @@ pub mod command; pub mod datetime; pub mod mode; +pub mod notification; pub mod swap_layout; pub mod session; pub mod tabs; diff --git a/src/widgets/notification.rs b/src/widgets/notification.rs new file mode 100644 index 0000000..5023fa7 --- /dev/null +++ b/src/widgets/notification.rs @@ -0,0 +1,78 @@ +use std::collections::BTreeMap; + +use chrono::{DateTime, Local}; + +use crate::render::FormattedPart; +use crate::{config::ZellijState, widgets::widget::Widget}; + +#[derive(Clone, Debug, Default)] +pub struct Message { + pub body: String, + pub received_at: DateTime<Local>, +} + +pub struct NotificationWidget { + show_interval: i64, + format_unread: Vec<FormattedPart>, + format_no_notifications: Vec<FormattedPart>, +} + +impl NotificationWidget { + pub fn new(config: &BTreeMap<String, String>) -> Self { + let format_unread = match config.get("notification_format_unread") { + Some(f) => FormattedPart::multiple_from_format_string(f), + None => FormattedPart::multiple_from_format_string(""), + }; + + let format_no_notifications = match config.get("notification_format_no_notifications") { + Some(f) => FormattedPart::multiple_from_format_string(f), + None => FormattedPart::multiple_from_format_string(""), + }; + + let show_interval = match config.get("notification_show_interval") { + Some(i) => i.parse::<i64>().unwrap_or(5), + None => 5, + }; + + Self { + show_interval, + format_unread, + format_no_notifications, + } + } +} + +impl Widget for NotificationWidget { + fn process(&self, _name: &str, state: &ZellijState) -> String { + let message = match state.incoming_notification { + Some(ref message) => message.clone(), + None => Message::default(), + }; + + let no_new = + message.received_at.timestamp() + self.show_interval < Local::now().timestamp(); + + tracing::debug!("no_new: {}", no_new); + + let format = match no_new { + true => self.format_no_notifications.clone(), + false => self.format_unread.clone(), + }; + + let mut output = "".to_owned(); + + for f in format.iter() { + let mut content = f.content.clone(); + + if content.contains("{message}") { + content = content.replace("{message}", message.body.as_str()); + } + + output = format!("{}{}", output, f.format_string(&content)); + } + + output.to_owned() + } + + fn process_click(&self, _state: &ZellijState, _pos: usize) {} +} |
