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
|
use crate::error::{ Error, Result };
use crate::facility::{ FacilityMap, FacilityType };
use crate::facility::envsys::EnvSys;
use crate::module::{ CreatableModule, Module };
use std::time::Duration;
use async_channel::Sender;
use async_executor::StaticExecutor;
pub struct BatteryModule {
is_visible: bool,
full_threshold: f32,
}
impl CreatableModule for BatteryModule {
const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS;
async fn new(facilities: &mut FacilityMap,
_executor: &'static StaticExecutor,
_redraw_sender: Sender<()>)
-> Result<BatteryModule>
{
let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom(
"Battery module can't access envsys facility."))?;
let mut is_visible = false;
for name in envsys.device_iter() {
if Self::is_battery_device(name) {
if Self::is_present(envsys, name)? {
is_visible = true;
}
}
}
let full_threshold = 96.5 / 100.0;
Ok(BatteryModule { is_visible, full_threshold })
}
}
impl Module for BatteryModule {
fn format(&mut self, facilities: &mut FacilityMap) -> Result<String> {
let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom(
"Battery module can't access envsys facility."))?;
/* When there's more than one battery, we want to present them in an
* order which is both stable and predictable. Their device names are also
* user-visible via envstat(8), so lexicographic ordering will be fine for
* both needs. Anyway, to make that happen, we collect and sort the ones
* we're interested in in advance.
*/
let mut devices = Vec::new();
for name in envsys.device_iter() {
if Self::is_battery_device(name) {
if Self::is_present(envsys, name)? {
devices.push(name);
}
}
}
devices.sort();
self.is_visible = !devices.is_empty();
let mut text = String::new();
for name in &devices {
if !text.is_empty() {
text.push_str(" ");
}
let is_charging = Self::is_charging(envsys, name)?;
let ratio = Self::ratio(envsys, name)?;
let is_full = ratio >= self.full_threshold;
if is_full && ! is_charging {
text.push_str("\u{1F333}100");
} else {
let percentage = (ratio * 100.0).round() as i32;
if is_charging {
text.push_str(&format!("\u{2668}{}%", percentage));
} else {
text.push_str(&format!("\u{1f343}{}%", percentage));
}
}
}
Ok(text)
}
fn sleep_duration(&self) -> Option<Duration> {
None
}
fn is_visible(&self) -> bool {
self.is_visible
}
}
impl BatteryModule {
fn is_battery_device(name: &str) -> bool {
name.starts_with("acpibat")
}
fn is_present(envsys: &EnvSys, device_name: &str) -> Result<bool> {
Ok(envsys.get_current_value(device_name, "present")? != 0)
}
fn is_charging(envsys: &EnvSys, device_name: &str) -> Result<bool> {
Ok(envsys.get_current_value(device_name, "charging")? != 0)
}
fn ratio(envsys: &EnvSys, device_name: &str) -> Result<f32> {
let current_charge = envsys.get_current_value(device_name, "charge")?;
let maximum_charge = envsys.get_maximum_value(device_name, "charge")?;
Ok(current_charge as f32 / maximum_charge as f32)
}
}
|