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
use std::collections::HashMap;
use std::process::Command;
use std::time::Duration;
use failure::ResultExt;
use lazy_static::lazy_static;
use regex::Regex;
use tokio_timer::Timer;
use crate::text::{Attributes, Text};
use crate::{Cnx, Result};
#[derive(Debug, PartialEq)]
struct Value<'a> {
temp: &'a str,
units: &'a str,
}
fn parse_sensors_output(output: &str) -> Result<HashMap<&str, Value<'_>>> {
lazy_static! {
static ref RE: Regex = Regex::new(
r"\n(?P<name>[\w ]+):\s+\+?(?P<temp>-?\d+\.\d+).(?P<units>[C|F])"
).expect("Failed to compile regex for parsing sensors output");
}
let mut map = HashMap::new();
for mat in RE.captures_iter(output) {
map.insert(
mat.name("name").unwrap().as_str(),
Value {
temp: mat.name("temp").unwrap().as_str(),
units: mat.name("units").unwrap().as_str(),
},
);
}
Ok(map)
}
pub struct Sensors {
timer: Timer,
update_interval: Duration,
attr: Attributes,
sensors: Vec<String>,
}
impl Sensors {
pub fn new<S: Into<String>>(cnx: &Cnx, attr: Attributes, sensors: Vec<S>) -> Sensors {
Sensors {
timer: cnx.timer(),
update_interval: Duration::from_secs(60),
attr,
sensors: sensors.into_iter().map(Into::into).collect(),
}
}
fn tick(&self) -> Result<Vec<Text>> {
let output = Command::new("sensors")
.output()
.context("Failed to run `sensors`")?;
let string = String::from_utf8(output.stdout).context("Invalid UTF-8 in sensors output")?;
let parsed = parse_sensors_output(&string).context("Failed to parse `sensors` output")?;
self.sensors
.iter()
.map(|sensor_name| {
let text = parsed
.get::<str>(sensor_name)
.map_or("?".to_owned(), |&Value { temp, units }| {
format!("{}°{}", temp, units)
});
Ok(Text {
attr: self.attr.clone(),
text,
stretch: false,
})
})
.collect()
}
}
timer_widget!(Sensors, timer, update_interval, tick);
#[cfg(test)]
mod test {
use super::{parse_sensors_output, Value};
#[test]
fn works() {
let output = r#"applesmc-isa-0300
Adapter: ISA adapter
Right Side : 0 RPM (min = 2000 RPM, max = 6199 RPM)
Ts1S: -127.0 C
Ts2S: +34.0 F
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +58.0 C (high = +105.0 C, crit = +105.0 C)
Core 0: +53.0 C (high = +105.0 C, crit = +105.0 C)
Core 1: +58.0 C (high = +105.0 C, crit = +105.0 C)
"#;
let parsed = parse_sensors_output(output).unwrap();
assert_eq!(
parsed.get("Core 0"),
Some(&Value {
temp: "53.0",
units: "C",
})
);
assert_eq!(
parsed.get("Core 1"),
Some(&Value {
temp: "58.0",
units: "C",
})
);
assert_eq!(
parsed.get("Ts1S"),
Some(&Value {
temp: "-127.0",
units: "C",
})
);
assert_eq!(
parsed.get("Ts2S"),
Some(&Value {
temp: "34.0",
units: "F",
})
);
assert_eq!(parsed.len(), 5);
}
}