2022-09-25 22:49:00 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
2022-08-21 23:36:07 +01:00
|
|
|
use color_eyre::Result;
|
2022-08-14 14:30:13 +01:00
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::{Label, Orientation};
|
|
|
|
use regex::{Captures, Regex};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use sysinfo::{CpuExt, System, SystemExt};
|
|
|
|
use tokio::spawn;
|
2022-09-25 22:49:00 +01:00
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
2022-08-14 14:30:13 +01:00
|
|
|
use tokio::time::sleep;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct SysInfoModule {
|
2022-08-28 16:57:41 +01:00
|
|
|
/// List of formatting strings.
|
2022-08-14 14:30:13 +01:00
|
|
|
format: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<gtk::Box> for SysInfoModule {
|
2022-09-25 22:49:00 +01:00
|
|
|
type SendMessage = HashMap<String, String>;
|
|
|
|
type ReceiveMessage = ();
|
|
|
|
|
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
|
|
|
_rx: Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
2022-08-14 14:30:13 +01:00
|
|
|
spawn(async move {
|
|
|
|
let mut sys = System::new_all();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
sys.refresh_all();
|
|
|
|
|
|
|
|
let mut format_info = HashMap::new();
|
|
|
|
|
|
|
|
let actual_used_memory = sys.total_memory() - sys.available_memory();
|
|
|
|
let memory_percent = actual_used_memory as f64 / sys.total_memory() as f64 * 100.0;
|
|
|
|
|
|
|
|
let cpu_percent = sys.global_cpu_info().cpu_usage();
|
|
|
|
|
|
|
|
// TODO: Add remaining format info
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
format_info.insert(
|
|
|
|
String::from("memory-percent"),
|
|
|
|
format!("{:0>2.0}", memory_percent),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("cpu-percent"),
|
|
|
|
format!("{:0>2.0}", cpu_percent),
|
|
|
|
);
|
|
|
|
|
|
|
|
tx.send(ModuleUpdateEvent::Update(format_info))
|
|
|
|
.await
|
2022-08-21 23:36:07 +01:00
|
|
|
.expect("Failed to send system info map");
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
sleep(tokio::time::Duration::from_secs(1)).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
) -> Result<ModuleWidget<gtk::Box>> {
|
|
|
|
let re = Regex::new(r"\{([\w-]+)}")?;
|
|
|
|
|
|
|
|
let container = gtk::Box::new(Orientation::Horizontal, 10);
|
|
|
|
|
|
|
|
let mut labels = Vec::new();
|
|
|
|
|
|
|
|
for format in &self.format {
|
|
|
|
let label = Label::builder().label(format).name("item").build();
|
|
|
|
container.add(&label);
|
|
|
|
labels.push(label);
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:30:13 +01:00
|
|
|
{
|
|
|
|
let formats = self.format;
|
2022-09-25 22:49:00 +01:00
|
|
|
context.widget_rx.attach(None, move |info| {
|
2022-08-14 14:30:13 +01:00
|
|
|
for (format, label) in formats.iter().zip(labels.clone()) {
|
|
|
|
let format_compiled = re.replace(format, |caps: &Captures| {
|
|
|
|
info.get(&caps[1])
|
|
|
|
.unwrap_or(&caps[0].to_string())
|
|
|
|
.to_string()
|
|
|
|
});
|
|
|
|
|
|
|
|
label.set_text(format_compiled.as_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
Continue(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(ModuleWidget {
|
|
|
|
widget: container,
|
|
|
|
popup: None,
|
|
|
|
})
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
}
|