From bf9337571a47ca962ade02dec447728ac49a8f73 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 3 Apr 2022 10:56:56 +0200 Subject: [PATCH] cpu / mem % for sysinfo module --- src/bar/module/date.rs | 4 ++-- src/bar/module/sysinfo.rs | 18 ++++++++++++++++-- src/bar/module/window.rs | 6 +++--- src/rtfmt.rs | 9 +++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/bar/module/date.rs b/src/bar/module/date.rs index 24f7f3e..ec92625 100644 --- a/src/bar/module/date.rs +++ b/src/bar/module/date.rs @@ -38,7 +38,7 @@ impl BarModuleFn for BarModuleDate { } fn build(&self) -> s::Block { - let d = chrono::Local::now().format("%F %X").to_string(); + let d = chrono::Local::now().format("⏰ %F %X").to_string(); s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), @@ -55,7 +55,7 @@ impl BarModuleFn for BarModuleDate { border_right: None, min_width: None, urgent: None, - separator: None, + separator: Some(true), separator_block_width: None, } } diff --git a/src/bar/module/sysinfo.rs b/src/bar/module/sysinfo.rs index 95a62c9..a0404b5 100644 --- a/src/bar/module/sysinfo.rs +++ b/src/bar/module/sysinfo.rs @@ -21,6 +21,7 @@ use std::cell::RefCell; use std::sync::Once; use swaybar_types as s; use sysinfo as si; +use sysinfo::ProcessorExt; use sysinfo::SystemExt; pub struct BarModuleSysInfo { @@ -50,6 +51,17 @@ impl Updater { } } +fn get_cpu_usage(sys: &RefCell, upd: &Updater) -> f32 { + upd.refresh_cpu(sys); + sys.borrow().global_processor_info().cpu_usage() +} + +fn get_memory_usage(sys: &RefCell, upd: &Updater) -> f64 { + upd.refresh_memory(sys); + let sys = sys.borrow(); + sys.used_memory() as f64 * 100 as f64 / sys.total_memory() as f64 +} + #[derive(Debug)] enum LoadAvg { One, @@ -88,12 +100,14 @@ impl BarModuleFn for BarModuleSysInfo { } fn build(&self) -> s::Block { - let fmt = "⚡ {load_avg_1} / {load_avg_5} / {load_avg_15}"; + let fmt = "💻 CPU: {cpu_usage:{:4.1}}% Mem: {mem_usage:{:4.1}}% Load: {load_avg_1:{:4.2}} / {load_avg_5:{:4.2}} / {load_avg_15:{:4.2}}"; let updater = Updater::new(); s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), full_text: fmt_replace!(fmt, true, { + "cpu_usage" => get_cpu_usage(&self.system, &updater), + "mem_usage" => get_memory_usage(&self.system, &updater), "load_avg_1" => get_load_average(&self.system, LoadAvg::One, &updater), "load_avg_5" => get_load_average(&self.system, @@ -113,7 +127,7 @@ impl BarModuleFn for BarModuleSysInfo { border_right: None, min_width: None, urgent: None, - separator: None, + separator: Some(true), separator_block_width: None, } } diff --git a/src/bar/module/window.rs b/src/bar/module/window.rs index 6075e57..83ac212 100644 --- a/src/bar/module/window.rs +++ b/src/bar/module/window.rs @@ -47,8 +47,8 @@ impl BarModuleFn for BarModuleWindow { s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), - full_text: title.to_string() + " — " + app_name, - align: Some(s::Align::Right), + full_text: "🪟 ".to_string() + title + " — " + app_name, + align: Some(s::Align::Left), markup: Some(s::Markup::Pango), short_text: None, color: None, @@ -60,7 +60,7 @@ impl BarModuleFn for BarModuleWindow { border_right: None, min_width: None, urgent: None, - separator: None, + separator: Some(true), separator_block_width: None, } } diff --git a/src/rtfmt.rs b/src/rtfmt.rs index 2d585b7..60e6134 100644 --- a/src/rtfmt.rs +++ b/src/rtfmt.rs @@ -25,6 +25,7 @@ use std::fmt; pub enum FmtArg { I64(i64), F64(f64), + F32(f32), String(String), } @@ -40,6 +41,12 @@ impl From for FmtArg { } } +impl From for FmtArg { + fn from(x: f32) -> FmtArg { + FmtArg::F32(x) + } +} + impl From<&str> for FmtArg { fn from(x: &str) -> FmtArg { FmtArg::String(x.to_string()) @@ -58,6 +65,7 @@ impl ToString for FmtArg { FmtArg::String(x) => x.clone(), FmtArg::I64(x) => x.to_string(), FmtArg::F64(x) => x.to_string(), + FmtArg::F32(x) => x.to_string(), } } } @@ -72,6 +80,7 @@ impl FormatArgument for FmtArg { Self::String(val) => fmt::Display::fmt(&val, f), Self::I64(val) => fmt::Display::fmt(&val, f), Self::F64(val) => fmt::Display::fmt(&val, f), + Self::F32(val) => fmt::Display::fmt(&val, f), } }