This repository was archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidget.rs
214 lines (183 loc) · 5.19 KB
/
widget.rs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::config::SharedConfig;
use crate::errors::*;
use crate::formatting::{RunningFormat, Values};
use crate::protocol::i3bar_block::I3BarBlock;
use serde_derive::Deserialize;
use smartstring::alias::String;
/// Spacing around the widget
#[derive(Debug, Clone, Copy)]
pub enum Spacing {
/// Add a leading and trailing space around the widget contents
Normal,
/// Hide both leading and trailing spaces when widget is hidden
Hidden,
}
/// State of the widget. Affects the theming.
#[derive(Debug, Clone, Copy, Deserialize)]
pub enum State {
Idle,
Info,
Good,
Warning,
Critical,
}
impl Default for State {
fn default() -> Self {
Self::Idle
}
}
/// The source of text for widget
#[derive(Debug)]
enum Source {
/// Simple text
Text(String),
/// Full and short texts
TextWithShort(String, String),
/// A format template
Format(RunningFormat, Option<Values>),
}
impl Source {
fn render(&self) -> Result<(String, Option<String>)> {
match self {
Source::Text(text) => Ok((text.clone(), None)),
Source::TextWithShort(full, short) => Ok((full.clone(), Some(short.clone()))),
Source::Format(format, Some(values)) => format.render(values),
Source::Format(_, None) => Ok((String::new(), None)),
}
}
}
#[derive(Debug)]
pub struct Widget {
instance: Option<usize>,
pub icon: String,
pub shared_config: SharedConfig,
pub state: State,
inner: I3BarBlock,
source: Source,
backup: Option<(Source, State)>,
}
impl Widget {
pub fn new(id: usize, shared_config: SharedConfig) -> Self {
let inner = I3BarBlock {
name: Some(id.to_string()),
..I3BarBlock::default()
};
Widget {
instance: None,
icon: String::new(),
shared_config,
state: State::Idle,
inner,
source: Source::Text(String::new()),
backup: None,
}
}
/*
* Builders
*/
pub fn with_instance(mut self, instance: usize) -> Self {
self.instance = Some(instance);
self.inner.instance = Some(instance.to_string());
self
}
pub fn with_icon_str(mut self, icon: String) -> Self {
self.icon = icon;
self
}
pub fn with_text(mut self, text: String) -> Self {
self.source = Source::Text(text);
self
}
pub fn with_state(mut self, state: State) -> Self {
self.state = state;
self
}
/*
* Setters
*/
pub fn set_text(&mut self, text: String) {
self.source = Source::Text(text);
}
pub fn set_texts(&mut self, short: String, full: String) {
self.source = Source::TextWithShort(short, full);
}
pub fn set_format(&mut self, format: RunningFormat) {
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);
}
}
/*
* Getters
*/
pub fn get_instance(&self) -> Option<usize> {
self.instance
}
/*
* Preserve / Restore
*/
pub fn preserve(&mut self) {
self.backup = Some((
std::mem::replace(&mut self.source, Source::Text(String::new())),
self.state,
));
}
pub fn restore(&mut self) {
if let Some(backup) = self.backup.take() {
self.source = backup.0;
self.state = backup.1;
}
}
/// Constuct `I3BarBlock` from this widget
pub fn get_data(&self) -> Result<I3BarBlock> {
let mut data = self.inner.clone();
let (key_bg, key_fg) = self.shared_config.theme.get_colors(self.state);
data.background = key_bg;
data.color = key_fg;
let (full, short) = self.source.render()?;
let full_spacing = if full.is_empty() {
Spacing::Hidden
} else {
Spacing::Normal
};
let short_spacing = if short.as_ref().map(String::is_empty).unwrap_or(true) {
Spacing::Hidden
} else {
Spacing::Normal
};
data.full_text = format!(
"{}{}{}",
match (self.icon.as_str(), full_spacing) {
("", Spacing::Normal) => " ",
("", Spacing::Hidden) => "",
(icon, _) => icon,
},
full,
match full_spacing {
Spacing::Normal => " ",
Spacing::Hidden => "",
}
);
data.short_text = short.as_ref().map(|short_text| {
format!(
"{}{}{}",
match (self.icon.as_str(), short_spacing) {
("", Spacing::Normal) => " ",
("", Spacing::Hidden) => "",
(icon, _) => icon,
},
short_text,
match short_spacing {
Spacing::Normal => " ",
Spacing::Hidden => "",
}
)
});
Ok(data)
}
}