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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::{
collections::HashMap,
rc::Rc,
sync::{Arc, Mutex},
time::Duration,
};
use aho_corasick::AhoCorasick;
use anyhow::Result;
use async_trait::async_trait;
use derive_builder::Builder;
use futures::task::AtomicWaker;
use rustix::fs::statvfs;
use tokio_stream::StreamExt;
use crate::{
attrs::Attrs,
bar::PanelDrawInfo,
common::{PanelCommon, ShowHide},
remove_string_from_config, remove_uint_from_config, Highlight,
ManagedIntervalStream, PanelConfig, PanelRunResult,
};
/// Displays information about storage for a given mountpoint.
#[derive(Builder, Debug)]
#[builder_struct_attr(allow(missing_docs))]
#[builder_impl_attr(allow(missing_docs))]
pub struct Storage {
name: &'static str,
#[builder(default = "Duration::from_secs(10)")]
interval: Duration,
#[builder(default)]
waker: Arc<AtomicWaker>,
path: String,
formatter: AhoCorasick,
format: &'static str,
attrs: Attrs,
#[builder(default, setter(strip_option))]
highlight: Option<Highlight>,
common: PanelCommon,
}
impl Storage {
fn draw(
&self,
cr: &Rc<cairo::Context>,
height: i32,
paused: Arc<Mutex<bool>>,
) -> Result<PanelDrawInfo> {
let fs_info = statvfs(self.path.as_str())?;
let used = fs_info.f_blocks - fs_info.f_bfree;
let avail = fs_info.f_bavail;
let percentage_used =
(used as f64 / (used + avail) as f64 * 100.0) as u64;
let used_bytes = used * fs_info.f_frsize;
let avail_bytes = avail * fs_info.f_frsize;
let mut text = String::new();
self.formatter.replace_all_with(
self.format,
&mut text,
|_, content, dst| match content {
"%path%" => {
dst.push_str(self.path.as_str());
true
}
"%gb_used%" => {
dst.push_str(
format!(
"{:.2}",
(used_bytes as f64 / 1024.0 / 1024.0 / 1024.0)
)
.as_str(),
);
true
}
"%gb_free%" => {
dst.push_str(
format!(
"{:.2}",
(avail_bytes as f64 / 1024.0 / 1024.0 / 1024.0)
)
.as_str(),
);
true
}
"%gb_total%" => {
dst.push_str(
format!(
"{:.2}",
((used_bytes + avail_bytes) as f64
/ 1024.0
/ 1024.0
/ 1024.0)
)
.as_str(),
);
true
}
"%mb_used%" => {
dst.push_str(
((used_bytes as f64 / 1024.0 / 1024.0) as u64)
.to_string()
.as_str(),
);
true
}
"%mb_free%" => {
dst.push_str(
((avail_bytes as f64 / 1024.0 / 1024.0) as u64)
.to_string()
.as_str(),
);
true
}
"%mb_total%" => {
dst.push_str(
(((used_bytes + avail_bytes) as f64 / 1024.0 / 1024.0)
as u64)
.to_string()
.as_str(),
);
true
}
"%percentage_used%" => {
dst.push_str(percentage_used.to_string().as_str());
true
}
"%percentage_free%" => {
dst.push_str((100 - percentage_used).to_string().as_str());
true
}
other => {
dst.push_str(other);
true
}
},
);
self.common.draw(
cr,
text.as_str(),
&self.attrs,
self.common.dependence,
self.highlight.clone(),
self.common.images.clone(),
height,
ShowHide::Default(paused, self.waker.clone()),
format!("{self:?}"),
)
}
}
#[async_trait(?Send)]
impl PanelConfig for Storage {
/// - `interval`: how long to wait in seconds between each check
/// - type: u64
/// - default: 10
/// - `path`: the file path to check
/// - type: String
/// - default: `/proc/meminfo` - If you're considering changing this, you
/// might want to use a different panel like
/// [`Inotify`][crate::panels::Inotify]
/// - `format`: the format string
/// - type: String
/// - default: `RAM: %percentage_used%`
/// - formatting options: `%{gb,mb}_{total,used,free}%,
/// %percentage_{used,free}%`
/// - `attrs`: A string specifying the attrs for the panel. See
/// [`Attrs::parse`] for details.
/// - `highlight`: A string specifying the highlight for the panel. See
/// [`Highlight::parse`] for details.
/// - See [`PanelCommon::parse_common`].
fn parse(
name: &'static str,
table: &mut HashMap<String, config::Value>,
_global: &config::Config,
) -> anyhow::Result<Self> {
let mut builder = StorageBuilder::default();
builder.name(name);
if let Some(interval) = remove_uint_from_config("interval", table) {
builder.interval(Duration::from_secs(interval));
}
if let Some(path) = remove_string_from_config("path", table) {
builder.path(path);
}
let common = PanelCommon::parse_common(table)?;
let format =
PanelCommon::parse_format(table, "", "%path%: %percentage_used%%");
let attrs = PanelCommon::parse_attr(table, "");
let highlight = PanelCommon::parse_highlight(table, "");
builder.common(common);
builder.format(format.leak());
builder.attrs(attrs);
builder.highlight(highlight);
builder.formatter(AhoCorasick::new([
"%path%",
"%gb_total%",
"%gb_used%",
"%gb_free%",
"%mb_total%",
"%mb_used%",
"%mb_free%",
"%percentage_used%",
"%percentage_free%",
"%ramp%",
])?);
Ok(builder.build()?)
}
fn props(&self) -> (&'static str, bool) {
(self.name, self.common.visible)
}
async fn run(
mut self: Box<Self>,
cr: Rc<cairo::Context>,
global_attrs: Attrs,
height: i32,
) -> PanelRunResult {
self.attrs.apply_to(&global_attrs);
let paused = Arc::new(Mutex::new(false));
let stream = ManagedIntervalStream::builder()
.duration(self.interval)
.paused(paused.clone())
.build()?
.map(move |_| self.draw(&cr, height, paused.clone()));
Ok((Box::pin(stream), None))
}
}