Some performance improvements
Getty Ritter
5 years ago
31 | 31 | ] |
32 | 32 | |
33 | 33 | [[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]] | |
34 | 44 | name = "glib" |
35 | 45 | version = "0.7.1" |
36 | 46 | source = "registry+https://github.com/rust-lang/crates.io-index" |
67 | 77 | dependencies = [ |
68 | 78 | "cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", |
69 | 79 | "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)", | |
70 | 81 | "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", |
71 | 82 | "pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", |
72 | 83 | "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)", | |
74 | 84 | "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)", |
75 | 85 | ] |
76 | 86 | |
82 | 92 | [[package]] |
83 | 93 | name = "libc" |
84 | 94 | 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" | |
85 | 108 | source = "registry+https://github.com/rust-lang/crates.io-index" |
86 | 109 | |
87 | 110 | [[package]] |
190 | 213 | "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" |
191 | 214 | "checksum cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e09d8a818b2ccc8983f04d95a9350c3cf8d24cc456cedca3b88fa3a81fdc0e2" |
192 | 215 | "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" | |
193 | 217 | "checksum glib 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e8fdc159c196a5dfa53a92929ac4c10c8a6637ffb43951f3fff89c2cd2365" |
194 | 218 | "checksum glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5bda542f3caee39a027638e9644ff89204101ad916fd7370b585ad2c5fc97e61" |
195 | 219 | "checksum gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23e05a14290d3dc255223cba51db4b0f3da438d5250657996fa99b2a30faf43e" |
196 | 220 | "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" |
197 | 221 | "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" | |
198 | 224 | "checksum pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4e36b55d7cd522bd183efeb3dfabc547bda1f26eadf8a1241dac09ab3fd3242c" |
199 | 225 | "checksum pango-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "747ab9cd4d537e6dc5ef0e4308c10dde8b706673b0237fed4e056b8d2c0b23c8" |
200 | 226 | "checksum pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "706e885b46a16ba96f46f3c8d880ca6082c2c62693b75f3d167bd3aa4e34b0d4" |
8 | 8 | cairo-rs = "0.6" |
9 | 9 | pango = "0.6" |
10 | 10 | pangocairo = "0.7" |
11 |
|
|
11 | # time = "0.1" | |
12 | chrono = "*" | |
12 | 13 | libc = "0.2" |
13 | 14 | |
14 | 15 | [dependencies.cairo-sys-rs] |
58 | 58 | tv_sec: 5, |
59 | 59 | tv_usec: 0, |
60 | 60 | }; |
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 | ||
61 | 71 | // do an initial pass at drawing the bar! |
62 |
draw(&ctx, & |
|
72 | draw(&ctx, &layout, &input, size); | |
73 | ||
63 | 74 | |
64 | 75 | // we're gonna keep looping until we don't |
65 | 76 | loop { |
68 | 79 | libc::FD_ZERO(&mut fds); |
69 | 80 | libc::FD_SET(window_fd, &mut fds); |
70 | 81 | libc::FD_SET(stdin_fd, &mut fds); |
82 | timer.tv_sec = 5; | |
71 | 83 | |
72 | 84 | // this will block until there's input on either of the |
73 | 85 | // above FDs or until five seconds have passed, whichever comes first |
86 | 98 | use std::io::BufRead; |
87 | 99 | input = String::new(); |
88 | 100 | stdin.read_line(&mut input).unwrap(); |
89 |
if input |
|
101 | if input.len() == 0 { | |
90 | 102 | break; |
91 | 103 | } |
92 |
draw(&ctx, & |
|
104 | draw(&ctx, &layout, &input, size); | |
93 | 105 | } |
94 | 106 | |
95 | 107 | // if we have X11 events, handle them. If any one was a quit |
102 | 114 | } |
103 | 115 | |
104 | 116 | // otherwise, draw the thing! |
105 |
draw(&ctx, & |
|
117 | draw(&ctx, &layout, &input, size); | |
106 | 118 | } |
107 | 119 | } |
108 | 120 | |
109 | 121 | |
110 | 122 | /// Do our Cairo drawing. This needs to be refactored to allow for |
111 | 123 | /// more configurability in terms of what gets written! |
112 |
fn draw(ctx: &cairo::Context, l |
|
124 | fn draw(ctx: &cairo::Context, layout: &pango::Layout, left: &str, size: Size) { | |
113 | 125 | // the background is... gray-ish? this'll be configurable eventually |
114 | 126 | ctx.set_source_rgb(0.1, 0.1, 0.1); |
115 | 127 | ctx.paint(); |
116 | 128 | |
117 | 129 | // and the text is white |
118 | 130 | 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); | |
128 | 131 | |
129 | 132 | // set up a struct with everything that widgets need to draw |
130 | 133 | let drawing = widgets::Drawing { |
135 | 138 | // set up our widgets |
136 | 139 | let text = widgets::Text::new(left); |
137 | 140 | let time = widgets::Time::new(); |
138 |
let |
|
141 | let bat = widgets::Battery; | |
139 | 142 | |
140 | 143 | // and create a 'config' which tells us which widgets to draw from |
141 | 144 | // the left, and which from the right |
144 | 147 | &text as &Widget, |
145 | 148 | ], |
146 | 149 | right: vec![ |
147 |
& |
|
150 | &bat as &Widget, | |
148 | 151 | &time as &Widget, |
149 | 152 | ], |
150 | 153 | }; |
67 | 67 | |
68 | 68 | impl Widget for Time { |
69 | 69 | 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))) | |
72 | 72 | } |
73 | 73 | } |
74 | 74 | |
102 | 102 | sz |
103 | 103 | } |
104 | 104 | } |
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 | } |