presentar-terminal 0.3.5

Terminal backend for Presentar UI framework with zero-allocation rendering
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
//! Battery panel rendering and utilities.
//!
//! Provides battery panel title building, charge formatting,
//! and helper functions for rendering battery metrics.

use presentar_core::Color;

// =============================================================================
// BATTERY TITLE BUILDING
// =============================================================================

/// Build battery panel title string.
///
/// Format: "Battery │ 85% │ Charging │ 2h 15m"
#[must_use]
pub fn build_battery_title(
    percent: f64,
    state: BatteryState,
    time_remaining: Option<u64>,
) -> String {
    let state_str = state.display_name();

    if let Some(secs) = time_remaining {
        let time_str = format_time_remaining(secs);
        format!("Battery │ {:.0}% │ {}{}", percent, state_str, time_str)
    } else {
        format!("Battery │ {:.0}% │ {}", percent, state_str)
    }
}

/// Build compact battery title for narrow panels.
///
/// Format: "Bat │ 85% ⚡"
#[must_use]
pub fn build_battery_title_compact(percent: f64, is_charging: bool) -> String {
    let icon = if is_charging { "" } else { "" };
    format!("Bat │ {:.0}%{}", percent, icon)
}

// =============================================================================
// BATTERY STATE
// =============================================================================

/// Battery charging state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BatteryState {
    /// Battery is charging
    Charging,
    /// Battery is discharging (on battery power)
    #[default]
    Discharging,
    /// Battery is full and connected to power
    Full,
    /// Battery status is unknown
    Unknown,
    /// Battery is not present
    NotPresent,
}

impl BatteryState {
    /// Get display name for battery state.
    #[must_use]
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Charging => "Charging",
            Self::Discharging => "Discharging",
            Self::Full => "Full",
            Self::Unknown => "Unknown",
            Self::NotPresent => "N/A",
        }
    }

    /// Get short display name.
    #[must_use]
    pub fn short_name(&self) -> &'static str {
        match self {
            Self::Charging => "CHG",
            Self::Discharging => "DIS",
            Self::Full => "FULL",
            Self::Unknown => "UNK",
            Self::NotPresent => "N/A",
        }
    }

    /// Get icon for battery state.
    #[must_use]
    pub fn icon(&self) -> &'static str {
        match self {
            Self::Charging => "",
            Self::Discharging => "🔋",
            Self::Full => "🔌",
            Self::Unknown => "",
            Self::NotPresent => "",
        }
    }

    /// Check if battery is charging.
    #[must_use]
    pub fn is_charging(&self) -> bool {
        matches!(self, Self::Charging)
    }

    /// Check if on battery power.
    #[must_use]
    pub fn is_discharging(&self) -> bool {
        matches!(self, Self::Discharging)
    }
}

// =============================================================================
// TIME FORMATTING
// =============================================================================

/// Format time remaining in human-readable form.
///
/// # Examples
/// - 3600 -> "1h 0m"
/// - 5400 -> "1h 30m"
/// - 300 -> "5m"
#[must_use]
pub fn format_time_remaining(seconds: u64) -> String {
    let hours = seconds / 3600;
    let minutes = (seconds % 3600) / 60;

    if hours > 0 {
        format!("{}h {}m", hours, minutes)
    } else {
        format!("{}m", minutes)
    }
}

/// Format time remaining in compact form.
///
/// # Examples
/// - 3600 -> "1:00"
/// - 5400 -> "1:30"
#[must_use]
pub fn format_time_compact(seconds: u64) -> String {
    let hours = seconds / 3600;
    let minutes = (seconds % 3600) / 60;
    format!("{}:{:02}", hours, minutes)
}

// =============================================================================
// BATTERY COLORS
// =============================================================================

/// Get color for battery percentage.
#[must_use]
pub fn battery_percent_color(percent: f64, is_charging: bool) -> Color {
    if is_charging {
        // When charging, always show positive color
        Color::new(0.3, 0.9, 0.5, 1.0) // Green
    } else if percent <= 10.0 {
        Color::new(1.0, 0.3, 0.3, 1.0) // Critical red
    } else if percent <= 20.0 {
        Color::new(1.0, 0.5, 0.2, 1.0) // Warning orange
    } else if percent <= 40.0 {
        Color::new(1.0, 0.8, 0.2, 1.0) // Yellow
    } else {
        Color::new(0.3, 0.9, 0.5, 1.0) // Green
    }
}

/// Get color for battery state.
#[must_use]
pub fn battery_state_color(state: BatteryState) -> Color {
    match state {
        BatteryState::Charging => Color::new(0.3, 0.9, 0.5, 1.0), // Green
        BatteryState::Full => Color::new(0.4, 0.8, 1.0, 1.0),     // Blue
        BatteryState::Discharging => Color::new(1.0, 0.8, 0.3, 1.0), // Yellow
        BatteryState::Unknown => Color::new(0.5, 0.5, 0.5, 1.0),  // Gray
        BatteryState::NotPresent => Color::new(0.3, 0.3, 0.3, 1.0), // Dark gray
    }
}

// =============================================================================
// BATTERY BAR SEGMENTS
// =============================================================================

/// Battery icon segment for visual display.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BatteryIcon {
    /// Number of filled segments (0-4)
    pub filled: u8,
    /// Total segments
    pub total: u8,
}

impl BatteryIcon {
    /// Create battery icon from percentage.
    #[must_use]
    pub fn from_percent(percent: f64) -> Self {
        let filled = match percent {
            p if p >= 87.5 => 4,
            p if p >= 62.5 => 3,
            p if p >= 37.5 => 2,
            p if p >= 12.5 => 1,
            _ => 0,
        };

        Self { filled, total: 4 }
    }

    /// Get visual representation.
    #[must_use]
    pub fn display(&self) -> String {
        let filled_char = '';
        let empty_char = '';

        let filled: String = std::iter::repeat(filled_char)
            .take(self.filled as usize)
            .collect();
        let empty: String = std::iter::repeat(empty_char)
            .take((self.total - self.filled) as usize)
            .collect();

        format!("[{}{}]", filled, empty)
    }
}

impl Default for BatteryIcon {
    fn default() -> Self {
        Self::from_percent(100.0)
    }
}

// =============================================================================
// HEALTH METRICS
// =============================================================================

/// Battery health status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BatteryHealth {
    /// Battery is healthy
    Good,
    /// Battery is degraded but functional
    Fair,
    /// Battery needs replacement
    Poor,
    /// Battery health unknown
    Unknown,
}

impl BatteryHealth {
    /// Determine health from capacity percentage.
    #[must_use]
    pub fn from_capacity(design_capacity: u64, full_capacity: u64) -> Self {
        if design_capacity == 0 {
            return Self::Unknown;
        }

        let percent = (full_capacity as f64 / design_capacity as f64) * 100.0;

        if percent >= 80.0 {
            Self::Good
        } else if percent >= 50.0 {
            Self::Fair
        } else {
            Self::Poor
        }
    }

    /// Get color for health status.
    #[must_use]
    pub fn color(&self) -> Color {
        match self {
            Self::Good => Color::new(0.3, 0.9, 0.5, 1.0), // Green
            Self::Fair => Color::new(1.0, 0.8, 0.3, 1.0), // Yellow
            Self::Poor => Color::new(1.0, 0.4, 0.3, 1.0), // Red
            Self::Unknown => Color::new(0.5, 0.5, 0.5, 1.0), // Gray
        }
    }

    /// Get display name.
    #[must_use]
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Good => "Good",
            Self::Fair => "Fair",
            Self::Poor => "Poor",
            Self::Unknown => "Unknown",
        }
    }
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // =========================================================================
    // build_battery_title tests
    // =========================================================================

    #[test]
    fn test_build_battery_title_with_time() {
        let title = build_battery_title(85.0, BatteryState::Charging, Some(8100));
        assert!(title.contains("Battery"));
        assert!(title.contains("85%"));
        assert!(title.contains("Charging"));
        assert!(title.contains("2h 15m"));
    }

    #[test]
    fn test_build_battery_title_no_time() {
        let title = build_battery_title(100.0, BatteryState::Full, None);
        assert!(title.contains("Full"));
        assert!(!title.contains("m")); // No time
    }

    #[test]
    fn test_build_battery_title_discharging() {
        let title = build_battery_title(50.0, BatteryState::Discharging, Some(3600));
        assert!(title.contains("Discharging"));
        assert!(title.contains("1h 0m"));
    }

    // =========================================================================
    // build_battery_title_compact tests
    // =========================================================================

    #[test]
    fn test_build_battery_title_compact_charging() {
        let title = build_battery_title_compact(85.0, true);
        assert!(title.contains("Bat"));
        assert!(title.contains("85%"));
        assert!(title.contains(""));
    }

    #[test]
    fn test_build_battery_title_compact_not_charging() {
        let title = build_battery_title_compact(50.0, false);
        assert!(!title.contains(""));
    }

    // =========================================================================
    // BatteryState tests
    // =========================================================================

    #[test]
    fn test_battery_state_display_name() {
        assert_eq!(BatteryState::Charging.display_name(), "Charging");
        assert_eq!(BatteryState::Discharging.display_name(), "Discharging");
        assert_eq!(BatteryState::Full.display_name(), "Full");
    }

    #[test]
    fn test_battery_state_short_name() {
        assert_eq!(BatteryState::Charging.short_name(), "CHG");
        assert_eq!(BatteryState::Discharging.short_name(), "DIS");
    }

    #[test]
    fn test_battery_state_icon() {
        assert!(!BatteryState::Charging.icon().is_empty());
        assert!(!BatteryState::Discharging.icon().is_empty());
    }

    #[test]
    fn test_battery_state_is_charging() {
        assert!(BatteryState::Charging.is_charging());
        assert!(!BatteryState::Discharging.is_charging());
    }

    #[test]
    fn test_battery_state_is_discharging() {
        assert!(BatteryState::Discharging.is_discharging());
        assert!(!BatteryState::Charging.is_discharging());
    }

    #[test]
    fn test_battery_state_default() {
        assert_eq!(BatteryState::default(), BatteryState::Discharging);
    }

    #[test]
    fn test_battery_state_derive_debug() {
        let state = BatteryState::Charging;
        let debug = format!("{:?}", state);
        assert!(debug.contains("Charging"));
    }

    // =========================================================================
    // format_time tests
    // =========================================================================

    #[test]
    fn test_format_time_remaining_hours_minutes() {
        assert_eq!(format_time_remaining(5400), "1h 30m");
        assert_eq!(format_time_remaining(7200), "2h 0m");
    }

    #[test]
    fn test_format_time_remaining_minutes_only() {
        assert_eq!(format_time_remaining(300), "5m");
        assert_eq!(format_time_remaining(0), "0m");
    }

    #[test]
    fn test_format_time_compact() {
        assert_eq!(format_time_compact(3600), "1:00");
        assert_eq!(format_time_compact(5400), "1:30");
        assert_eq!(format_time_compact(90), "0:01");
    }

    // =========================================================================
    // battery color tests
    // =========================================================================

    #[test]
    fn test_battery_percent_color_charging() {
        let color = battery_percent_color(5.0, true);
        assert!(
            color.g > 0.8,
            "Charging should be green even at low percent"
        );
    }

    #[test]
    fn test_battery_percent_color_critical() {
        let color = battery_percent_color(5.0, false);
        assert!(color.r > 0.9 && color.g < 0.5, "Critical should be red");
    }

    #[test]
    fn test_battery_percent_color_warning() {
        let color = battery_percent_color(15.0, false);
        assert!(color.r > 0.9, "Warning should be orange");
    }

    #[test]
    fn test_battery_percent_color_low() {
        let color = battery_percent_color(30.0, false);
        assert!(color.r > 0.9 && color.g > 0.7, "Low should be yellow");
    }

    #[test]
    fn test_battery_percent_color_normal() {
        let color = battery_percent_color(80.0, false);
        assert!(color.g > 0.8, "Normal should be green");
    }

    #[test]
    fn test_battery_state_color() {
        let color = battery_state_color(BatteryState::Charging);
        assert!(color.g > 0.8);

        let color = battery_state_color(BatteryState::Full);
        assert!(color.b > 0.9);
    }

    // =========================================================================
    // BatteryIcon tests
    // =========================================================================

    #[test]
    fn test_battery_icon_from_percent_full() {
        let icon = BatteryIcon::from_percent(100.0);
        assert_eq!(icon.filled, 4);
    }

    #[test]
    fn test_battery_icon_from_percent_empty() {
        let icon = BatteryIcon::from_percent(5.0);
        assert_eq!(icon.filled, 0);
    }

    #[test]
    fn test_battery_icon_from_percent_half() {
        let icon = BatteryIcon::from_percent(50.0);
        assert_eq!(icon.filled, 2);
    }

    #[test]
    fn test_battery_icon_display() {
        let icon = BatteryIcon::from_percent(75.0);
        let display = icon.display();
        assert!(display.starts_with('['));
        assert!(display.ends_with(']'));
        assert!(display.contains(''));
    }

    #[test]
    fn test_battery_icon_default() {
        let icon = BatteryIcon::default();
        assert_eq!(icon.filled, 4);
    }

    #[test]
    fn test_battery_icon_derive_debug() {
        let icon = BatteryIcon::from_percent(50.0);
        let debug = format!("{:?}", icon);
        assert!(debug.contains("BatteryIcon"));
    }

    // =========================================================================
    // BatteryHealth tests
    // =========================================================================

    #[test]
    fn test_battery_health_from_capacity_good() {
        let health = BatteryHealth::from_capacity(5000, 4500);
        assert_eq!(health, BatteryHealth::Good);
    }

    #[test]
    fn test_battery_health_from_capacity_fair() {
        let health = BatteryHealth::from_capacity(5000, 3000);
        assert_eq!(health, BatteryHealth::Fair);
    }

    #[test]
    fn test_battery_health_from_capacity_poor() {
        let health = BatteryHealth::from_capacity(5000, 2000);
        assert_eq!(health, BatteryHealth::Poor);
    }

    #[test]
    fn test_battery_health_from_capacity_zero() {
        let health = BatteryHealth::from_capacity(0, 0);
        assert_eq!(health, BatteryHealth::Unknown);
    }

    #[test]
    fn test_battery_health_color() {
        let color = BatteryHealth::Good.color();
        assert!(color.g > 0.8);

        let color = BatteryHealth::Poor.color();
        assert!(color.r > 0.9);
    }

    #[test]
    fn test_battery_health_display_name() {
        assert_eq!(BatteryHealth::Good.display_name(), "Good");
        assert_eq!(BatteryHealth::Fair.display_name(), "Fair");
        assert_eq!(BatteryHealth::Poor.display_name(), "Poor");
    }

    #[test]
    fn test_battery_health_derive_debug() {
        let health = BatteryHealth::Good;
        let debug = format!("{:?}", health);
        assert!(debug.contains("Good"));
    }
}