cpu / mem % for sysinfo module

main
Tassilo Horn 3 years ago
parent 9dd0a1aa10
commit bf9337571a
  1. 4
      src/bar/module/date.rs
  2. 18
      src/bar/module/sysinfo.rs
  3. 6
      src/bar/module/window.rs
  4. 9
      src/rtfmt.rs

@ -38,7 +38,7 @@ impl BarModuleFn for BarModuleDate {
} }
fn build(&self) -> s::Block { 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 { s::Block {
name: Some(Self::name()), name: Some(Self::name()),
instance: Some(self.instance.clone()), instance: Some(self.instance.clone()),
@ -55,7 +55,7 @@ impl BarModuleFn for BarModuleDate {
border_right: None, border_right: None,
min_width: None, min_width: None,
urgent: None, urgent: None,
separator: None, separator: Some(true),
separator_block_width: None, separator_block_width: None,
} }
} }

@ -21,6 +21,7 @@ use std::cell::RefCell;
use std::sync::Once; use std::sync::Once;
use swaybar_types as s; use swaybar_types as s;
use sysinfo as si; use sysinfo as si;
use sysinfo::ProcessorExt;
use sysinfo::SystemExt; use sysinfo::SystemExt;
pub struct BarModuleSysInfo { pub struct BarModuleSysInfo {
@ -50,6 +51,17 @@ impl Updater {
} }
} }
fn get_cpu_usage(sys: &RefCell<si::System>, upd: &Updater) -> f32 {
upd.refresh_cpu(sys);
sys.borrow().global_processor_info().cpu_usage()
}
fn get_memory_usage(sys: &RefCell<si::System>, 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)] #[derive(Debug)]
enum LoadAvg { enum LoadAvg {
One, One,
@ -88,12 +100,14 @@ impl BarModuleFn for BarModuleSysInfo {
} }
fn build(&self) -> s::Block { 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(); let updater = Updater::new();
s::Block { s::Block {
name: Some(Self::name()), name: Some(Self::name()),
instance: Some(self.instance.clone()), instance: Some(self.instance.clone()),
full_text: fmt_replace!(fmt, true, { 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, "load_avg_1" => get_load_average(&self.system,
LoadAvg::One, &updater), LoadAvg::One, &updater),
"load_avg_5" => get_load_average(&self.system, "load_avg_5" => get_load_average(&self.system,
@ -113,7 +127,7 @@ impl BarModuleFn for BarModuleSysInfo {
border_right: None, border_right: None,
min_width: None, min_width: None,
urgent: None, urgent: None,
separator: None, separator: Some(true),
separator_block_width: None, separator_block_width: None,
} }
} }

@ -47,8 +47,8 @@ impl BarModuleFn for BarModuleWindow {
s::Block { s::Block {
name: Some(Self::name()), name: Some(Self::name()),
instance: Some(self.instance.clone()), instance: Some(self.instance.clone()),
full_text: title.to_string() + " — " + app_name, full_text: "🪟 ".to_string() + title + " — " + app_name,
align: Some(s::Align::Right), align: Some(s::Align::Left),
markup: Some(s::Markup::Pango), markup: Some(s::Markup::Pango),
short_text: None, short_text: None,
color: None, color: None,
@ -60,7 +60,7 @@ impl BarModuleFn for BarModuleWindow {
border_right: None, border_right: None,
min_width: None, min_width: None,
urgent: None, urgent: None,
separator: None, separator: Some(true),
separator_block_width: None, separator_block_width: None,
} }
} }

@ -25,6 +25,7 @@ use std::fmt;
pub enum FmtArg { pub enum FmtArg {
I64(i64), I64(i64),
F64(f64), F64(f64),
F32(f32),
String(String), String(String),
} }
@ -40,6 +41,12 @@ impl From<f64> for FmtArg {
} }
} }
impl From<f32> for FmtArg {
fn from(x: f32) -> FmtArg {
FmtArg::F32(x)
}
}
impl From<&str> for FmtArg { impl From<&str> for FmtArg {
fn from(x: &str) -> FmtArg { fn from(x: &str) -> FmtArg {
FmtArg::String(x.to_string()) FmtArg::String(x.to_string())
@ -58,6 +65,7 @@ impl ToString for FmtArg {
FmtArg::String(x) => x.clone(), FmtArg::String(x) => x.clone(),
FmtArg::I64(x) => x.to_string(), FmtArg::I64(x) => x.to_string(),
FmtArg::F64(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::String(val) => fmt::Display::fmt(&val, f),
Self::I64(val) => fmt::Display::fmt(&val, f), Self::I64(val) => fmt::Display::fmt(&val, f),
Self::F64(val) => fmt::Display::fmt(&val, f), Self::F64(val) => fmt::Display::fmt(&val, f),
Self::F32(val) => fmt::Display::fmt(&val, f),
} }
} }

Loading…
Cancel
Save