gdritter repos knurling / fe2caff
Switch battery check to be mutable-update form Getty Ritter 4 years ago
6 changed file(s) with 61 addition(s) and 26 deletion(s). Collapse all Expand all
9090 let section = section
9191 .as_table()
9292 .ok_or_else(|| format_err!("invalid config"))?;
93 let name = section["name"].as_str().ok_or_else(|| format_err!("invalid config"))?;
93 let name = section["name"]
94 .as_str()
95 .ok_or_else(|| format_err!("invalid config"))?;
9496 if name == "sep" {
9597 target = &mut conf.right;
9698 } else {
182184 }
183185
184186 pub fn update(&mut self) {
185 for w in self.left.iter_mut() { w.update() }
186 for w in self.right.iter_mut() { w.update() }
187 for w in self.left.iter_mut() {
188 w.update()
189 }
190 for w in self.right.iter_mut() {
191 w.update()
192 }
187193 }
188194
189195 pub fn font(&self) -> &str {
208214 layout.set_font_description(&font);
209215 layout.set_text("lj");
210216 let (_, h) = layout.get_size();
211 (h / pango::SCALE)
212 }
213 }
217 h / pango::SCALE
218 }
219 }
1 use crate::widgets::widget::{Widget,Drawing,Located};
1 use crate::widgets::widget::{Drawing, Located, Widget};
22
33 pub struct Battery {
44 file_list: Vec<std::path::PathBuf>,
55 charging: Option<std::path::PathBuf>,
6 last_status: f64,
7 last_charging: bool,
68 }
79
810 impl Battery {
1921 }
2022 }
2123 let ac_path = std::path::Path::new("/sys/class/power_supply/AC/online");
22
2324 Ok(Battery {
2425 file_list: batteries,
2526 charging: if ac_path.exists() {
2728 } else {
2829 None
2930 },
31 last_status: 1.0f64,
32 last_charging: false,
3033 })
3134 }
3235
5558
5659 impl Widget for Battery {
5760 fn draw(&self, d: &Drawing, loc: Located) -> i32 {
58 let amt = self.read_status();
61 let amt = self.last_status;
5962 let sz = d.size.ht - (d.buffer as i32 * 2);
6063 let x = loc.target_x(d, sz);
6164 match amt {
62 _ if self.is_charging().unwrap_or(false) => d.ctx.set_source_rgb(0.5, 0.5, 1.0),
63 Ok(x) if x < 0.1 => d.ctx.set_source_rgb(1.0, 0.0, 0.0),
64 Ok(x) if x < 0.5 => d.ctx.set_source_rgb(1.0, 1.0, 0.0),
65 Ok(_) => d.ctx.set_source_rgb(0.0, 1.0, 0.5),
66 Err(_) => d.ctx.set_source_rgb(0.0, 0.0, 0.0),
65 _ if self.last_charging => d.ctx.set_source_rgb(0.5, 0.5, 1.0),
66 x if x < 0.1 => d.ctx.set_source_rgb(1.0, 0.0, 0.0),
67 x if x < 0.5 => d.ctx.set_source_rgb(1.0, 1.0, 0.0),
68 _ => d.ctx.set_source_rgb(0.0, 1.0, 0.5),
6769 }
6870
6971 d.ctx.rectangle(
7072 x,
7173 d.buffer * 2.0,
72 sz as f64 * amt.unwrap_or(1.0),
74 sz as f64 * amt,
7375 sz as f64 - d.buffer * 2.0,
7476 );
7577 d.ctx.fill();
8183
8284 sz
8385 }
86
87 fn update_frequency(&self) -> Option<u64> {
88 Some(10)
89 }
90
91 fn update(&mut self) {
92 if let Ok(status) = self.read_status() {
93 self.last_status = status;
94 }
95
96 if let Ok(charging) = self.is_charging() {
97 self.last_charging = charging;
98 }
99 }
84100 }
33 pub mod standard;
44 pub mod widget;
55
6 pub use crate::widgets::widget::{Located,Drawing,Size,Widget};
6 pub use crate::widgets::widget::{Drawing, Located, Size, Widget};
77
8 const ALL_WIDGETS: [(&str, &dyn Fn(&toml::map::Map<String, toml::Value>) -> Result<Box<dyn Widget>, failure::Error>); 6] = [
8 const ALL_WIDGETS: [(
9 &str,
10 &dyn Fn(&toml::map::Map<String, toml::Value>) -> Result<Box<dyn Widget>, failure::Error>,
11 ); 6] = [
912 ("box", &|_| Ok(Box::new(standard::Time::new()))),
1013 ("battery", &|_| Ok(Box::new(battery::Battery::new()?))),
1114 ("caesura", &|_| Ok(Box::new(standard::Caesura))),
1215 ("mpd", &|config| {
13 let host = config["host"].as_str().ok_or_else(|| format_err!("MPD host should be a string"))?;
14 let port = config["port"].as_integer().ok_or_else(|| format_err!("MPD port should be an integer"))?;
16 let host = config["host"]
17 .as_str()
18 .ok_or_else(|| format_err!("MPD host should be a string"))?;
19 let port = config["port"]
20 .as_integer()
21 .ok_or_else(|| format_err!("MPD port should be an integer"))?;
1522 Ok(Box::new(mpd::MPD::new(host.to_string(), port as usize)))
1623 }),
1724 ("stdin", &|_| Ok(Box::new(standard::Stdin::new()))),
1825 ("time", &|_| Ok(Box::new(standard::Time::new()))),
1926 ];
2027
21 pub fn mk_widget(name: &str, section: &toml::map::Map<String, toml::Value>) -> Result<Box<dyn Widget>, failure::Error> {
28 pub fn mk_widget(
29 name: &str,
30 section: &toml::map::Map<String, toml::Value>,
31 ) -> Result<Box<dyn Widget>, failure::Error> {
2232 for (n, f) in ALL_WIDGETS.iter() {
2333 if n == &name {
2434 return f(section);
1 use crate::widgets::widget::{Widget,Drawing,Located};
1 use crate::widgets::widget::{Drawing, Located, Widget};
22
3 use std::io::{Write, BufRead, BufReader};
3 use std::io::{BufRead, BufReader, Write};
44 use std::net::TcpStream;
55
66 pub struct MPD {
1717 impl MPD {
1818 pub fn new(host: String, port: usize) -> MPD {
1919 let last_state = State::Stopped;
20 MPD {host, port, last_state}
20 MPD {
21 host,
22 port,
23 last_state,
24 }
2125 }
2226
2327 fn get_song(&self) -> Result<State, failure::Error> {
1 pub use crate::widgets::widget::{Located,Drawing,Size,Widget};
1 pub use crate::widgets::widget::{Drawing, Located, Size, Widget};
22
33 #[derive(Debug)]
44 pub struct Time {
4343 None
4444 }
4545
46 fn update(&mut self) {
47 }
46 fn update(&mut self) {}
4847
4948 fn draw(&self, d: &Drawing, loc: Located) -> i32;
5049 }