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
|
use std::{collections::BTreeMap, str::FromStr};
use chrono::Local;
use chrono_tz::Tz;
use crate::render::FormattedPart;
use crate::{config::ZellijState, widgets::widget::Widget};
pub struct DateTimeWidget {
format: String,
color_format: FormattedPart,
time_zone: Option<Tz>,
}
impl DateTimeWidget {
pub fn new(config: &BTreeMap<String, String>) -> Self {
let mut format = "%H:%M";
if let Some(form) = config.get("datetime_format") {
format = form;
}
let mut time_zone_string = "Etc/UTC";
if let Some(tz_string) = config.get("datetime_timezone") {
time_zone_string = tz_string;
}
let time_zone = match Tz::from_str(time_zone_string) {
Ok(tz) => Some(tz),
Err(_) => None,
};
let mut color_format = "";
if let Some(form) = config.get("datetime") {
color_format = form;
}
Self {
format: format.to_owned(),
color_format: FormattedPart::from_format_string(color_format),
time_zone,
}
}
}
impl Widget for DateTimeWidget {
fn process(&self, _name: &str ,_state: &ZellijState) -> String {
let mut output = self.color_format.content.clone();
if output.contains("{format}") {
let date = Local::now();
let mut tz = Tz::UTC;
if let Some(t) = self.time_zone {
tz = t;
}
output = output.replace(
"{format}",
format!("{}", date.with_timezone(&tz).format(self.format.as_str())).as_str(),
);
}
self.color_format.format_string(&output)
}
fn process_click(&self, _state: &ZellijState, _pos: usize) {}
}
|