gdritter repos knurling / 6bff348
Modify battery widget to indicate when charging Getty Ritter 4 years ago
1 changed file(s) with 19 addition(s) and 0 deletion(s). Collapse all Expand all
9595
9696 pub struct Battery {
9797 file_list: Vec<std::path::PathBuf>,
98 charging: Option<std::path::PathBuf>
9899 }
99100
100101 impl Battery {
110111 batteries.push(path);
111112 }
112113 }
114 let ac_path = std::path::Path::new("/sys/class/power_supply/AC/online");
113115
114116 Ok(Battery {
115117 file_list: batteries,
118 charging: if ac_path.exists() {
119 Some(ac_path.to_path_buf())
120 } else {
121 None
122 },
116123 })
124 }
125
126 fn is_charging(&self) -> Result<bool, failure::Error> {
127 if let Some(path) = &self.charging {
128 let is_connected: i32 =
129 std::fs::read_to_string(path)?.trim().parse()?;
130 Ok(is_connected != 0)
131 } else {
132 Ok(false)
133 }
117134 }
118135
119136 fn read_status(&self) -> Result<f64, failure::Error> {
135152 let sz = d.size.ht - 8;
136153 let x = loc.target_x(d, sz);
137154 match amt {
155 _ if self.is_charging().unwrap_or(false) =>
156 d.ctx.set_source_rgb(0.5, 0.5, 1.0),
138157 Ok(x) if x < 0.1 =>
139158 d.ctx.set_source_rgb(1.0, 0.0, 0.0),
140159 Ok(x) if x < 0.5 =>