More on the sysinfo bar module

main
Tassilo Horn 3 years ago
parent 37c85880e7
commit 9dd0a1aa10
  1. 67
      src/bar/module/sysinfo.rs
  2. 2
      src/fmt_replace.rs
  3. 1
      src/lib.rs
  4. 9
      src/rtfmt.rs
  5. 1
      src/tree.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<sysinfo::System>,
system: RefCell<si::System>,
}
struct Updater {
cpu: Once,
memory: Once,
}
impl Updater {
fn new() -> Updater {
Updater {
cpu: Once::new(),
memory: Once::new(),
}
}
fn refresh_cpu(&self, sys: &RefCell<si::System>) {
self.cpu.call_once(|| sys.borrow_mut().refresh_cpu());
}
fn refresh_memory(&self, sys: &RefCell<si::System>) {
self.memory.call_once(|| sys.borrow_mut().refresh_memory());
}
}
#[derive(Debug)]
enum LoadAvg {
One,
Five,
Fifteen,
}
fn get_load_average(
sys: &RefCell<si::System>,
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<dyn BarModuleFn> {
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,

@ -60,6 +60,8 @@ macro_rules! fmt_replace {
};
}
pub(crate) use fmt_replace;
#[test]
fn foo() {
let foo = "{a}, {b}";

@ -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;

@ -24,6 +24,7 @@ use std::fmt;
pub enum FmtArg {
I64(i64),
F64(f64),
String(String),
}
@ -33,6 +34,12 @@ impl From<i64> for FmtArg {
}
}
impl From<f64> 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),
}
}

@ -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;

Loading…
Cancel
Save