aboutsummaryrefslogtreecommitdiffstats
path: root/src/widgets/notification.rs
blob: 1b8cfcd916a95f2b3b1fea8fa7bc30d003ae9a39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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, _name: &str, _state: &ZellijState, _pos: usize) {}
}