Bladeren bron

Add more blocks

Frans Bergman 1 jaar geleden
bovenliggende
commit
63274e8ba8
9 gewijzigde bestanden met toevoegingen van 433 en 24 verwijderingen
  1. 284 6
      Cargo.lock
  2. 3 0
      Cargo.toml
  3. 15 0
      src/block.rs
  4. 13 0
      src/block/date.rs
  5. 17 0
      src/block/mem.rs
  6. 33 0
      src/block/music.rs
  7. 28 0
      src/block/temp.rs
  8. 19 0
      src/block/volume.rs
  9. 21 18
      src/main.rs

+ 284 - 6
Cargo.lock

@@ -74,15 +74,128 @@ version = "0.8.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
 
+[[package]]
+name = "darling"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
+dependencies = [
+ "fnv",
+ "ident_case",
+ "proc-macro2",
+ "quote 1.0.29",
+ "strsim",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
+dependencies = [
+ "darling_core",
+ "quote 1.0.29",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "dbus"
+version = "0.9.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bb21987b9fb1613058ba3843121dd18b163b254d8a6e797e144cbac14d96d1b"
+dependencies = [
+ "libc",
+ "libdbus-sys",
+ "winapi",
+]
+
+[[package]]
+name = "derive_is_enum_variant"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0ac8859845146979953797f03cc5b282fb4396891807cdb3d04929a88418197"
+dependencies = [
+ "heck",
+ "quote 0.3.15",
+ "syn 0.11.11",
+]
+
 [[package]]
 name = "dwm-status"
 version = "0.1.0"
 dependencies = [
  "chrono",
+ "itertools",
+ "mpris",
+ "pulsectl-rs",
  "systemstat",
  "xcb",
 ]
 
+[[package]]
+name = "either"
+version = "1.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
+
+[[package]]
+name = "enum-kinds"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462"
+dependencies = [
+ "proc-macro2",
+ "quote 1.0.29",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "fnv"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+
+[[package]]
+name = "from_variants"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4e859c8f2057687618905dbe99fc76e836e0a69738865ef90e46fc214a41bbf2"
+dependencies = [
+ "from_variants_impl",
+]
+
+[[package]]
+name = "from_variants_impl"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55a5e644a80e6d96b2b4910fa7993301d7b7926c045b475b62202b20a36ce69e"
+dependencies = [
+ "darling",
+ "proc-macro2",
+ "quote 1.0.29",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "heck"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
+dependencies = [
+ "unicode-segmentation",
+]
+
 [[package]]
 name = "iana-time-zone"
 version = "0.1.57"
@@ -106,6 +219,21 @@ dependencies = [
  "cc",
 ]
 
+[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
+[[package]]
+name = "itertools"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
+dependencies = [
+ "either",
+]
+
 [[package]]
 name = "js-sys"
 version = "0.3.64"
@@ -127,6 +255,42 @@ version = "0.2.147"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
 
+[[package]]
+name = "libdbus-sys"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06085512b750d640299b79be4bad3d2fa90a9c00b1fd9e1b46364f66f0485c72"
+dependencies = [
+ "pkg-config",
+]
+
+[[package]]
+name = "libpulse-binding"
+version = "2.27.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1745b20bfc194ac12ef828f144f0ec2d4a7fe993281fa3567a0bd4969aee6890"
+dependencies = [
+ "bitflags",
+ "libc",
+ "libpulse-sys",
+ "num-derive",
+ "num-traits",
+ "winapi",
+]
+
+[[package]]
+name = "libpulse-sys"
+version = "1.20.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2191e6880818d1df4cf72eac8e91dce7a5a52ba0da4b2a5cdafabc22b937eadb"
+dependencies = [
+ "libc",
+ "num-derive",
+ "num-traits",
+ "pkg-config",
+ "winapi",
+]
+
 [[package]]
 name = "log"
 version = "0.4.19"
@@ -145,6 +309,19 @@ version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
 
+[[package]]
+name = "mpris"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55cef955a7826b1e00e901a3652e7a895abd221fb4ab61547e7d0e4c235d7feb"
+dependencies = [
+ "dbus",
+ "derive_is_enum_variant",
+ "enum-kinds",
+ "from_variants",
+ "thiserror",
+]
+
 [[package]]
 name = "nom"
 version = "7.1.3"
@@ -155,6 +332,17 @@ dependencies = [
  "minimal-lexical",
 ]
 
+[[package]]
+name = "num-derive"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
+dependencies = [
+ "proc-macro2",
+ "quote 1.0.29",
+ "syn 1.0.109",
+]
+
 [[package]]
 name = "num-traits"
 version = "0.2.15"
@@ -170,6 +358,12 @@ version = "1.18.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
 
+[[package]]
+name = "pkg-config"
+version = "0.3.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.64"
@@ -179,6 +373,15 @@ dependencies = [
  "unicode-ident",
 ]
 
+[[package]]
+name = "pulsectl-rs"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06a988bceed1981b2c5fc4a3da0e4e073fdaff8e6bd022b089f54bc573dc3cfc"
+dependencies = [
+ "libpulse-binding",
+]
+
 [[package]]
 name = "quick-xml"
 version = "0.28.2"
@@ -188,6 +391,12 @@ dependencies = [
  "memchr",
 ]
 
+[[package]]
+name = "quote"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
+
 [[package]]
 name = "quote"
 version = "1.0.29"
@@ -203,6 +412,34 @@ version = "1.0.171"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
 
+[[package]]
+name = "strsim"
+version = "0.10.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
+
+[[package]]
+name = "syn"
+version = "0.11.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
+dependencies = [
+ "quote 0.3.15",
+ "synom",
+ "unicode-xid",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.109"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
+dependencies = [
+ "proc-macro2",
+ "quote 1.0.29",
+ "unicode-ident",
+]
+
 [[package]]
 name = "syn"
 version = "2.0.25"
@@ -210,10 +447,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "15e3fc8c0c74267e2df136e5e5fb656a464158aa57624053375eb9c8c6e25ae2"
 dependencies = [
  "proc-macro2",
- "quote",
+ "quote 1.0.29",
  "unicode-ident",
 ]
 
+[[package]]
+name = "synom"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
+dependencies = [
+ "unicode-xid",
+]
+
 [[package]]
 name = "systemstat"
 version = "0.2.3"
@@ -228,6 +474,26 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "thiserror"
+version = "1.0.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42"
+dependencies = [
+ "thiserror-impl",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f"
+dependencies = [
+ "proc-macro2",
+ "quote 1.0.29",
+ "syn 2.0.25",
+]
+
 [[package]]
 name = "time"
 version = "0.1.45"
@@ -261,6 +527,18 @@ version = "1.0.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
 
+[[package]]
+name = "unicode-segmentation"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
+
+[[package]]
+name = "unicode-xid"
+version = "0.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
+
 [[package]]
 name = "wasi"
 version = "0.10.0+wasi-snapshot-preview1"
@@ -287,8 +565,8 @@ dependencies = [
  "log",
  "once_cell",
  "proc-macro2",
- "quote",
- "syn",
+ "quote 1.0.29",
+ "syn 2.0.25",
  "wasm-bindgen-shared",
 ]
 
@@ -298,7 +576,7 @@ version = "0.2.87"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
 dependencies = [
- "quote",
+ "quote 1.0.29",
  "wasm-bindgen-macro-support",
 ]
 
@@ -309,8 +587,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
 dependencies = [
  "proc-macro2",
- "quote",
- "syn",
+ "quote 1.0.29",
+ "syn 2.0.25",
  "wasm-bindgen-backend",
  "wasm-bindgen-shared",
 ]

+ 3 - 0
Cargo.toml

@@ -7,5 +7,8 @@ edition = "2021"
 
 [dependencies]
 chrono = "0.4.26"
+itertools = "0.11.0"
+mpris = "2.0.1"
+pulsectl-rs = "0.3.2"
 systemstat = "0.2.3"
 xcb = "1.2.1"

+ 15 - 0
src/block.rs

@@ -0,0 +1,15 @@
+mod date;
+mod mem;
+mod music;
+mod temp;
+mod volume;
+
+pub use date::Date;
+pub use mem::Mem;
+pub use music::Music;
+pub use temp::Temp;
+pub use volume::Volume;
+
+pub trait Block {
+    fn display(&self) -> String;
+}

+ 13 - 0
src/block/date.rs

@@ -0,0 +1,13 @@
+use chrono::prelude::*;
+
+use crate::block::Block;
+
+pub struct Date {
+    pub format: String,
+}
+
+impl Block for Date {
+    fn display(&self) -> String {
+        Local::now().format(self.format.as_str()).to_string()
+    }
+}

+ 17 - 0
src/block/mem.rs

@@ -0,0 +1,17 @@
+use systemstat::{saturating_sub_bytes, Platform, System};
+
+use crate::block::Block;
+
+pub struct Mem {}
+
+impl Block for Mem {
+    fn display(&self) -> String {
+        let sys = System::new();
+
+        if let Ok(mem) = sys.memory() {
+            format!("{}", saturating_sub_bytes(mem.total, mem.free))
+        } else {
+            "".to_string()
+        }
+    }
+}

+ 33 - 0
src/block/music.rs

@@ -0,0 +1,33 @@
+use itertools::Itertools;
+use mpris::PlayerFinder;
+
+use crate::block::Block;
+
+pub struct Music {}
+
+impl Block for Music {
+    fn display(&self) -> String {
+        let finder = PlayerFinder::new().expect("Could not create finder");
+
+        let player = finder.find_active();
+
+        match player {
+            Ok(player) => {
+                let meta = player.get_metadata().expect("Could not get metadata");
+                let artist: String = meta
+                    .artists()
+                    .map(|a| a.into_iter().intersperse(", ").collect())
+                    .unwrap_or("".to_string());
+                let track: String = meta
+                    .title()
+                    .map(|a| a.to_string())
+                    .unwrap_or("".to_string());
+                format!("{} - {}", artist, track)
+            }
+            Err(e) => {
+                println!("{:?}", e);
+                "".to_string()
+            }
+        }
+    }
+}

+ 28 - 0
src/block/temp.rs

@@ -0,0 +1,28 @@
+use systemstat::{Platform, System};
+
+use crate::block::Block;
+
+pub struct Temp {}
+
+const COLOR_RESET: char = '\x01';
+const COLOR_YELLOW: char = '\x03';
+const COLOR_RED: char = '\x04';
+
+impl Block for Temp {
+    fn display(&self) -> String {
+        let sys = System::new();
+
+        if let Ok(temp) = sys.cpu_temp() {
+            let color = if temp > 70.0 {
+                COLOR_RED
+            } else if temp > 50.0 {
+                COLOR_YELLOW
+            } else {
+                COLOR_RESET
+            };
+            format!("{}+{}°C{}", color, temp, COLOR_RESET)
+        } else {
+            "".to_string()
+        }
+    }
+}

+ 19 - 0
src/block/volume.rs

@@ -0,0 +1,19 @@
+use pulsectl::controllers::DeviceControl;
+use pulsectl::controllers::SinkController;
+
+use crate::block::Block;
+
+pub struct Volume {}
+
+impl Block for Volume {
+    fn display(&self) -> String {
+        let mut handler = SinkController::create().unwrap();
+
+        handler
+            .get_default_device()
+            .expect("No device")
+            .volume
+            .min()
+            .to_string()
+    }
+}

+ 21 - 18
src/main.rs

@@ -1,25 +1,12 @@
+mod block;
+
 use std::thread;
 use std::time::Duration;
 
+use itertools::Itertools;
 use xcb::x;
 
-use systemstat::{saturating_sub_bytes, Platform, System};
-
-use chrono::prelude::*;
-
-fn date_block() -> String {
-    Local::now().format("%A %d %B | %H:%M").to_string()
-}
-
-fn mem_block() -> String {
-    let sys = System::new();
-
-    if let Ok(mem) = sys.memory() {
-        format!("{}", saturating_sub_bytes(mem.total, mem.free))
-    } else {
-        "".to_string()
-    }
-}
+use block::*;
 
 fn main() -> xcb::Result<()> {
     let (conn, screen_num) = xcb::Connection::connect(None)?;
@@ -28,8 +15,24 @@ fn main() -> xcb::Result<()> {
     let setup = conn.get_setup();
     let screen = setup.roots().nth(screen_num as usize).unwrap();
 
+    let blocks: Vec<Box<dyn Block>> = vec![
+        Box::new(Music {}),
+        Box::new(Volume {}),
+        Box::new(Temp {}),
+        //Box::new(Mem {}),
+        Box::new(Date {
+            format: "%A %d %B | %H:%M".to_string(),
+        }),
+    ];
+
     loop {
-        let status_bar = format!(" {} | {} ", mem_block(), date_block());
+        let status_bar: String = blocks
+            .iter()
+            .map(|block| block.display())
+            .intersperse(" | ".to_string())
+            .collect();
+
+        let status_bar = format!(" {} ", status_bar);
 
         conn.send_request(&x::ChangeProperty {
             mode: x::PropMode::Replace,