gdritter repos knurling / 14a6cd6
Some performance improvements Getty Ritter 5 years ago
4 changed file(s) with 102 addition(s) and 20 deletion(s). Collapse all Expand all
3131 ]
3232
3333 [[package]]
34 name = "chrono"
35 version = "0.4.6"
36 source = "registry+https://github.com/rust-lang/crates.io-index"
37 dependencies = [
38 "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
39 "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
40 "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
41 ]
42
43 [[package]]
3444 name = "glib"
3545 version = "0.7.1"
3646 source = "registry+https://github.com/rust-lang/crates.io-index"
6777 dependencies = [
6878 "cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
6979 "cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
80 "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
7081 "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
7182 "pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
7283 "pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
73 "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
7484 "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
7585 ]
7686
8292 [[package]]
8393 name = "libc"
8494 version = "0.2.50"
95 source = "registry+https://github.com/rust-lang/crates.io-index"
96
97 [[package]]
98 name = "num-integer"
99 version = "0.1.39"
100 source = "registry+https://github.com/rust-lang/crates.io-index"
101 dependencies = [
102 "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
103 ]
104
105 [[package]]
106 name = "num-traits"
107 version = "0.2.6"
85108 source = "registry+https://github.com/rust-lang/crates.io-index"
86109
87110 [[package]]
190213 "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
191214 "checksum cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e09d8a818b2ccc8983f04d95a9350c3cf8d24cc456cedca3b88fa3a81fdc0e2"
192215 "checksum cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3fa13914fdc013387afa771f554f2f71d6ae931f4e5be9246c337d60c3dc484"
216 "checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
193217 "checksum glib 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e8fdc159c196a5dfa53a92929ac4c10c8a6637ffb43951f3fff89c2cd2365"
194218 "checksum glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5bda542f3caee39a027638e9644ff89204101ad916fd7370b585ad2c5fc97e61"
195219 "checksum gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23e05a14290d3dc255223cba51db4b0f3da438d5250657996fa99b2a30faf43e"
196220 "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
197221 "checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1"
222 "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
223 "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
198224 "checksum pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4e36b55d7cd522bd183efeb3dfabc547bda1f26eadf8a1241dac09ab3fd3242c"
199225 "checksum pango-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "747ab9cd4d537e6dc5ef0e4308c10dde8b706673b0237fed4e056b8d2c0b23c8"
200226 "checksum pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "706e885b46a16ba96f46f3c8d880ca6082c2c62693b75f3d167bd3aa4e34b0d4"
88 cairo-rs = "0.6"
99 pango = "0.6"
1010 pangocairo = "0.7"
11 time = "0.1"
11 # time = "0.1"
12 chrono = "*"
1213 libc = "0.2"
1314
1415 [dependencies.cairo-sys-rs]
5858 tv_sec: 5,
5959 tv_usec: 0,
6060 };
61
62 let layout = pangocairo::functions::create_layout(&ctx).unwrap();
63
64 // allow for the whole width of the bar, minus a small fixed amount
65 layout.set_width((size.wd - 20) * pango::SCALE);
66 // this should also be configurable, but Fira Mono is a good font
67 let mut font = pango::FontDescription::from_string("Fira Mono 18");
68 font.set_weight(pango::Weight::Bold);
69 layout.set_font_description(&font);
70
6171 // do an initial pass at drawing the bar!
62 draw(&ctx, &input, size);
72 draw(&ctx, &layout, &input, size);
73
6374
6475 // we're gonna keep looping until we don't
6576 loop {
6879 libc::FD_ZERO(&mut fds);
6980 libc::FD_SET(window_fd, &mut fds);
7081 libc::FD_SET(stdin_fd, &mut fds);
82 timer.tv_sec = 5;
7183
7284 // this will block until there's input on either of the
7385 // above FDs or until five seconds have passed, whichever comes first
8698 use std::io::BufRead;
8799 input = String::new();
88100 stdin.read_line(&mut input).unwrap();
89 if input == "" {
101 if input.len() == 0 {
90102 break;
91103 }
92 draw(&ctx, &input, size);
104 draw(&ctx, &layout, &input, size);
93105 }
94106
95107 // if we have X11 events, handle them. If any one was a quit
102114 }
103115
104116 // otherwise, draw the thing!
105 draw(&ctx, &input, size);
117 draw(&ctx, &layout, &input, size);
106118 }
107119 }
108120
109121
110122 /// Do our Cairo drawing. This needs to be refactored to allow for
111123 /// more configurability in terms of what gets written!
112 fn draw(ctx: &cairo::Context, left: &str, size: Size) {
124 fn draw(ctx: &cairo::Context, layout: &pango::Layout, left: &str, size: Size) {
113125 // the background is... gray-ish? this'll be configurable eventually
114126 ctx.set_source_rgb(0.1, 0.1, 0.1);
115127 ctx.paint();
116128
117129 // and the text is white
118130 ctx.set_source_rgb(1.0, 1.0, 1.0);
119
120 // Our pango layout handles placing and drawing text
121 let layout = pangocairo::functions::create_layout(&ctx).unwrap();
122 // allow for the whole width of the bar, minus a small fixed amount
123 layout.set_width((size.wd - 20) * pango::SCALE);
124 // this should also be configurable, but Fira Mono is a good font
125 let mut font = pango::FontDescription::from_string("Fira Mono 18");
126 font.set_weight(pango::Weight::Bold);
127 layout.set_font_description(&font);
128131
129132 // set up a struct with everything that widgets need to draw
130133 let drawing = widgets::Drawing {
135138 // set up our widgets
136139 let text = widgets::Text::new(left);
137140 let time = widgets::Time::new();
138 let sbox = widgets::SmallBox;
141 let bat = widgets::Battery;
139142
140143 // and create a 'config' which tells us which widgets to draw from
141144 // the left, and which from the right
144147 &text as &Widget,
145148 ],
146149 right: vec![
147 &sbox as &Widget,
150 &bat as &Widget,
148151 &time as &Widget,
149152 ],
150153 };
6767
6868 impl Widget for Time {
6969 fn draw(&self, d: &Drawing, loc: Located) -> i32 {
70 let now = time::now();
71 loc.draw_text(d, &time::strftime(&self.fmt, &now).unwrap())
70 let now = chrono::Local::now();
71 loc.draw_text(d, &format!("{}", &now.format(&self.fmt)))
7272 }
7373 }
7474
102102 sz
103103 }
104104 }
105
106
107
108 pub struct Battery;
109
110 impl Battery {
111 fn read_status(&self) -> f64 {
112 use std::fs;
113
114 let mut batteries = Vec::new();
115 for entry in fs::read_dir("/sys/class/power_supply").unwrap() {
116 let e = entry.unwrap();
117 if e.file_name().to_string_lossy().starts_with("BAT") {
118 batteries.push(e.path());
119 }
120 }
121
122 let mut charges: Vec<i32> = Vec::new();
123 for mut bat in batteries {
124 bat.push("capacity");
125 let r = fs::read_to_string(bat).unwrap();
126 charges.push(r.trim().parse::<i32>().unwrap());
127 }
128
129 let len = charges.len() as f64;
130 let sum: i32 = charges.into_iter().sum();
131 sum as f64 / len / 100.0
132 }
133 }
134
135 impl Widget for Battery {
136 fn draw(&self, d: &Drawing, loc: Located) -> i32 {
137 let amt = self.read_status();
138 let sz = d.size.ht - 8;
139 let x = loc.target_x(d, sz);
140 if amt < 0.1 {
141 d.ctx.set_source_rgb(1.0, 0.0, 0.0);
142 } else if amt < 0.5 {
143 d.ctx.set_source_rgb(1.0, 1.0, 0.0);
144 } else {
145 d.ctx.set_source_rgb(0.0, 1.0, 0.5);
146 }
147 d.ctx.rectangle(x, 8.0, sz as f64 * amt, sz as f64 - 8.0);
148 d.ctx.fill();
149
150 d.ctx.set_source_rgb(1.0, 1.0, 1.0);
151 d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
152 d.ctx.stroke();
153
154 sz
155 }
156 }