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
use std::fs::{read_to_string, OpenOptions};
use std::io::prelude::*;
use std::time::Duration;
use crossbeam_channel::Sender;
use serde_derive::Deserialize;
use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::de::deserialize_duration;
use crate::errors::*;
use crate::formatting::value::Value;
use crate::formatting::FormatTemplate;
use crate::scheduler::Task;
use crate::widgets::text::TextWidget;
use crate::widgets::{I3BarWidget, State};
pub struct Load {
id: usize,
text: TextWidget,
logical_cores: u32,
format: FormatTemplate,
update_interval: Duration,
minimum_info: f64,
minimum_warning: f64,
minimum_critical: f64,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
pub struct LoadConfig {
pub format: String,
#[serde(deserialize_with = "deserialize_duration")]
pub interval: Duration,
pub info: f64,
pub warning: f64,
pub critical: f64,
}
impl Default for LoadConfig {
fn default() -> Self {
Self {
format: "{1m}".to_string(),
interval: Duration::from_secs(5),
info: 0.3,
warning: 0.6,
critical: 0.9,
}
}
}
impl ConfigBlock for Load {
type Config = LoadConfig;
fn new(
id: usize,
block_config: Self::Config,
shared_config: SharedConfig,
_tx_update_request: Sender<Task>,
) -> Result<Self> {
let text = TextWidget::new(id, 0, shared_config)
.with_icon("cogs")?
.with_state(State::Info);
let content = read_to_string("/proc/cpuinfo")
.block_error("load", "Your system doesn't support /proc/cpuinfo")?;
let logical_cores = content
.lines()
.filter(|l| l.starts_with("processor"))
.count() as u32;
Ok(Load {
id,
logical_cores,
update_interval: block_config.interval,
minimum_info: block_config.info,
minimum_warning: block_config.warning,
minimum_critical: block_config.critical,
format: FormatTemplate::from_string(&block_config.format)
.block_error("load", "Invalid format specified for load")?,
text,
})
}
}
impl Block for Load {
fn update(&mut self) -> Result<Option<Update>> {
let mut f = OpenOptions::new()
.read(true)
.open("/proc/loadavg")
.block_error(
"load",
"Your system does not support reading the load average from /proc/loadavg",
)?;
let mut loadavg = String::new();
f.read_to_string(&mut loadavg)
.block_error("load", "Failed to read the load average of your system!")?;
let split: Vec<f64> = (&loadavg)
.split(' ')
.take(3)
.map(|x| x.parse().unwrap())
.collect();
let values = map!(
"1m" => Value::from_float(split[0]),
"5m" => Value::from_float(split[1]),
"15m" => Value::from_float(split[2]),
);
let used_perc = split[0] / (self.logical_cores as f64);
self.text.set_state(match used_perc {
x if x > self.minimum_critical => State::Critical,
x if x > self.minimum_warning => State::Warning,
x if x > self.minimum_info => State::Info,
_ => State::Idle,
});
self.text.set_text(self.format.render(&values)?);
Ok(Some(self.update_interval.into()))
}
fn view(&self) -> Vec<&dyn I3BarWidget> {
vec![&self.text]
}
fn id(&self) -> usize {
self.id
}
}