Skip to content

Commit c488add

Browse files
unhappychoiceclaude
andcommitted
feat(system): emit PixelArt sprites for monitor_cpu
Extends `system_monitor_cpu` with `Shape::PixelArt`. The CPU usage ratio routes through `sprites::cpu_sprite()`, picking one of five buckets (sleepy → relaxed → normal → busy → overheating) and labelling it with the formatted percent (`"42%"`). Refs #261. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 6cfbcd6 commit c488add

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎src/fetcher/system/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ mod tests {
656656
assert_realtime_contract(
657657
&SystemMonitorCpu::default(),
658658
"system_monitor_cpu",
659-
&[Shape::Ratio, Shape::Text],
659+
&[Shape::Ratio, Shape::Text, Shape::PixelArt],
660660
Shape::Ratio,
661661
Shape::Entries,
662662
0,

‎src/fetcher/system/monitor_cpu.rs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::samples;
1010

1111
use super::super::{FetchContext, RealtimeFetcher, Safety};
1212
use super::payload;
13+
use super::sprites;
1314

1415
pub struct SystemMonitorCpu {
1516
state: Mutex<System>,
@@ -42,12 +43,13 @@ impl RealtimeFetcher for SystemMonitorCpu {
4243
"Aggregated CPU usage across all cores, sampled every frame. Pair with a gauge renderer for a live meter or use the `Text` shape for a plain percentage."
4344
}
4445
fn shapes(&self) -> &[Shape] {
45-
&[Shape::Ratio, Shape::Text]
46+
&[Shape::Ratio, Shape::Text, Shape::PixelArt]
4647
}
4748
fn sample_body(&self, shape: Shape) -> Option<Body> {
4849
Some(match shape {
4950
Shape::Ratio => samples::ratio(0.42, "cpu"),
5051
Shape::Text => samples::text("42%"),
52+
Shape::PixelArt => Body::PixelArt(sprites::cpu_sprite(0.42, "42%")),
5153
_ => return None,
5254
})
5355
}
@@ -59,6 +61,7 @@ impl RealtimeFetcher for SystemMonitorCpu {
5961
let label = format!("{pct:.0}%");
6062
match ctx.shape.unwrap_or(Shape::Ratio) {
6163
Shape::Text => payload(Body::Text(TextData { value: label })),
64+
Shape::PixelArt => payload(Body::PixelArt(sprites::cpu_sprite(ratio, &label))),
6265
_ => payload(Body::Ratio(RatioData {
6366
value: ratio,
6467
label: Some(label),
@@ -84,4 +87,34 @@ mod tests {
8487
let p = SystemMonitorCpu::new().compute(&ctx_with_shape(Some(Shape::Text)));
8588
assert!(matches!(p.body, Body::Text(_)));
8689
}
90+
91+
#[test]
92+
fn cpu_load_emits_pixel_art_when_requested() {
93+
let p = SystemMonitorCpu::new().compute(&ctx_with_shape(Some(Shape::PixelArt)));
94+
let Body::PixelArt(d) = p.body else {
95+
panic!("expected PixelArt body");
96+
};
97+
assert_eq!(d.pixels.len(), 16);
98+
let label = d.label.unwrap();
99+
assert!(label.ends_with('%'), "label = {label}");
100+
}
101+
102+
#[test]
103+
fn sample_body_covers_every_supported_shape() {
104+
let fetcher = SystemMonitorCpu::new();
105+
assert!(matches!(
106+
fetcher.sample_body(Shape::Ratio),
107+
Some(Body::Ratio(_))
108+
));
109+
assert!(matches!(
110+
fetcher.sample_body(Shape::Text),
111+
Some(Body::Text(_))
112+
));
113+
let pixel = fetcher.sample_body(Shape::PixelArt).unwrap();
114+
let Body::PixelArt(d) = pixel else {
115+
panic!("expected PixelArt sample body");
116+
};
117+
assert_eq!(d.label.as_deref(), Some("42%"));
118+
assert!(fetcher.sample_body(Shape::Entries).is_none());
119+
}
87120
}

0 commit comments

Comments
 (0)