feat: memory and swap graphs

the values are in the SysinfoSnapshot verbatim,
in case they might be displayed in another way (textually)
This commit is contained in:
electria 2026-08-16 08:41:53 -07:00
commit d068d110e2
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 45 additions and 10 deletions

View file

@ -16,6 +16,8 @@ enum Message {
struct State {
total_cpu_graph: Graph,
cpu_graphs: Vec<Graph>,
memory_graph: Graph,
swap_graph: Graph,
}
impl State {
@ -26,6 +28,7 @@ impl State {
stream::channel(1, async |mut sender| {
let mut sys = sysinfo::System::new();
let refreshes = sysinfo::RefreshKind::nothing()
.with_memory(sysinfo::MemoryRefreshKind::everything())
.with_cpu(sysinfo::CpuRefreshKind::nothing().with_cpu_usage());
sys.refresh_specifics(refreshes);
@ -47,6 +50,14 @@ impl State {
self.total_cpu_graph.update(snapshot.cpu_total);
self.total_cpu_graph.cache.clear();
self.memory_graph
.update(snapshot.memory as f32 / snapshot.memory_total as f32 * 100.);
self.memory_graph.cache.clear();
self.swap_graph
.update(snapshot.swap as f32 / snapshot.swap_total as f32 * 100.);
self.swap_graph.cache.clear();
if self.cpu_graphs.len() != snapshot.cpus.len() {
self.cpu_graphs = snapshot
.cpus
@ -66,18 +77,32 @@ impl State {
Task::none()
}
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
widget::row([
widget::Canvas::new(&self.total_cpu_graph)
.width(Length::Fill)
widget::column([
widget::row([
widget::Canvas::new(&self.total_cpu_graph)
.width(Length::Fill)
.height(Length::Fill)
.into(),
widget::grid(
self.cpu_graphs
.iter()
.map(|graph| widget::Canvas::new(graph).into()),
)
.columns(2)
.height(Length::Fill)
.into(),
widget::grid(
self.cpu_graphs
.iter()
.map(|graph| widget::Canvas::new(graph).into()),
)
.columns(2)
.height(Length::Fill)
])
.into(),
widget::row([
widget::canvas(&self.memory_graph)
.width(Length::Fill)
.height(Length::Fill)
.into(),
widget::canvas(&self.swap_graph)
.width(Length::Fill)
.height(Length::Fill)
.into(),
])
.into(),
])
.into()

View file

@ -2,6 +2,11 @@
pub struct SysinfoSnapshot {
pub cpu_total: f32,
pub cpus: Vec<f32>,
pub memory: u64,
pub memory_total: u64,
pub swap: u64,
pub swap_total: u64,
}
impl From<&sysinfo::System> for SysinfoSnapshot {
@ -9,6 +14,11 @@ impl From<&sysinfo::System> for SysinfoSnapshot {
Self {
cpu_total: value.global_cpu_usage(),
cpus: value.cpus().iter().map(|cpu| cpu.cpu_usage()).collect(),
memory: value.used_memory(),
memory_total: value.total_memory(),
swap: value.used_swap(),
swap_total: value.total_swap(),
}
}
}