splashboard 1.2.0

A customizable terminal splash screen with plugin-based data sources
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
//! Cross-platform system fetchers backed by `sysinfo`.
//!
//! All are `Safety::Safe` — local kernel counters only, no network or exec. Realtime fetchers
//! cache a `Mutex<System>` and refresh only the fields they need per frame, so the `<1ms
//! infallible` contract holds even as many widgets sample the same source.

use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde::Deserialize;
use sysinfo::{Disks, ProcessesToUpdate, System};

use crate::options::OptionSchema;
use crate::payload::{
    BadgeData, Bar, BarsData, Body, EntriesData, Entry, Payload, RatioData, Status, TextBlockData,
    TextData,
};
use crate::render::Shape;
use crate::samples;

use super::{FetchContext, FetchError, Fetcher, RealtimeFetcher, Safety};

pub fn realtime_fetchers() -> Vec<Arc<dyn RealtimeFetcher>> {
    vec![
        Arc::new(SystemFetcher::new()),
        Arc::new(CpuLoadFetcher::new()),
        Arc::new(MemoryFetcher::new()),
        Arc::new(UptimeFetcher),
        Arc::new(LoadAverageFetcher),
        Arc::new(ProcessTopFetcher::new()),
        Arc::new(BatteryFetcher::new()),
    ]
}

pub fn cached_fetchers() -> Vec<Arc<dyn Fetcher>> {
    vec![Arc::new(DiskFetcher)]
}

const SYSTEM_OPTION_SCHEMAS: &[OptionSchema] = &[OptionSchema {
    name: "kind",
    type_hint: "\"terminal\" | \"os\" | \"os_version\" | \"hostname\" | \"shell\" | \"arch\"",
    required: false,
    default: Some("\"terminal\""),
    description: "Selects the single value emitted by the `Text` shape. Ignored by `Entries` / `TextBlock` shapes, which always return the full rollup.",
}];

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SystemOptions {
    #[serde(default)]
    pub kind: Option<SystemKind>,
}

#[derive(Debug, Default, Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SystemKind {
    #[default]
    Terminal,
    Os,
    OsVersion,
    Hostname,
    Shell,
    Arch,
}

/// `os / host / uptime / load / cpu / memory` rollup. `Entries` by default; `TextBlock`
/// collapses each row to `"key: value"`; `Text` emits a single identity field selected by the
/// `kind` option (terminal name, OS label, hostname, shell, arch) for hero / attribution lines.
pub struct SystemFetcher {
    state: Mutex<System>,
    os: String,
    host: String,
}

impl SystemFetcher {
    pub fn new() -> Self {
        let mut sys = System::new();
        sys.refresh_cpu_usage();
        sys.refresh_memory();
        Self {
            state: Mutex::new(sys),
            os: os_label(),
            host: System::host_name().unwrap_or_else(|| "unknown".into()),
        }
    }
}

impl Default for SystemFetcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RealtimeFetcher for SystemFetcher {
    fn name(&self) -> &str {
        "system"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Host identity rollup combining OS, hostname, uptime, load, CPU, and memory into one block. Use the `kind` option on the `Text` shape to extract a single field (terminal, OS, hostname, shell, arch) for hero or attribution lines."
    }
    fn shapes(&self) -> &[Shape] {
        // Listed specific-to-broad so multi-shape renderers (text_plain, animated_postfx)
        // pick `Text` by default — that's the variant where `kind = "terminal" | "os" | …`
        // takes effect, which is what hero / attribution widgets almost always want. The
        // full rollup still lands via `render = "grid_table"` (Entries-only renderer).
        &[Shape::Text, Shape::TextBlock, Shape::Entries]
    }
    fn default_shape(&self) -> Shape {
        // Preserve "no render spec = full rollup". Widgets that omit `render = ...` pick up
        // the Entries view (CPU / memory / uptime / …); the reordered `shapes()` only
        // affects intersection with multi-shape renderers.
        Shape::Entries
    }
    fn option_schemas(&self) -> &[OptionSchema] {
        SYSTEM_OPTION_SCHEMAS
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Entries => samples::entries(&[
                ("os", "linux"),
                ("host", "dev"),
                ("uptime", "3d 4h"),
                ("load", "0.42"),
                ("cpu", "18%"),
                ("memory", "67%"),
            ]),
            Shape::TextBlock => samples::text_block(&[
                "os: linux",
                "host: dev",
                "uptime: 3d 4h",
                "load: 0.42",
                "cpu: 18%",
                "memory: 67%",
            ]),
            Shape::Text => samples::text("iTerm2"),
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        if matches!(ctx.shape, Some(Shape::Text)) {
            return self.compute_text(ctx);
        }
        let mut sys = self.state.lock().expect("system state mutex poisoned");
        sys.refresh_cpu_usage();
        sys.refresh_memory();
        let rows = [
            ("os", self.os.clone()),
            ("host", self.host.clone()),
            ("uptime", format_uptime(System::uptime())),
            ("load", format!("{:.2}", System::load_average().one)),
            ("cpu", format!("{:.0}%", sys.global_cpu_usage())),
            ("memory", format!("{:.0}%", memory_ratio(&sys) * 100.0)),
        ];
        match ctx.shape.unwrap_or(Shape::Entries) {
            Shape::TextBlock => payload(Body::TextBlock(TextBlockData {
                lines: rows.iter().map(|(k, v)| format!("{k}: {v}")).collect(),
            })),
            _ => payload(Body::Entries(EntriesData {
                items: rows.iter().map(|(k, v)| entry(k, v)).collect(),
            })),
        }
    }
}

impl SystemFetcher {
    fn compute_text(&self, ctx: &FetchContext) -> Payload {
        let opts: SystemOptions = match parse_options(ctx.options.as_ref()) {
            Ok(o) => o,
            Err(msg) => return options_placeholder(&msg),
        };
        let value = resolve_system_kind(opts.kind.unwrap_or_default());
        payload(Body::Text(TextData { value }))
    }
}

fn resolve_system_kind(kind: SystemKind) -> String {
    match kind {
        SystemKind::Terminal => detect_terminal(),
        SystemKind::Os => System::name().unwrap_or_else(|| "unknown".into()),
        SystemKind::OsVersion => os_label(),
        SystemKind::Hostname => System::host_name().unwrap_or_else(|| "unknown".into()),
        SystemKind::Shell => detect_shell(),
        SystemKind::Arch => std::env::consts::ARCH.into(),
    }
}

/// Detect the terminal emulator via well-known env vars. Env vars are the only signal available
/// without spawning a subprocess, so this is a best-effort match with a generic fallback.
fn detect_terminal() -> String {
    let env = |k: &str| std::env::var(k).ok();
    if env("WT_SESSION").is_some() {
        return "Windows Terminal".into();
    }
    if env("GHOSTTY_RESOURCES_DIR").is_some() {
        return "Ghostty".into();
    }
    if env("KITTY_WINDOW_ID").is_some() || env("TERM").as_deref() == Some("xterm-kitty") {
        return "Kitty".into();
    }
    if env("ALACRITTY_WINDOW_ID").is_some() || env("ALACRITTY_LOG").is_some() {
        return "Alacritty".into();
    }
    if env("WEZTERM_PANE").is_some() {
        return "WezTerm".into();
    }
    match env("TERM_PROGRAM").as_deref() {
        Some("iTerm.app") => "iTerm2".into(),
        Some("Apple_Terminal") => "Terminal".into(),
        Some("ghostty") => "Ghostty".into(),
        Some("WezTerm") => "WezTerm".into(),
        Some("Hyper") => "Hyper".into(),
        Some("vscode") => "VS Code".into(),
        Some(other) if !other.is_empty() => other.into(),
        _ => "terminal".into(),
    }
}

fn detect_shell() -> String {
    std::env::var("SHELL")
        .ok()
        .and_then(|s| {
            std::path::Path::new(&s)
                .file_name()
                .map(|n| n.to_string_lossy().into_owned())
        })
        .unwrap_or_else(|| "shell".into())
}

fn parse_options<T: serde::de::DeserializeOwned + Default>(
    raw: Option<&toml::Value>,
) -> Result<T, String> {
    match raw {
        None => Ok(T::default()),
        Some(value) => value
            .clone()
            .try_into::<T>()
            .map_err(|e| format!("invalid options: {e}")),
    }
}

fn options_placeholder(msg: &str) -> Payload {
    payload(Body::Text(TextData {
        value: format!("⚠ {msg}"),
    }))
}

/// Aggregated CPU usage across all cores. `Ratio` (0..=1) for gauges, `Text` for plain text.
pub struct CpuLoadFetcher {
    state: Mutex<System>,
}

impl CpuLoadFetcher {
    pub fn new() -> Self {
        let mut sys = System::new();
        sys.refresh_cpu_usage();
        Self {
            state: Mutex::new(sys),
        }
    }
}

impl Default for CpuLoadFetcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RealtimeFetcher for CpuLoadFetcher {
    fn name(&self) -> &str {
        "system_cpu"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "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."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Ratio, Shape::Text]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Ratio => samples::ratio(0.42, "cpu"),
            Shape::Text => samples::text("42%"),
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        let mut sys = self.state.lock().expect("cpu state mutex poisoned");
        sys.refresh_cpu_usage();
        let pct = sys.global_cpu_usage();
        let ratio = (f64::from(pct) / 100.0).clamp(0.0, 1.0);
        let label = format!("{pct:.0}%");
        match ctx.shape.unwrap_or(Shape::Ratio) {
            Shape::Text => payload(Body::Text(TextData { value: label })),
            _ => payload(Body::Ratio(RatioData {
                value: ratio,
                label: Some(label),
                denominator: None,
            })),
        }
    }
}

/// RAM usage. `Ratio` by default, `Text` as `"3.2 GB / 16 GB"`, `Entries` as used/total/free rows.
pub struct MemoryFetcher {
    state: Mutex<System>,
}

impl MemoryFetcher {
    pub fn new() -> Self {
        let mut sys = System::new();
        sys.refresh_memory();
        Self {
            state: Mutex::new(sys),
        }
    }
}

impl Default for MemoryFetcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RealtimeFetcher for MemoryFetcher {
    fn name(&self) -> &str {
        "system_memory"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "RAM utilisation as a used/total ratio. `Text` formats as `\"6.4 GiB / 16 GiB\"` and `Entries` breaks it into used / total / free rows."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Ratio, Shape::Text, Shape::Entries]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Ratio => samples::ratio(0.67, "memory"),
            Shape::Text => samples::text("6.4 GiB / 16 GiB"),
            Shape::Entries => samples::entries(&[
                ("used", "6.4 GiB"),
                ("total", "16 GiB"),
                ("free", "9.6 GiB"),
            ]),
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        let mut sys = self.state.lock().expect("memory state mutex poisoned");
        sys.refresh_memory();
        let total = sys.total_memory();
        let used = sys.used_memory();
        let ratio = ratio_of(used, total);
        let label = format!("{} / {}", format_bytes(used), format_bytes(total));
        match ctx.shape.unwrap_or(Shape::Ratio) {
            Shape::Text => payload(Body::Text(TextData { value: label })),
            Shape::Entries => payload(Body::Entries(EntriesData {
                items: vec![
                    entry("used", &format_bytes(used)),
                    entry("total", &format_bytes(total)),
                    entry("free", &format_bytes(total.saturating_sub(used))),
                ],
            })),
            _ => payload(Body::Ratio(RatioData {
                value: ratio,
                label: Some(label),
                denominator: None,
            })),
        }
    }
}

/// Time since boot as a compact `"3d 4h"` / `"2h 15m"` / `"45m"` string.
pub struct UptimeFetcher;

impl RealtimeFetcher for UptimeFetcher {
    fn name(&self) -> &str {
        "system_uptime"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Time since the host last booted, formatted as a compact `\"3d 4h\"` / `\"2h 15m\"` / `\"45m\"` string."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Text]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        match shape {
            Shape::Text => Some(samples::text("3d 4h 12m")),
            _ => None,
        }
    }
    fn compute(&self, _: &FetchContext) -> Payload {
        payload(Body::Text(TextData {
            value: format_uptime(System::uptime()),
        }))
    }
}

/// 1 / 5 / 15-minute load average. `Text` default; `Entries` splits the three windows.
/// Windows doesn't expose load average — shown as `"n/a (windows)"` there.
pub struct LoadAverageFetcher;

impl RealtimeFetcher for LoadAverageFetcher {
    fn name(&self) -> &str {
        "system_load"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Unix 1 / 5 / 15-minute load averages. `Text` joins the three values on one line; `Entries` splits them into separate rows. Reads as `\"n/a (windows)\"` on Windows, which has no equivalent counter."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Text, Shape::Entries]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Text => samples::text("0.42  0.38  0.31"),
            Shape::Entries => {
                samples::entries(&[("1min", "0.42"), ("5min", "0.38"), ("15min", "0.31")])
            }
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        let la = System::load_average();
        match ctx.shape.unwrap_or(Shape::Text) {
            Shape::Entries => payload(Body::Entries(EntriesData {
                items: vec![
                    entry("1min", &format_load(la.one)),
                    entry("5min", &format_load(la.five)),
                    entry("15min", &format_load(la.fifteen)),
                ],
            })),
            _ => payload(Body::Text(TextData {
                value: load_line(la.one, la.five, la.fifteen),
            })),
        }
    }
}

/// Top N processes by CPU usage. `Entries` default (`"python": "42.1%"`), `TextBlock` collapses
/// to one process per row.
pub struct ProcessTopFetcher {
    state: Mutex<System>,
}

const PROCESS_TOP_COUNT: usize = 5;

impl ProcessTopFetcher {
    pub fn new() -> Self {
        let mut sys = System::new();
        sys.refresh_processes(ProcessesToUpdate::All, true);
        Self {
            state: Mutex::new(sys),
        }
    }
}

impl Default for ProcessTopFetcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RealtimeFetcher for ProcessTopFetcher {
    fn name(&self) -> &str {
        "system_processes"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Top five processes by current CPU usage, refreshed every frame. `Entries` pairs each process name with its percentage; `TextBlock` collapses to one process per line."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Entries, Shape::TextBlock]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Entries => samples::entries(&[
                ("node", "12.4%"),
                ("cargo", "8.1%"),
                ("firefox", "6.3%"),
                ("zsh", "2.1%"),
            ]),
            Shape::TextBlock => samples::text_block(&[
                "node       12.4%",
                "cargo       8.1%",
                "firefox     6.3%",
                "zsh         2.1%",
            ]),
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        let mut sys = self.state.lock().expect("process state mutex poisoned");
        sys.refresh_processes(ProcessesToUpdate::All, true);
        let rows = top_processes(&sys, PROCESS_TOP_COUNT);
        match ctx.shape.unwrap_or(Shape::Entries) {
            Shape::TextBlock => payload(Body::TextBlock(TextBlockData {
                lines: rows.iter().map(|(n, c)| format!("{n}  {c:.1}%")).collect(),
            })),
            _ => payload(Body::Entries(EntriesData {
                items: rows
                    .iter()
                    .map(|(n, c)| entry(n, &format!("{c:.1}%")))
                    .collect(),
            })),
        }
    }
}

/// Disk usage. Cached (mount scan on each refresh is a syscall, not <1ms). Defaults to the
/// largest disk as `Ratio`; `Text` renders `"45% of 500 GB"`; `Bars` lists every mount.
pub struct DiskFetcher;

#[async_trait]
impl Fetcher for DiskFetcher {
    fn name(&self) -> &str {
        "system_disk_usage"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Local disk usage. `Ratio` and `Text` summarise the largest mounted disk (used vs total); `Bars` lists every detected mount with its used bytes."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Ratio, Shape::Text, Shape::Bars]
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Ratio => samples::ratio(0.58, "disk"),
            Shape::Text => samples::text("58% of 400 GB"),
            Shape::Bars => samples::bars(&[("/", 42), ("/home", 110), ("/data", 200)]),
            _ => return None,
        })
    }
    async fn fetch(&self, ctx: &FetchContext) -> Result<Payload, FetchError> {
        let disks = Disks::new_with_refreshed_list();
        Ok(match ctx.shape.unwrap_or(Shape::Ratio) {
            Shape::Bars => payload(Body::Bars(BarsData {
                bars: disk_bars(&disks),
            })),
            Shape::Text => payload(Body::Text(TextData {
                value: primary_disk(&disks)
                    .map(|(t, a)| disk_label(t, a))
                    .unwrap_or_else(|| "no disks detected".into()),
            })),
            _ => primary_disk(&disks)
                .map(|(total, available)| {
                    let used = total.saturating_sub(available);
                    payload(Body::Ratio(RatioData {
                        value: ratio_of(used, total),
                        label: Some(disk_label(total, available)),
                        denominator: None,
                    }))
                })
                .unwrap_or_else(|| {
                    payload(Body::Text(TextData {
                        value: "no disks detected".into(),
                    }))
                }),
        })
    }
}

const BATTERY_OPTION_SCHEMAS: &[OptionSchema] = &[
    OptionSchema {
        name: "kind",
        type_hint: "\"summary\" | \"percent\" | \"status\" | \"time_remaining\"",
        required: false,
        default: Some("\"summary\""),
        description: "Selects the format of the `Text` shape. Ignored by `Ratio` / `Entries`.",
    },
    OptionSchema {
        name: "index",
        type_hint: "integer",
        required: false,
        default: Some("0"),
        description: "Index of the battery to read on multi-battery systems.",
    },
];

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BatteryOptions {
    #[serde(default)]
    pub kind: Option<BatteryTextKind>,
    #[serde(default)]
    pub index: Option<usize>,
}

#[derive(Debug, Default, Clone, Copy, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BatteryTextKind {
    #[default]
    Summary,
    Percent,
    Status,
    TimeRemaining,
}

#[derive(Clone, Copy)]
enum BatteryState {
    Charging,
    Discharging,
    Full,
    Empty,
    Unknown,
}

impl BatteryState {
    fn label(self) -> &'static str {
        match self {
            Self::Charging => "Charging",
            Self::Discharging => "Discharging",
            Self::Full => "Full",
            Self::Empty => "Empty",
            Self::Unknown => "Unknown",
        }
    }
}

struct BatterySnapshot {
    charge: f64,
    state: BatteryState,
    time_remaining_secs: Option<u64>,
    cycle_count: Option<u32>,
    health: Option<f64>,
}

/// Primary (or selected) battery state. `Ratio` pairs with `gauge_battery`; `Text` is a
/// formatted summary (`kind` picks the field); `Entries` rolls up charge / state / time / cycles
/// / health. Hosts without a battery (desktops, servers) render as a "full AC" stand-in so the
/// widget doesn't disappear.
pub struct BatteryFetcher {
    manager: Mutex<Option<starship_battery::Manager>>,
}

impl BatteryFetcher {
    pub fn new() -> Self {
        Self {
            manager: Mutex::new(starship_battery::Manager::new().ok()),
        }
    }
}

impl Default for BatteryFetcher {
    fn default() -> Self {
        Self::new()
    }
}

impl RealtimeFetcher for BatteryFetcher {
    fn name(&self) -> &str {
        "system_battery"
    }
    fn safety(&self) -> Safety {
        Safety::Safe
    }
    fn description(&self) -> &'static str {
        "Charge level and state of the primary (or `index`-selected) battery. `Ratio` drives gauges, `Text` formats a summary line whose field is picked by `kind`, and `Entries` rolls up charge / state / time-left / cycles / health. Hosts without a battery render a steady `\"AC\"` placeholder."
    }
    fn shapes(&self) -> &[Shape] {
        &[Shape::Ratio, Shape::Text, Shape::Entries, Shape::Badge]
    }
    fn option_schemas(&self) -> &[OptionSchema] {
        BATTERY_OPTION_SCHEMAS
    }
    fn sample_body(&self, shape: Shape) -> Option<Body> {
        Some(match shape {
            Shape::Ratio => samples::ratio(0.87, "battery"),
            Shape::Text => samples::text("87% • Charging • 1h 23m"),
            Shape::Entries => samples::entries(&[
                ("charge", "87%"),
                ("state", "Charging"),
                ("time_left", "1h 23m"),
                ("cycles", "284"),
                ("health", "97%"),
            ]),
            Shape::Badge => samples::badge(Status::Ok, "87% · Charging"),
            _ => return None,
        })
    }
    fn compute(&self, ctx: &FetchContext) -> Payload {
        let opts: BatteryOptions = match parse_options(ctx.options.as_ref()) {
            Ok(o) => o,
            Err(msg) => return options_placeholder(&msg),
        };
        let snapshot = self.read_snapshot(opts.index.unwrap_or(0));
        let shape = ctx.shape.unwrap_or(Shape::Ratio);
        match (snapshot, shape) {
            (Some(snap), Shape::Text) => payload(Body::Text(TextData {
                value: format_battery_text(&snap, opts.kind.unwrap_or_default()),
            })),
            (Some(snap), Shape::Entries) => payload(Body::Entries(EntriesData {
                items: battery_entries(&snap),
            })),
            (Some(snap), Shape::Badge) => payload(Body::Badge(battery_badge(&snap))),
            (Some(snap), _) => payload(Body::Ratio(RatioData {
                value: snap.charge,
                label: Some(format!(
                    "{} • {}",
                    format_percent(snap.charge),
                    snap.state.label()
                )),
                denominator: None,
            })),
            (None, shape) => no_battery_payload(shape, opts.kind.unwrap_or_default()),
        }
    }
}

impl BatteryFetcher {
    fn read_snapshot(&self, index: usize) -> Option<BatterySnapshot> {
        let manager = self.manager.lock().expect("battery manager mutex poisoned");
        let manager = manager.as_ref()?;
        let battery = manager.batteries().ok()?.nth(index)?.ok()?;
        Some(snapshot_from(&battery))
    }
}

fn snapshot_from(battery: &starship_battery::Battery) -> BatterySnapshot {
    BatterySnapshot {
        charge: f64::from(battery.state_of_charge().value).clamp(0.0, 1.0),
        state: map_battery_state(battery.state()),
        time_remaining_secs: time_remaining_secs(battery),
        cycle_count: battery.cycle_count(),
        health: battery_health(battery),
    }
}

fn map_battery_state(s: starship_battery::State) -> BatteryState {
    use starship_battery::State as S;
    match s {
        S::Charging => BatteryState::Charging,
        S::Discharging => BatteryState::Discharging,
        S::Full => BatteryState::Full,
        S::Empty => BatteryState::Empty,
        _ => BatteryState::Unknown,
    }
}

fn time_remaining_secs(battery: &starship_battery::Battery) -> Option<u64> {
    let dur = match battery.state() {
        starship_battery::State::Charging => battery.time_to_full(),
        starship_battery::State::Discharging => battery.time_to_empty(),
        _ => None,
    }?;
    Some(dur.value.max(0.0) as u64)
}

fn battery_health(battery: &starship_battery::Battery) -> Option<f64> {
    let full = f64::from(battery.energy_full().value);
    let design = f64::from(battery.energy_full_design().value);
    if design <= 0.0 {
        None
    } else {
        Some((full / design).clamp(0.0, 1.0))
    }
}

fn format_battery_text(snap: &BatterySnapshot, kind: BatteryTextKind) -> String {
    match kind {
        BatteryTextKind::Percent => format_percent(snap.charge),
        BatteryTextKind::Status => snap.state.label().into(),
        BatteryTextKind::TimeRemaining => snap
            .time_remaining_secs
            .map(format_uptime)
            .unwrap_or_else(|| "—".into()),
        BatteryTextKind::Summary => match snap.time_remaining_secs {
            Some(secs) => format!(
                "{} • {} • {}",
                format_percent(snap.charge),
                snap.state.label(),
                format_uptime(secs)
            ),
            None => format!("{} • {}", format_percent(snap.charge), snap.state.label()),
        },
    }
}

fn battery_badge(snap: &BatterySnapshot) -> BadgeData {
    let status = match snap.state {
        BatteryState::Charging | BatteryState::Full => Status::Ok,
        _ if snap.charge < 0.20 => Status::Error,
        _ if snap.charge < 0.50 => Status::Warn,
        _ => Status::Ok,
    };
    BadgeData {
        status,
        label: format!("{} · {}", format_percent(snap.charge), snap.state.label()),
    }
}

fn battery_entries(snap: &BatterySnapshot) -> Vec<Entry> {
    let mut items = vec![
        entry("charge", &format_percent(snap.charge)),
        entry("state", snap.state.label()),
    ];
    if let Some(secs) = snap.time_remaining_secs {
        items.push(entry("time_left", &format_uptime(secs)));
    }
    if let Some(cycles) = snap.cycle_count {
        items.push(entry("cycles", &cycles.to_string()));
    }
    if let Some(h) = snap.health {
        items.push(entry("health", &format_percent(h)));
    }
    items
}

fn no_battery_payload(shape: Shape, kind: BatteryTextKind) -> Payload {
    match shape {
        Shape::Text => payload(Body::Text(TextData {
            value: match kind {
                BatteryTextKind::Percent => "100%".into(),
                BatteryTextKind::TimeRemaining => "—".into(),
                _ => "AC".into(),
            },
        })),
        Shape::Entries => payload(Body::Entries(EntriesData {
            items: vec![entry("power", "AC")],
        })),
        Shape::Badge => payload(Body::Badge(BadgeData {
            status: Status::Ok,
            label: "AC".into(),
        })),
        _ => payload(Body::Ratio(RatioData {
            value: 1.0,
            label: Some("AC".into()),
            denominator: None,
        })),
    }
}

fn format_percent(ratio: f64) -> String {
    format!("{:.0}%", ratio.clamp(0.0, 1.0) * 100.0)
}

fn payload(body: Body) -> Payload {
    Payload {
        icon: None,
        status: None,
        format: None,
        body,
    }
}

fn entry(key: &str, value: &str) -> Entry {
    Entry {
        key: key.into(),
        value: Some(value.into()),
        status: None,
    }
}

fn os_label() -> String {
    System::long_os_version()
        .or_else(System::name)
        .unwrap_or_else(|| "unknown".into())
}

fn memory_ratio(sys: &System) -> f64 {
    ratio_of(sys.used_memory(), sys.total_memory())
}

fn ratio_of(numer: u64, denom: u64) -> f64 {
    if denom == 0 {
        0.0
    } else {
        (numer as f64 / denom as f64).clamp(0.0, 1.0)
    }
}

fn top_processes(sys: &System, count: usize) -> Vec<(String, f32)> {
    let mut rows: Vec<(String, f32)> = sys
        .processes()
        .values()
        .map(|p| (p.name().to_string_lossy().into_owned(), p.cpu_usage()))
        .collect();
    rows.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    rows.truncate(count);
    rows
}

fn primary_disk(disks: &Disks) -> Option<(u64, u64)> {
    disks
        .iter()
        .filter(|d| d.total_space() > 0)
        .max_by_key(|d| d.total_space())
        .map(|d| (d.total_space(), d.available_space()))
}

fn disk_bars(disks: &Disks) -> Vec<Bar> {
    disks
        .iter()
        .filter(|d| d.total_space() > 0)
        .map(|d| Bar {
            label: d.mount_point().to_string_lossy().into_owned(),
            value: d.total_space().saturating_sub(d.available_space()),
        })
        .collect()
}

fn disk_label(total: u64, available: u64) -> String {
    let used = total.saturating_sub(available);
    format!(
        "{:.0}% of {}",
        ratio_of(used, total) * 100.0,
        format_bytes(total)
    )
}

fn format_uptime(secs: u64) -> String {
    let days = secs / 86_400;
    let hours = (secs % 86_400) / 3600;
    let minutes = (secs % 3600) / 60;
    match (days, hours, minutes) {
        (0, 0, m) => format!("{m}m"),
        (0, h, m) => format!("{h}h {m}m"),
        (d, h, _) => format!("{d}d {h}h"),
    }
}

fn format_load(v: f64) -> String {
    if cfg!(windows) {
        "n/a".into()
    } else {
        format!("{v:.2}")
    }
}

#[cfg(windows)]
fn load_line(_: f64, _: f64, _: f64) -> String {
    "n/a (windows)".into()
}

#[cfg(not(windows))]
fn load_line(one: f64, five: f64, fifteen: f64) -> String {
    format!("{one:.2} {five:.2} {fifteen:.2}")
}

const KB: u64 = 1024;
const MB: u64 = 1024 * KB;
const GB: u64 = 1024 * MB;
const TB: u64 = 1024 * GB;

fn format_bytes(bytes: u64) -> String {
    if bytes >= TB {
        format!("{:.1} TB", bytes as f64 / TB as f64)
    } else if bytes >= GB {
        format!("{:.1} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.0} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.0} KB", bytes as f64 / KB as f64)
    } else {
        format!("{bytes} B")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::paths::TEST_ENV_LOCK;
    use std::time::Duration;

    struct EnvGuard {
        _lock: std::sync::MutexGuard<'static, ()>,
        restore: Vec<(&'static str, Option<String>)>,
    }

    impl EnvGuard {
        fn set(pairs: &[(&'static str, Option<&str>)]) -> Self {
            let lock = TEST_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
            let mut restore: Vec<(&'static str, Option<String>)> = Vec::new();
            for (key, value) in pairs {
                if !restore.iter().any(|(k, _)| k == key) {
                    restore.push((*key, std::env::var(key).ok()));
                }
                match value {
                    Some(value) => unsafe { std::env::set_var(key, value) },
                    None => unsafe { std::env::remove_var(key) },
                }
            }
            Self {
                _lock: lock,
                restore,
            }
        }
    }

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            self.restore.iter().for_each(|(key, value)| match value {
                Some(value) => unsafe { std::env::set_var(key, value) },
                None => unsafe { std::env::remove_var(key) },
            });
        }
    }

    const TERMINAL_ENV_KEYS: &[&str] = &[
        "WT_SESSION",
        "GHOSTTY_RESOURCES_DIR",
        "KITTY_WINDOW_ID",
        "TERM",
        "ALACRITTY_WINDOW_ID",
        "ALACRITTY_LOG",
        "WEZTERM_PANE",
        "TERM_PROGRAM",
    ];

    fn ctx_with_shape(shape: Option<Shape>) -> FetchContext {
        FetchContext {
            widget_id: "w".into(),
            timeout: Duration::from_secs(1),
            shape,
            ..Default::default()
        }
    }

    fn ctx_text(options: Option<&str>) -> FetchContext {
        let options = options.map(|s| toml::from_str::<toml::Value>(s).unwrap());
        FetchContext {
            widget_id: "w".into(),
            timeout: Duration::from_secs(1),
            shape: Some(Shape::Text),
            options,
            ..Default::default()
        }
    }

    fn detect_terminal_with(overrides: &[(&'static str, &'static str)]) -> String {
        let pairs: Vec<_> = TERMINAL_ENV_KEYS
            .iter()
            .map(|key| {
                (
                    *key,
                    overrides.iter().find_map(|(override_key, value)| {
                        (*override_key == *key).then_some(*value)
                    }),
                )
            })
            .collect();
        let _guard = EnvGuard::set(&pairs);
        detect_terminal()
    }

    fn assert_realtime_contract(
        fetcher: &dyn RealtimeFetcher,
        expected_name: &str,
        expected_shapes: &[Shape],
        default_shape: Shape,
        unsupported_shape: Shape,
        option_count: usize,
    ) {
        assert_eq!(fetcher.name(), expected_name);
        assert_eq!(fetcher.safety(), Safety::Safe);
        assert!(!fetcher.description().is_empty());
        assert_eq!(fetcher.shapes(), expected_shapes);
        assert_eq!(fetcher.default_shape(), default_shape);
        assert_eq!(fetcher.option_schemas().len(), option_count);
        expected_shapes
            .iter()
            .for_each(|shape| assert!(fetcher.sample_body(*shape).is_some()));
        assert!(fetcher.sample_body(unsupported_shape).is_none());
    }

    fn assert_cached_contract(
        fetcher: &dyn Fetcher,
        expected_name: &str,
        expected_shapes: &[Shape],
        unsupported_shape: Shape,
    ) {
        assert_eq!(fetcher.name(), expected_name);
        assert_eq!(fetcher.safety(), Safety::Safe);
        assert!(!fetcher.description().is_empty());
        assert_eq!(fetcher.shapes(), expected_shapes);
        assert_eq!(fetcher.default_shape(), expected_shapes[0]);
        expected_shapes
            .iter()
            .for_each(|shape| assert!(fetcher.sample_body(*shape).is_some()));
        assert!(fetcher.sample_body(unsupported_shape).is_none());
    }

    #[test]
    fn system_family_registers_builtin_fetchers() {
        let realtime_names: Vec<_> = realtime_fetchers()
            .into_iter()
            .map(|fetcher| fetcher.name().to_string())
            .collect();
        let cached_names: Vec<_> = cached_fetchers()
            .into_iter()
            .map(|fetcher| fetcher.name().to_string())
            .collect();
        assert_eq!(
            realtime_names,
            vec![
                "system",
                "system_cpu",
                "system_memory",
                "system_uptime",
                "system_load",
                "system_processes",
                "system_battery",
            ]
        );
        assert_eq!(cached_names, vec!["system_disk_usage"]);
    }

    #[test]
    fn fetcher_contracts_cover_supported_shapes_and_samples() {
        assert_realtime_contract(
            &SystemFetcher::default(),
            "system",
            &[Shape::Text, Shape::TextBlock, Shape::Entries],
            Shape::Entries,
            Shape::Ratio,
            1,
        );
        assert_realtime_contract(
            &CpuLoadFetcher::default(),
            "system_cpu",
            &[Shape::Ratio, Shape::Text],
            Shape::Ratio,
            Shape::Entries,
            0,
        );
        assert_realtime_contract(
            &MemoryFetcher::default(),
            "system_memory",
            &[Shape::Ratio, Shape::Text, Shape::Entries],
            Shape::Ratio,
            Shape::Bars,
            0,
        );
        assert_realtime_contract(
            &UptimeFetcher,
            "system_uptime",
            &[Shape::Text],
            Shape::Text,
            Shape::Entries,
            0,
        );
        assert_realtime_contract(
            &LoadAverageFetcher,
            "system_load",
            &[Shape::Text, Shape::Entries],
            Shape::Text,
            Shape::Badge,
            0,
        );
        assert_realtime_contract(
            &ProcessTopFetcher::default(),
            "system_processes",
            &[Shape::Entries, Shape::TextBlock],
            Shape::Entries,
            Shape::Ratio,
            0,
        );
        assert_realtime_contract(
            &BatteryFetcher::default(),
            "system_battery",
            &[Shape::Ratio, Shape::Text, Shape::Entries, Shape::Badge],
            Shape::Ratio,
            Shape::Bars,
            2,
        );
        assert_cached_contract(
            &DiskFetcher,
            "system_disk_usage",
            &[Shape::Ratio, Shape::Text, Shape::Bars],
            Shape::Entries,
        );
    }

    #[test]
    fn parse_options_defaults_and_surfaces_invalid_input() {
        let system: SystemOptions = parse_options(None).unwrap();
        assert!(system.kind.is_none());

        let battery_raw: toml::Value = toml::from_str("kind = \"percent\"\nindex = 2").unwrap();
        let battery: BatteryOptions = parse_options(Some(&battery_raw)).unwrap();
        assert!(matches!(battery.kind, Some(BatteryTextKind::Percent)));
        assert_eq!(battery.index, Some(2));

        let invalid: toml::Value = toml::from_str("bogus = true").unwrap();
        let err = parse_options::<BatteryOptions>(Some(&invalid)).unwrap_err();
        assert!(err.starts_with("invalid options:"));
    }

    #[test]
    fn options_placeholder_wraps_the_message_in_warning_text() {
        let Body::Text(text) = options_placeholder("bad config").body else {
            panic!("expected text body");
        };
        assert_eq!(text.value, "⚠ bad config");
    }

    #[test]
    fn detect_terminal_prefers_known_env_markers_and_fallbacks() {
        assert_eq!(
            detect_terminal_with(&[("WT_SESSION", "1"), ("TERM_PROGRAM", "Hyper")]),
            "Windows Terminal"
        );
        assert_eq!(
            detect_terminal_with(&[("GHOSTTY_RESOURCES_DIR", "/tmp/resources")]),
            "Ghostty"
        );
        assert_eq!(detect_terminal_with(&[("TERM", "xterm-kitty")]), "Kitty");
        assert_eq!(
            detect_terminal_with(&[("ALACRITTY_LOG", "/tmp/alacritty.log")]),
            "Alacritty"
        );
        assert_eq!(detect_terminal_with(&[("WEZTERM_PANE", "pane")]), "WezTerm");
        assert_eq!(
            detect_terminal_with(&[("TERM_PROGRAM", "vscode")]),
            "VS Code"
        );
        assert_eq!(
            detect_terminal_with(&[("TERM_PROGRAM", "CustomTerm")]),
            "CustomTerm"
        );
        assert_eq!(detect_terminal_with(&[]), "terminal");
    }

    #[test]
    fn detect_shell_uses_basename_and_fallback() {
        let _guard = EnvGuard::set(&[("SHELL", Some("/usr/local/bin/fish"))]);
        assert_eq!(detect_shell(), "fish");
        drop(_guard);

        let _guard = EnvGuard::set(&[("SHELL", None)]);
        assert_eq!(detect_shell(), "shell");
    }

    #[test]
    fn resolve_system_kind_covers_each_identity_variant() {
        let pairs: Vec<_> = TERMINAL_ENV_KEYS
            .iter()
            .copied()
            .map(|key| (key, None))
            .chain([("SHELL", Some("/bin/zsh")), ("TERM_PROGRAM", Some("Hyper"))])
            .collect();
        let _guard = EnvGuard::set(&pairs);

        assert_eq!(resolve_system_kind(SystemKind::Terminal), "Hyper");
        assert!(!resolve_system_kind(SystemKind::Os).is_empty());
        assert!(!resolve_system_kind(SystemKind::OsVersion).is_empty());
        assert!(!resolve_system_kind(SystemKind::Hostname).is_empty());
        assert_eq!(resolve_system_kind(SystemKind::Shell), "zsh");
        assert_eq!(
            resolve_system_kind(SystemKind::Arch),
            std::env::consts::ARCH
        );
    }

    #[test]
    fn format_uptime_covers_minute_hour_day_ranges() {
        assert_eq!(format_uptime(0), "0m");
        assert_eq!(format_uptime(45 * 60), "45m");
        assert_eq!(format_uptime(2 * 3600 + 15 * 60), "2h 15m");
        assert_eq!(format_uptime(3 * 86_400 + 4 * 3600 + 30 * 60), "3d 4h");
    }

    #[test]
    fn format_bytes_buckets_by_unit() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(4 * KB), "4 KB");
        assert_eq!(format_bytes(250 * MB), "250 MB");
        assert_eq!(format_bytes(2 * GB + GB / 2), "2.5 GB");
        assert_eq!(format_bytes(3 * TB), "3.0 TB");
    }

    #[test]
    fn ratio_of_handles_zero_denominator() {
        assert_eq!(ratio_of(10, 0), 0.0);
        assert_eq!(ratio_of(5, 10), 0.5);
        assert_eq!(ratio_of(20, 10), 1.0);
    }

    #[test]
    fn cpu_load_defaults_to_ratio() {
        let p = CpuLoadFetcher::new().compute(&ctx_with_shape(None));
        assert!(matches!(p.body, Body::Ratio(_)));
    }

    #[test]
    fn cpu_load_emits_text_when_requested() {
        let p = CpuLoadFetcher::new().compute(&ctx_with_shape(Some(Shape::Text)));
        assert!(matches!(p.body, Body::Text(_)));
    }

    #[test]
    fn memory_defaults_to_ratio() {
        let p = MemoryFetcher::new().compute(&ctx_with_shape(None));
        let Body::Ratio(r) = p.body else {
            panic!("expected ratio");
        };
        assert!((0.0..=1.0).contains(&r.value));
    }

    #[test]
    fn memory_entries_shape_has_three_rows() {
        let p = MemoryFetcher::new().compute(&ctx_with_shape(Some(Shape::Entries)));
        let Body::Entries(e) = p.body else {
            panic!("expected entries");
        };
        let keys: Vec<_> = e.items.iter().map(|i| i.key.as_str()).collect();
        assert_eq!(keys, ["used", "total", "free"]);
    }

    #[test]
    fn memory_text_shape_formats_used_over_total() {
        let p = MemoryFetcher::new().compute(&ctx_with_shape(Some(Shape::Text)));
        let Body::Text(text) = p.body else {
            panic!("expected text");
        };
        assert!(text.value.contains(" / "));
    }

    #[test]
    fn uptime_emits_text() {
        let p = UptimeFetcher.compute(&ctx_with_shape(None));
        let Body::Text(t) = p.body else {
            panic!("expected text");
        };
        assert!(!t.value.is_empty());
    }

    #[test]
    fn load_average_defaults_to_text() {
        let p = LoadAverageFetcher.compute(&ctx_with_shape(None));
        assert!(matches!(p.body, Body::Text(_)));
    }

    #[test]
    fn load_average_entries_shape_has_three_windows() {
        let p = LoadAverageFetcher.compute(&ctx_with_shape(Some(Shape::Entries)));
        let Body::Entries(e) = p.body else {
            panic!("expected entries");
        };
        let keys: Vec<_> = e.items.iter().map(|i| i.key.as_str()).collect();
        assert_eq!(keys, ["1min", "5min", "15min"]);
    }

    #[test]
    fn system_rollup_emits_six_rows() {
        let p = SystemFetcher::new().compute(&ctx_with_shape(None));
        let Body::Entries(e) = p.body else {
            panic!("expected entries");
        };
        assert_eq!(e.items.len(), 6);
    }

    #[test]
    fn system_text_shape_defaults_to_terminal_kind() {
        let p = SystemFetcher::new().compute(&ctx_text(None));
        let Body::Text(t) = p.body else {
            panic!("expected text");
        };
        assert!(!t.value.is_empty());
    }

    #[test]
    fn system_text_shape_emits_arch_when_requested() {
        let p = SystemFetcher::new().compute(&ctx_text(Some("kind = \"arch\"")));
        let Body::Text(t) = p.body else {
            panic!("expected text");
        };
        assert_eq!(t.value, std::env::consts::ARCH);
    }

    #[test]
    fn system_text_shape_rejects_unknown_kind_to_placeholder() {
        let p = SystemFetcher::new().compute(&ctx_text(Some("kind = \"bogus\"")));
        let Body::Text(t) = p.body else {
            panic!("expected text");
        };
        assert!(t.value.starts_with("⚠"));
    }

    #[test]
    fn detect_terminal_returns_non_empty_label() {
        assert!(!detect_terminal().is_empty());
    }

    /// Prints one Text-shape line per `kind` on the host running the tests. Kept `#[ignore]` so
    /// the regular run stays side-effect free, but a dev can verify real output with
    /// `cargo test -- --ignored fetcher::system::tests::live_system_text_all_kinds --nocapture`.
    #[test]
    #[ignore]
    fn live_system_text_all_kinds() {
        let cases = [
            ("terminal", "kind = \"terminal\""),
            ("os", "kind = \"os\""),
            ("os_version", "kind = \"os_version\""),
            ("hostname", "kind = \"hostname\""),
            ("shell", "kind = \"shell\""),
            ("arch", "kind = \"arch\""),
        ];
        for (label, opts) in cases {
            let p = SystemFetcher::new().compute(&ctx_text(Some(opts)));
            let Body::Text(t) = p.body else {
                panic!("expected text for {label}");
            };
            eprintln!("{label:<12} → {}", t.value);
            assert!(!t.value.is_empty());
        }
    }

    #[test]
    fn system_text_block_shape_returns_key_value_strings() {
        let p = SystemFetcher::new().compute(&ctx_with_shape(Some(Shape::TextBlock)));
        let Body::TextBlock(l) = p.body else {
            panic!("expected text_block");
        };
        assert_eq!(l.lines.len(), 6);
        assert!(l.lines.iter().all(|s| s.contains(": ")));
    }

    #[test]
    fn process_top_respects_count_cap() {
        let p = ProcessTopFetcher::new().compute(&ctx_with_shape(None));
        let Body::Entries(e) = p.body else {
            panic!("expected entries");
        };
        assert!(e.items.len() <= PROCESS_TOP_COUNT);
    }

    #[test]
    fn process_top_text_block_shape_formats_rows() {
        let p = ProcessTopFetcher::new().compute(&ctx_with_shape(Some(Shape::TextBlock)));
        let Body::TextBlock(block) = p.body else {
            panic!("expected text block");
        };
        assert!(block.lines.len() <= PROCESS_TOP_COUNT);
        assert!(block.lines.iter().all(|line| line.ends_with('%')));
    }

    fn snapshot(charge: f64, state: BatteryState, secs: Option<u64>) -> BatterySnapshot {
        BatterySnapshot {
            charge,
            state,
            time_remaining_secs: secs,
            cycle_count: Some(284),
            health: Some(0.97),
        }
    }

    #[test]
    fn battery_summary_includes_time_when_available() {
        let snap = snapshot(0.87, BatteryState::Charging, Some(83 * 60));
        assert_eq!(
            format_battery_text(&snap, BatteryTextKind::Summary),
            "87% • Charging • 1h 23m"
        );
    }

    #[test]
    fn battery_summary_omits_time_when_missing() {
        let snap = snapshot(1.0, BatteryState::Full, None);
        assert_eq!(
            format_battery_text(&snap, BatteryTextKind::Summary),
            "100% • Full"
        );
    }

    #[test]
    fn battery_text_kinds_pick_distinct_fields() {
        let snap = snapshot(0.5, BatteryState::Discharging, Some(45 * 60));
        assert_eq!(format_battery_text(&snap, BatteryTextKind::Percent), "50%");
        assert_eq!(
            format_battery_text(&snap, BatteryTextKind::Status),
            "Discharging"
        );
        assert_eq!(
            format_battery_text(&snap, BatteryTextKind::TimeRemaining),
            "45m"
        );
    }

    #[test]
    fn battery_time_remaining_dash_when_missing() {
        let snap = snapshot(1.0, BatteryState::Full, None);
        assert_eq!(
            format_battery_text(&snap, BatteryTextKind::TimeRemaining),
            "—"
        );
    }

    #[test]
    fn battery_state_mapping_and_labels_cover_all_variants() {
        use starship_battery::State;

        assert_eq!(map_battery_state(State::Charging).label(), "Charging");
        assert_eq!(map_battery_state(State::Discharging).label(), "Discharging");
        assert_eq!(map_battery_state(State::Full).label(), "Full");
        assert_eq!(map_battery_state(State::Empty).label(), "Empty");
        assert_eq!(map_battery_state(State::Unknown).label(), "Unknown");
    }

    #[test]
    fn battery_entries_include_optional_fields_only_when_present() {
        let with = snapshot(0.5, BatteryState::Charging, Some(60));
        let mut without = snapshot(0.5, BatteryState::Charging, None);
        without.cycle_count = None;
        without.health = None;
        assert_eq!(battery_entries(&with).len(), 5);
        assert_eq!(battery_entries(&without).len(), 2);
    }

    #[test]
    fn no_battery_ratio_is_full_ac() {
        let p = no_battery_payload(Shape::Ratio, BatteryTextKind::Summary);
        let Body::Ratio(r) = p.body else {
            panic!("expected ratio")
        };
        assert_eq!(r.value, 1.0);
        assert_eq!(r.label.as_deref(), Some("AC"));
    }

    #[test]
    fn no_battery_text_varies_by_kind() {
        let summary = no_battery_payload(Shape::Text, BatteryTextKind::Summary);
        let percent = no_battery_payload(Shape::Text, BatteryTextKind::Percent);
        let time = no_battery_payload(Shape::Text, BatteryTextKind::TimeRemaining);
        let extract = |p: Payload| match p.body {
            Body::Text(t) => t.value,
            _ => panic!(),
        };
        assert_eq!(extract(summary), "AC");
        assert_eq!(extract(percent), "100%");
        assert_eq!(extract(time), "—");
    }

    #[test]
    fn no_battery_entries_and_badge_use_ac_placeholders() {
        let Body::Entries(entries) =
            no_battery_payload(Shape::Entries, BatteryTextKind::Summary).body
        else {
            panic!("expected entries");
        };
        let Body::Badge(badge) = no_battery_payload(Shape::Badge, BatteryTextKind::Summary).body
        else {
            panic!("expected badge");
        };
        assert_eq!(entries.items[0].key, "power");
        assert_eq!(entries.items[0].value.as_deref(), Some("AC"));
        assert_eq!(badge.status, Status::Ok);
        assert_eq!(badge.label, "AC");
    }

    #[test]
    fn battery_compute_never_panics_on_any_shape() {
        let f = BatteryFetcher::new();
        for shape in [
            None,
            Some(Shape::Ratio),
            Some(Shape::Text),
            Some(Shape::Entries),
            Some(Shape::Badge),
        ] {
            let p = f.compute(&ctx_with_shape(shape));
            // Each branch must produce *some* body; on hosts without a battery we land on the
            // AC stand-in, on laptops we get the real reading. Both are valid.
            assert!(!matches!(p.body, Body::Image(_)));
        }
    }

    #[test]
    fn battery_badge_status_reflects_charge_and_state() {
        let low = snapshot(0.05, BatteryState::Discharging, None);
        assert_eq!(battery_badge(&low).status, Status::Error);
        let mid = snapshot(0.30, BatteryState::Discharging, None);
        assert_eq!(battery_badge(&mid).status, Status::Warn);
        let high = snapshot(0.95, BatteryState::Discharging, None);
        assert_eq!(battery_badge(&high).status, Status::Ok);
        let charging_low = snapshot(0.05, BatteryState::Charging, None);
        assert_eq!(battery_badge(&charging_low).status, Status::Ok);
    }

    #[test]
    fn battery_rejects_unknown_option_to_placeholder() {
        let f = BatteryFetcher::new();
        let p = f.compute(&ctx_text(Some("bogus = true")));
        let Body::Text(t) = p.body else {
            panic!("expected text placeholder")
        };
        assert!(t.value.starts_with("⚠"));
    }

    #[test]
    fn battery_compute_without_manager_uses_no_battery_fallbacks() {
        let fetcher = BatteryFetcher {
            manager: Mutex::new(None),
        };

        let Body::Text(text) = fetcher.compute(&ctx_text(Some("kind = \"percent\""))).body else {
            panic!("expected text");
        };
        let Body::Entries(entries) = fetcher.compute(&ctx_with_shape(Some(Shape::Entries))).body
        else {
            panic!("expected entries");
        };
        let Body::Badge(badge) = fetcher.compute(&ctx_with_shape(Some(Shape::Badge))).body else {
            panic!("expected badge");
        };

        assert_eq!(text.value, "100%");
        assert_eq!(entries.items[0].value.as_deref(), Some("AC"));
        assert_eq!(badge.label, "AC");
    }

    #[tokio::test]
    async fn disk_defaults_to_ratio_or_text_fallback() {
        let ctx = ctx_with_shape(None);
        let p = DiskFetcher.fetch(&ctx).await.unwrap();
        assert!(matches!(p.body, Body::Ratio(_) | Body::Text(_)));
    }

    #[tokio::test]
    async fn disk_bars_shape_emits_bars_body() {
        let ctx = ctx_with_shape(Some(Shape::Bars));
        let p = DiskFetcher.fetch(&ctx).await.unwrap();
        assert!(matches!(p.body, Body::Bars(_)));
    }
}