Modify battery widget to indicate when charging
Getty Ritter
5 years ago
95 | 95 | |
96 | 96 | pub struct Battery { |
97 | 97 | file_list: Vec<std::path::PathBuf>, |
98 | charging: Option<std::path::PathBuf> | |
98 | 99 | } |
99 | 100 | |
100 | 101 | impl Battery { |
110 | 111 | batteries.push(path); |
111 | 112 | } |
112 | 113 | } |
114 | let ac_path = std::path::Path::new("/sys/class/power_supply/AC/online"); | |
113 | 115 | |
114 | 116 | Ok(Battery { |
115 | 117 | file_list: batteries, |
118 | charging: if ac_path.exists() { | |
119 | Some(ac_path.to_path_buf()) | |
120 | } else { | |
121 | None | |
122 | }, | |
116 | 123 | }) |
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 | } | |
117 | 134 | } |
118 | 135 | |
119 | 136 | fn read_status(&self) -> Result<f64, failure::Error> { |
135 | 152 | let sz = d.size.ht - 8; |
136 | 153 | let x = loc.target_x(d, sz); |
137 | 154 | match amt { |
155 | _ if self.is_charging().unwrap_or(false) => | |
156 | d.ctx.set_source_rgb(0.5, 0.5, 1.0), | |
138 | 157 | Ok(x) if x < 0.1 => |
139 | 158 | d.ctx.set_source_rgb(1.0, 0.0, 0.0), |
140 | 159 | Ok(x) if x < 0.5 => |