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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::config::SharedConfig;
use crate::errors::*;
use crate::formatting::{Format, Fragment, Values};
use crate::protocol::i3bar_block::I3BarBlock;
use serde::Deserialize;
use smart_default::SmartDefault;
#[derive(Debug, Clone, Default)]
pub struct Widget {
pub state: State,
source: Source,
}
impl Widget {
pub fn new() -> Self {
Self::default()
}
pub fn with_text(mut self, text: String) -> Self {
self.set_text(text);
self
}
pub fn with_state(mut self, state: State) -> Self {
self.state = state;
self
}
pub fn with_format(mut self, format: Format) -> Self {
self.set_format(format);
self
}
pub fn set_text(&mut self, text: String) {
if text.is_empty() {
self.source = Source::None;
} else {
self.source = Source::Text(text);
}
}
pub fn set_format(&mut self, format: Format) {
match &mut self.source {
Source::Format(old, _) => *old = format,
_ => self.source = Source::Format(format, None),
}
}
pub fn set_values(&mut self, new_values: Values) {
if let Source::Format(_, values) = &mut self.source {
*values = Some(new_values);
}
}
pub fn intervals(&self) -> Vec<u64> {
match &self.source {
Source::Format(f, _) => f.intervals(),
_ => Vec::new(),
}
}
pub fn get_data(&self, shared_config: &SharedConfig, id: usize) -> Result<Vec<I3BarBlock>> {
let (key_bg, key_fg) = shared_config.theme.get_colors(self.state);
let (full, short) = self.source.render(shared_config)?;
let mut template = I3BarBlock {
instance: format!("{id}:"),
background: key_bg,
color: key_fg,
..I3BarBlock::default()
};
let mut parts = Vec::new();
if full.is_empty() {
return Ok(parts);
}
if !short.is_empty() {
template.short_text = "<span/>".into();
}
parts.extend(full.into_iter().map(|w| {
let mut data = template.clone();
data.full_text = w.formatted_text();
if let Some(i) = &w.metadata.instance {
data.instance.push_str(i);
}
data
}));
template.full_text = "<span/>".into();
parts.extend(short.into_iter().map(|w| {
let mut data = template.clone();
data.short_text = w.formatted_text();
if let Some(i) = &w.metadata.instance {
data.instance.push_str(i);
}
data
}));
Ok(parts)
}
}
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq, SmartDefault)]
pub enum State {
#[default]
#[serde(alias = "idle")]
Idle,
#[serde(alias = "info")]
Info,
#[serde(alias = "good")]
Good,
#[serde(alias = "warning")]
Warning,
#[serde(alias = "critical")]
Critical,
}
#[derive(Debug, Clone, SmartDefault)]
enum Source {
#[default]
None,
Text(String),
Format(Format, Option<Values>),
}
impl Source {
fn render(&self, config: &SharedConfig) -> Result<(Vec<Fragment>, Vec<Fragment>)> {
match self {
Self::Text(text) => Ok((vec![text.clone().into()], vec![])),
Self::Format(format, Some(values)) => format.render(values, config),
Self::None | Self::Format(_, None) => Ok((vec![], vec![])),
}
}
}