From 9dd0a1aa109fc1c7f152537c41b9c64081311e49 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sat, 2 Apr 2022 23:02:08 +0200 Subject: [PATCH] More on the sysinfo bar module --- src/bar/module/sysinfo.rs | 67 +++++++++++++++++++++++++++++++++------ src/fmt_replace.rs | 2 ++ src/lib.rs | 1 - src/rtfmt.rs | 9 ++++++ src/tree.rs | 1 + 5 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/bar/module/sysinfo.rs b/src/bar/module/sysinfo.rs index 1f29d1d..95a62c9 100644 --- a/src/bar/module/sysinfo.rs +++ b/src/bar/module/sysinfo.rs @@ -16,21 +16,66 @@ //! The date `swayrbar` module. use crate::bar::module::BarModuleFn; +use crate::fmt_replace::fmt_replace; use std::cell::RefCell; +use std::sync::Once; use swaybar_types as s; -use sysinfo; +use sysinfo as si; use sysinfo::SystemExt; pub struct BarModuleSysInfo { pub instance: String, - system: RefCell, + system: RefCell, +} + +struct Updater { + cpu: Once, + memory: Once, +} + +impl Updater { + fn new() -> Updater { + Updater { + cpu: Once::new(), + memory: Once::new(), + } + } + + fn refresh_cpu(&self, sys: &RefCell) { + self.cpu.call_once(|| sys.borrow_mut().refresh_cpu()); + } + + fn refresh_memory(&self, sys: &RefCell) { + self.memory.call_once(|| sys.borrow_mut().refresh_memory()); + } +} + +#[derive(Debug)] +enum LoadAvg { + One, + Five, + Fifteen, +} + +fn get_load_average( + sys: &RefCell, + avg: LoadAvg, + upd: &Updater, +) -> f64 { + upd.refresh_cpu(sys); + let load_avg = sys.borrow().load_average(); + match avg { + LoadAvg::One => load_avg.one, + LoadAvg::Five => load_avg.five, + LoadAvg::Fifteen => load_avg.fifteen, + } } impl BarModuleFn for BarModuleSysInfo { fn init() -> Box { Box::new(BarModuleSysInfo { instance: "0".to_string(), - system: RefCell::new(sysinfo::System::new_all()), + system: RefCell::new(si::System::new_all()), }) } @@ -43,15 +88,19 @@ impl BarModuleFn for BarModuleSysInfo { } fn build(&self) -> s::Block { - let x = self.system.borrow().load_average().one.to_string(); - self.system.borrow_mut().refresh_specifics( - sysinfo::RefreshKind::new().with_cpu().with_memory(), - ); - + let fmt = "⚡ {load_avg_1} / {load_avg_5} / {load_avg_15}"; + let updater = Updater::new(); s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), - full_text: x, + full_text: fmt_replace!(fmt, true, { + "load_avg_1" => get_load_average(&self.system, + LoadAvg::One, &updater), + "load_avg_5" => get_load_average(&self.system, + LoadAvg::Five, &updater), + "load_avg_15" => get_load_average(&self.system, + LoadAvg::Fifteen, &updater), + }), align: Some(s::Align::Right), markup: Some(s::Markup::Pango), short_text: None, diff --git a/src/fmt_replace.rs b/src/fmt_replace.rs index 971fc01..2c5109b 100644 --- a/src/fmt_replace.rs +++ b/src/fmt_replace.rs @@ -60,6 +60,8 @@ macro_rules! fmt_replace { }; } +pub(crate) use fmt_replace; + #[test] fn foo() { let foo = "{a}, {b}"; diff --git a/src/lib.rs b/src/lib.rs index 53ddef0..3e0004b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,6 @@ pub mod client; pub mod cmds; pub mod config; pub mod demon; -#[macro_use] pub mod fmt_replace; pub mod ipc; pub mod layout; diff --git a/src/rtfmt.rs b/src/rtfmt.rs index 989bf1b..2d585b7 100644 --- a/src/rtfmt.rs +++ b/src/rtfmt.rs @@ -24,6 +24,7 @@ use std::fmt; pub enum FmtArg { I64(i64), + F64(f64), String(String), } @@ -33,6 +34,12 @@ impl From for FmtArg { } } +impl From for FmtArg { + fn from(x: f64) -> FmtArg { + FmtArg::F64(x) + } +} + impl From<&str> for FmtArg { fn from(x: &str) -> FmtArg { FmtArg::String(x.to_string()) @@ -50,6 +57,7 @@ impl ToString for FmtArg { match self { FmtArg::String(x) => x.clone(), FmtArg::I64(x) => x.to_string(), + FmtArg::F64(x) => x.to_string(), } } } @@ -63,6 +71,7 @@ impl FormatArgument for FmtArg { match self { Self::String(val) => fmt::Display::fmt(&val, f), Self::I64(val) => fmt::Display::fmt(&val, f), + Self::F64(val) => fmt::Display::fmt(&val, f), } } diff --git a/src/tree.rs b/src/tree.rs index 2927ded..99dd7b9 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -16,6 +16,7 @@ //! Convenience data structures built from the IPC structs. use crate::config; +use crate::fmt_replace::fmt_replace; use crate::ipc; use crate::ipc::NodeMethods; use crate::util;