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::*;
|
2022-10-15 16:27:25 +01:00
|
|
|
use gtk::Label;
|
2022-08-14 14:30:13 +01:00
|
|
|
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();
|
|
|
|
|
2022-10-16 00:58:47 +01:00
|
|
|
while let Some(refresh) = refresh_rx.recv().await {
|
|
|
|
match refresh {
|
|
|
|
RefreshType::Memory => refresh_memory_tokens(&mut format_info, &mut sys),
|
|
|
|
RefreshType::Cpu => refresh_cpu_tokens(&mut format_info, &mut sys),
|
|
|
|
RefreshType::Temps => refresh_temp_tokens(&mut format_info, &mut sys),
|
|
|
|
RefreshType::Disks => refresh_disk_tokens(&mut format_info, &mut sys),
|
|
|
|
RefreshType::Network => {
|
|
|
|
refresh_network_tokens(&mut format_info, &mut sys, interval.networks())
|
|
|
|
}
|
|
|
|
RefreshType::System => refresh_system_tokens(&mut format_info, &mut sys),
|
|
|
|
};
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-10-16 00:58:47 +01:00
|
|
|
tx.send(ModuleUpdateEvent::Update(format_info.clone()))
|
2022-09-25 22:49:00 +01:00
|
|
|
.await
|
2022-08-21 23:36:07 +01:00
|
|
|
.expect("Failed to send system info map");
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-25 22:49:00 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2022-10-15 16:27:25 +01:00
|
|
|
info: &ModuleInfo,
|
2022-09-25 22:49:00 +01:00
|
|
|
) -> Result<ModuleWidget<gtk::Box>> {
|
|
|
|
let re = Regex::new(r"\{([\w-]+)}")?;
|
|
|
|
|
2022-10-15 16:27:25 +01:00
|
|
|
let container = gtk::Box::new(info.bar_position.get_orientation(), 10);
|
2022-09-25 22:49:00 +01:00
|
|
|
|
|
|
|
let mut labels = Vec::new();
|
|
|
|
|
|
|
|
for format in &self.format {
|
|
|
|
let label = Label::builder().label(format).name("item").build();
|
2022-10-15 16:27:25 +01:00
|
|
|
label.set_angle(info.bar_position.get_angle());
|
2022-09-25 22:49:00 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-10-16 00:58:47 +01:00
|
|
|
|
|
|
|
fn refresh_memory_tokens(format_info: &mut HashMap<String, String>, sys: &mut System) {
|
|
|
|
sys.refresh_memory();
|
|
|
|
|
|
|
|
let total_memory = sys.total_memory();
|
|
|
|
let available_memory = sys.available_memory();
|
|
|
|
|
|
|
|
let actual_used_memory = total_memory - available_memory;
|
|
|
|
let memory_percent = actual_used_memory as f64 / total_memory as f64 * 100.0;
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
String::from("memory-free"),
|
|
|
|
(bytes_to_gigabytes(available_memory)).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("memory-used"),
|
|
|
|
(bytes_to_gigabytes(actual_used_memory)).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("memory-total"),
|
|
|
|
(bytes_to_gigabytes(total_memory)).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("memory-percent"),
|
|
|
|
format!("{:0>2.0}", memory_percent),
|
|
|
|
);
|
|
|
|
|
|
|
|
let used_swap = sys.used_swap();
|
|
|
|
let total_swap = sys.total_swap();
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
String::from("swap-free"),
|
|
|
|
(bytes_to_gigabytes(sys.free_swap())).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("swap-used"),
|
|
|
|
(bytes_to_gigabytes(used_swap)).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("swap-total"),
|
|
|
|
(bytes_to_gigabytes(total_swap)).to_string(),
|
|
|
|
);
|
|
|
|
format_info.insert(
|
|
|
|
String::from("swap-percent"),
|
|
|
|
format!("{:0>2.0}", used_swap as f64 / total_swap as f64 * 100.0),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_cpu_tokens(format_info: &mut HashMap<String, String>, sys: &mut System) {
|
|
|
|
sys.refresh_cpu();
|
|
|
|
|
|
|
|
let cpu_info = sys.global_cpu_info();
|
|
|
|
let cpu_percent = cpu_info.cpu_usage();
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
String::from("cpu-percent"),
|
|
|
|
format!("{:0>2.0}", cpu_percent),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_temp_tokens(format_info: &mut HashMap<String, String>, sys: &mut System) {
|
|
|
|
sys.refresh_components();
|
|
|
|
|
|
|
|
let components = sys.components();
|
|
|
|
for component in components {
|
|
|
|
let key = component.label().replace(' ', "-");
|
|
|
|
let temp = component.temperature();
|
|
|
|
|
|
|
|
format_info.insert(format!("temp-c:{key}"), format!("{temp:.0}"));
|
|
|
|
format_info.insert(format!("temp-f:{key}"), format!("{:.0}", c_to_f(temp)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_disk_tokens(format_info: &mut HashMap<String, String>, sys: &mut System) {
|
|
|
|
sys.refresh_disks();
|
|
|
|
|
|
|
|
for disk in sys.disks() {
|
|
|
|
// replace braces to avoid conflict with regex
|
|
|
|
let key = disk
|
|
|
|
.mount_point()
|
|
|
|
.to_str()
|
|
|
|
.map(|s| s.replace('{', "").replace('}', ""));
|
|
|
|
|
|
|
|
if let Some(key) = key {
|
|
|
|
let total = disk.total_space();
|
|
|
|
let available = disk.available_space();
|
|
|
|
let used = total - available;
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
format!("disk-free:{key}"),
|
|
|
|
bytes_to_gigabytes(available).to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
format!("disk-used:{key}"),
|
|
|
|
bytes_to_gigabytes(used).to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
format!("disk-total:{key}"),
|
|
|
|
bytes_to_gigabytes(total).to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
format!("disk-percent:{key}"),
|
|
|
|
format!("{:0>2.0}", used as f64 / total as f64 * 100.0),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_network_tokens(format_info: &mut HashMap<String, String>, sys: &mut System, interval: u64) {
|
|
|
|
sys.refresh_networks();
|
|
|
|
|
|
|
|
for (iface, network) in sys.networks() {
|
|
|
|
format_info.insert(
|
|
|
|
format!("net-down:{iface}"),
|
|
|
|
format!("{:0>2.0}", bytes_to_megabits(network.received()) / interval),
|
|
|
|
);
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
format!("net-up:{iface}"),
|
|
|
|
format!(
|
|
|
|
"{:0>2.0}",
|
|
|
|
bytes_to_megabits(network.transmitted()) / interval
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh_system_tokens(format_info: &mut HashMap<String, String>, sys: &System) {
|
|
|
|
// no refresh required for these tokens
|
|
|
|
|
|
|
|
let load_average = sys.load_average();
|
|
|
|
format_info.insert(String::from("load-average:1"), load_average.one.to_string());
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
String::from("load-average:5"),
|
|
|
|
load_average.five.to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
format_info.insert(
|
|
|
|
String::from("load-average:15"),
|
|
|
|
load_average.fifteen.to_string(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let uptime = Duration::from_secs(sys.uptime()).as_secs();
|
|
|
|
let hours = uptime / 3600;
|
|
|
|
format_info.insert(
|
|
|
|
String::from("uptime"),
|
|
|
|
format!("{:0>2}:{:0>2}", hours, (uptime % 3600) / 60),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts celsius to fahrenheit.
|
|
|
|
fn c_to_f(c: f32) -> f32 {
|
|
|
|
c * 9.0 / 5.0 + 32.0
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn bytes_to_gigabytes(b: u64) -> u64 {
|
|
|
|
const BYTES_IN_GIGABYTE: u64 = 1_000_000_000;
|
|
|
|
b / BYTES_IN_GIGABYTE
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn bytes_to_megabits(b: u64) -> u64 {
|
|
|
|
const BYTES_IN_MEGABIT: u64 = 125_000;
|
|
|
|
b / BYTES_IN_MEGABIT
|
|
|
|
}
|