Browse Source

Some performance improvements

Getty Ritter 5 years ago
parent
commit
14a6cd63db
4 changed files with 102 additions and 20 deletions
  1. 27 1
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 19 16
      src/main.rs
  4. 54 2
      src/widgets.rs

+ 27 - 1
Cargo.lock

@@ -30,6 +30,16 @@ dependencies = [
  "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "chrono"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
+ "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "glib"
 version = "0.7.1"
@@ -67,10 +77,10 @@ version = "0.1.0"
 dependencies = [
  "cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
  "pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
  "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -84,6 +94,19 @@ name = "libc"
 version = "0.2.50"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "num-integer"
+version = "0.1.39"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "pango"
 version = "0.6.0"
@@ -190,11 +213,14 @@ dependencies = [
 "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
 "checksum cairo-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9e09d8a818b2ccc8983f04d95a9350c3cf8d24cc456cedca3b88fa3a81fdc0e2"
 "checksum cairo-sys-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3fa13914fdc013387afa771f554f2f71d6ae931f4e5be9246c337d60c3dc484"
+"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
 "checksum glib 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e8fdc159c196a5dfa53a92929ac4c10c8a6637ffb43951f3fff89c2cd2365"
 "checksum glib-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5bda542f3caee39a027638e9644ff89204101ad916fd7370b585ad2c5fc97e61"
 "checksum gobject-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23e05a14290d3dc255223cba51db4b0f3da438d5250657996fa99b2a30faf43e"
 "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
 "checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1"
+"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
+"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1"
 "checksum pango 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4e36b55d7cd522bd183efeb3dfabc547bda1f26eadf8a1241dac09ab3fd3242c"
 "checksum pango-sys 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "747ab9cd4d537e6dc5ef0e4308c10dde8b706673b0237fed4e056b8d2c0b23c8"
 "checksum pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "706e885b46a16ba96f46f3c8d880ca6082c2c62693b75f3d167bd3aa4e34b0d4"

+ 2 - 1
Cargo.toml

@@ -8,7 +8,8 @@ edition = "2018"
 cairo-rs = "0.6"
 pango = "0.6"
 pangocairo = "0.7"
-time = "0.1"
+# time = "0.1"
+chrono = "*"
 libc = "0.2"
 
 [dependencies.cairo-sys-rs]

+ 19 - 16
src/main.rs

@@ -58,8 +58,19 @@ fn main() {
         tv_sec: 5,
         tv_usec: 0,
     };
+
+    let layout = pangocairo::functions::create_layout(&ctx).unwrap();
+
+    // allow for the whole width of the bar, minus a small fixed amount
+    layout.set_width((size.wd - 20) * pango::SCALE);
+    // this should also be configurable, but Fira Mono is a good font
+    let mut font = pango::FontDescription::from_string("Fira Mono 18");
+    font.set_weight(pango::Weight::Bold);
+    layout.set_font_description(&font);
+
     // do an initial pass at drawing the bar!
-    draw(&ctx, &input, size);
+    draw(&ctx, &layout, &input, size);
+
 
     // we're gonna keep looping until we don't
     loop {
@@ -68,6 +79,7 @@ fn main() {
             libc::FD_ZERO(&mut fds);
             libc::FD_SET(window_fd, &mut fds);
             libc::FD_SET(stdin_fd, &mut fds);
+            timer.tv_sec = 5;
 
             // this will block until there's input on either of the
             // above FDs or until five seconds have passed, whichever comes first
@@ -86,10 +98,10 @@ fn main() {
             use std::io::BufRead;
             input = String::new();
             stdin.read_line(&mut input).unwrap();
-            if input == "" {
+            if input.len() == 0 {
                 break;
             }
-            draw(&ctx, &input, size);
+            draw(&ctx, &layout, &input, size);
         }
 
         // if we have X11 events, handle them. If any one was a quit
@@ -102,14 +114,14 @@ fn main() {
         }
 
         // otherwise, draw the thing!
-        draw(&ctx, &input, size);
+        draw(&ctx, &layout, &input, size);
     }
 }
 
 
 /// Do our Cairo drawing. This needs to be refactored to allow for
 /// more configurability in terms of what gets written!
-fn draw(ctx: &cairo::Context, left: &str, size: Size) {
+fn draw(ctx: &cairo::Context, layout: &pango::Layout, left: &str, size: Size) {
     // the background is... gray-ish? this'll be configurable eventually
     ctx.set_source_rgb(0.1, 0.1, 0.1);
     ctx.paint();
@@ -117,15 +129,6 @@ fn draw(ctx: &cairo::Context, left: &str, size: Size) {
     // and the text is white
     ctx.set_source_rgb(1.0, 1.0, 1.0);
 
-    // Our pango layout handles placing and drawing text
-    let layout = pangocairo::functions::create_layout(&ctx).unwrap();
-    // allow for the whole width of the bar, minus a small fixed amount
-    layout.set_width((size.wd - 20) * pango::SCALE);
-    // this should also be configurable, but Fira Mono is a good font
-    let mut font = pango::FontDescription::from_string("Fira Mono 18");
-    font.set_weight(pango::Weight::Bold);
-    layout.set_font_description(&font);
-
     // set up a struct with everything that widgets need to draw
     let drawing = widgets::Drawing {
         ctx: ctx,
@@ -135,7 +138,7 @@ fn draw(ctx: &cairo::Context, left: &str, size: Size) {
     // set up our widgets
     let text = widgets::Text::new(left);
     let time = widgets::Time::new();
-    let sbox = widgets::SmallBox;
+    let bat = widgets::Battery;
 
     // and create a 'config' which tells us which widgets to draw from
     // the left, and which from the right
@@ -144,7 +147,7 @@ fn draw(ctx: &cairo::Context, left: &str, size: Size) {
             &text as &Widget,
         ],
         right: vec![
-            &sbox as &Widget,
+            &bat as &Widget,
             &time as &Widget,
         ],
     };

+ 54 - 2
src/widgets.rs

@@ -67,8 +67,8 @@ impl Time {
 
 impl Widget for Time {
     fn draw(&self, d: &Drawing, loc: Located) -> i32 {
-        let now = time::now();
-        loc.draw_text(d, &time::strftime(&self.fmt, &now).unwrap())
+        let now = chrono::Local::now();
+        loc.draw_text(d, &format!("{}", &now.format(&self.fmt)))
     }
 }
 
@@ -102,3 +102,55 @@ impl Widget for SmallBox {
         sz
     }
 }
+
+
+
+pub struct Battery;
+
+impl Battery {
+    fn read_status(&self) -> f64 {
+        use std::fs;
+
+        let mut batteries = Vec::new();
+        for entry in fs::read_dir("/sys/class/power_supply").unwrap() {
+            let e = entry.unwrap();
+            if e.file_name().to_string_lossy().starts_with("BAT") {
+                batteries.push(e.path());
+            }
+        }
+
+        let mut charges: Vec<i32> = Vec::new();
+        for mut bat in batteries {
+            bat.push("capacity");
+            let r = fs::read_to_string(bat).unwrap();
+            charges.push(r.trim().parse::<i32>().unwrap());
+        }
+
+        let len = charges.len() as f64;
+        let sum: i32 = charges.into_iter().sum();
+        sum as f64 / len / 100.0
+    }
+}
+
+impl Widget for Battery {
+    fn draw(&self, d: &Drawing, loc: Located) -> i32 {
+        let amt = self.read_status();
+        let sz = d.size.ht - 8;
+        let x = loc.target_x(d, sz);
+        if amt < 0.1 {
+            d.ctx.set_source_rgb(1.0, 0.0, 0.0);
+        } else if amt < 0.5 {
+            d.ctx.set_source_rgb(1.0, 1.0, 0.0);
+        } else {
+            d.ctx.set_source_rgb(0.0, 1.0, 0.5);
+        }
+        d.ctx.rectangle(x, 8.0, sz as f64 * amt, sz as f64 - 8.0);
+        d.ctx.fill();
+
+        d.ctx.set_source_rgb(1.0, 1.0, 1.0);
+        d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
+        d.ctx.stroke();
+
+        sz
+    }
+}