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
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
//! UI helper functions for ptop.
//!
//! Formatting and utility functions used across panels.

// =============================================================================
// FORMATTING FUNCTIONS
// =============================================================================

/// Format bytes to human-readable string (e.g., "1.5G", "128M", "512K").
///
/// Uses binary units (1024-based):
/// - T = 1024^4 bytes
/// - G = 1024^3 bytes
/// - M = 1024^2 bytes
/// - K = 1024 bytes
///
/// # Examples
/// ```ignore
/// assert_eq!(format_bytes(1536), "1.5K");
/// assert_eq!(format_bytes(1073741824), "1.0G");
/// ```
#[must_use]
pub fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;
    const TB: u64 = GB * 1024;

    if bytes >= TB {
        format!("{:.1}T", bytes as f64 / TB as f64)
    } else if bytes >= GB {
        format!("{:.1}G", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.1}M", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1}K", bytes as f64 / KB as f64)
    } else {
        format!("{bytes}B")
    }
}

/// Format bytes per second rate (e.g., "1.5G", "128M", "512K").
///
/// Similar to `format_bytes` but for transfer rates.
/// Uses one decimal place for GB/MB, no decimals for KB/B.
#[must_use]
pub fn format_bytes_rate(bytes_per_sec: f64) -> String {
    const KB: f64 = 1024.0;
    const MB: f64 = KB * 1024.0;
    const GB: f64 = MB * 1024.0;

    if bytes_per_sec >= GB {
        format!("{:.1}G", bytes_per_sec / GB)
    } else if bytes_per_sec >= MB {
        format!("{:.1}M", bytes_per_sec / MB)
    } else if bytes_per_sec >= KB {
        format!("{:.0}K", bytes_per_sec / KB)
    } else {
        format!("{bytes_per_sec:.0}B")
    }
}

/// Format uptime seconds to human-readable string (e.g., "5d 3h", "2h 15m").
///
/// # Format
/// - Days + hours if >= 1 day
/// - Hours + minutes if >= 1 hour
/// - Just minutes otherwise
#[must_use]
pub fn format_uptime(secs: u64) -> String {
    let days = secs / 86400;
    let hours = (secs % 86400) / 3600;
    let mins = (secs % 3600) / 60;

    if days > 0 {
        format!("{days}d {hours}h")
    } else if hours > 0 {
        format!("{hours}h {mins}m")
    } else {
        format!("{mins}m")
    }
}

// =============================================================================
// SYMBOLS
// =============================================================================

/// Get symbol for PSI (Pressure Stall Information) percentage.
///
/// # Symbols
/// - "▲▲" - Critical (>50%)
/// - "▲" - High (>20%)
/// - "▼" - Medium (>5%)
/// - "◐" - Low (>1%)
/// - "—" - None (≤1%)
#[must_use]
pub fn pressure_symbol(pct: f64) -> &'static str {
    if pct > 50.0 {
        "▲▲"
    } else if pct > 20.0 {
        ""
    } else if pct > 5.0 {
        ""
    } else if pct > 1.0 {
        ""
    } else {
        ""
    }
}

/// Get service name from well-known port number.
///
/// # Returns
/// - Service name for known ports (SSH, HTTP, etc.)
/// - "App" for application ports (9000-9999)
/// - Empty string for unknown ports
#[must_use]
pub fn port_to_service(port: u16) -> &'static str {
    match port {
        22 => "SSH",
        80 => "HTTP",
        443 => "HTTPS",
        53 => "DNS",
        25 => "SMTP",
        21 => "FTP",
        3306 => "MySQL",
        5432 => "Pgsql",
        6379 => "Redis",
        27017 => "Mongo",
        8080 => "HTTP",
        8443 => "HTTPS",
        9000..=9999 => "App",
        _ => "",
    }
}

// =============================================================================
// STANDARD COLORS (used across helper functions)
// =============================================================================

use presentar_core::Color;

/// Standard dim color for labels.
pub const DIM_COLOR: Color = Color {
    r: 0.3,
    g: 0.3,
    b: 0.3,
    a: 1.0,
};

/// Cyan color for cached memory.
pub const CACHED_COLOR: Color = Color {
    r: 0.3,
    g: 0.8,
    b: 0.9,
    a: 1.0,
};

/// Blue color for free memory.
pub const FREE_COLOR: Color = Color {
    r: 0.4,
    g: 0.4,
    b: 0.9,
    a: 1.0,
};

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

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

    // =========================================================================
    // format_bytes tests
    // =========================================================================

    #[test]
    fn test_format_bytes_zero() {
        assert_eq!(format_bytes(0), "0B");
    }

    #[test]
    fn test_format_bytes_single_byte() {
        assert_eq!(format_bytes(1), "1B");
    }

    #[test]
    fn test_format_bytes_under_1k() {
        assert_eq!(format_bytes(512), "512B");
        assert_eq!(format_bytes(1023), "1023B");
    }

    #[test]
    fn test_format_bytes_exactly_1k() {
        assert_eq!(format_bytes(1024), "1.0K");
    }

    #[test]
    fn test_format_bytes_kilobytes() {
        assert_eq!(format_bytes(1536), "1.5K");
        assert_eq!(format_bytes(10 * 1024), "10.0K");
        assert_eq!(format_bytes(512 * 1024), "512.0K");
    }

    #[test]
    fn test_format_bytes_exactly_1m() {
        assert_eq!(format_bytes(1024 * 1024), "1.0M");
    }

    #[test]
    fn test_format_bytes_megabytes() {
        assert_eq!(format_bytes(1024 * 1024 * 2), "2.0M");
        assert_eq!(format_bytes(1024 * 1024 + 512 * 1024), "1.5M");
        assert_eq!(format_bytes(100 * 1024 * 1024), "100.0M");
    }

    #[test]
    fn test_format_bytes_exactly_1g() {
        assert_eq!(format_bytes(1024 * 1024 * 1024), "1.0G");
    }

    #[test]
    fn test_format_bytes_gigabytes() {
        let gb = 1024 * 1024 * 1024;
        assert_eq!(format_bytes(gb + gb / 2), "1.5G");
        assert_eq!(format_bytes(8 * gb), "8.0G");
        assert_eq!(format_bytes(16 * gb), "16.0G");
    }

    #[test]
    fn test_format_bytes_exactly_1t() {
        let tb: u64 = 1024 * 1024 * 1024 * 1024;
        assert_eq!(format_bytes(tb), "1.0T");
    }

    #[test]
    fn test_format_bytes_terabytes() {
        let tb: u64 = 1024 * 1024 * 1024 * 1024;
        assert_eq!(format_bytes(2 * tb), "2.0T");
        assert_eq!(format_bytes(tb + tb / 2), "1.5T");
    }

    #[test]
    fn test_format_bytes_large_terabytes() {
        let tb: u64 = 1024 * 1024 * 1024 * 1024;
        assert_eq!(format_bytes(100 * tb), "100.0T");
    }

    #[test]
    fn test_format_bytes_typical_ram_sizes() {
        let gb = 1024 * 1024 * 1024;
        assert_eq!(format_bytes(4 * gb), "4.0G");
        assert_eq!(format_bytes(8 * gb), "8.0G");
        assert_eq!(format_bytes(16 * gb), "16.0G");
        assert_eq!(format_bytes(32 * gb), "32.0G");
        assert_eq!(format_bytes(64 * gb), "64.0G");
    }

    #[test]
    fn test_format_bytes_typical_disk_sizes() {
        let gb: u64 = 1024 * 1024 * 1024;
        let tb: u64 = gb * 1024;
        assert_eq!(format_bytes(256 * gb), "256.0G");
        assert_eq!(format_bytes(512 * gb), "512.0G");
        assert_eq!(format_bytes(tb), "1.0T");
        assert_eq!(format_bytes(2 * tb), "2.0T");
    }

    // =========================================================================
    // format_bytes_rate tests
    // =========================================================================

    #[test]
    fn test_format_bytes_rate_zero() {
        assert_eq!(format_bytes_rate(0.0), "0B");
    }

    #[test]
    fn test_format_bytes_rate_under_1k() {
        assert_eq!(format_bytes_rate(512.0), "512B");
        assert_eq!(format_bytes_rate(1023.5), "1024B");
    }

    #[test]
    fn test_format_bytes_rate_kilobytes() {
        assert_eq!(format_bytes_rate(1024.0), "1K");
        assert_eq!(format_bytes_rate(2048.0), "2K");
        assert_eq!(format_bytes_rate(100.0 * 1024.0), "100K");
    }

    #[test]
    fn test_format_bytes_rate_megabytes() {
        let mb = 1024.0 * 1024.0;
        assert_eq!(format_bytes_rate(mb), "1.0M");
        assert_eq!(format_bytes_rate(1.5 * mb), "1.5M");
        assert_eq!(format_bytes_rate(100.0 * mb), "100.0M");
    }

    #[test]
    fn test_format_bytes_rate_gigabytes() {
        let gb = 1024.0 * 1024.0 * 1024.0;
        assert_eq!(format_bytes_rate(gb), "1.0G");
        assert_eq!(format_bytes_rate(1.5 * gb), "1.5G");
        assert_eq!(format_bytes_rate(10.0 * gb), "10.0G");
    }

    #[test]
    fn test_format_bytes_rate_typical_network_speeds() {
        let mb = 1024.0 * 1024.0;
        // 100 Mbps = ~12.5 MB/s
        assert_eq!(format_bytes_rate(12.5 * mb), "12.5M");
        // 1 Gbps = ~125 MB/s
        assert_eq!(format_bytes_rate(125.0 * mb), "125.0M");
    }

    // =========================================================================
    // format_uptime tests
    // =========================================================================

    #[test]
    fn test_format_uptime_zero() {
        assert_eq!(format_uptime(0), "0m");
    }

    #[test]
    fn test_format_uptime_seconds_only() {
        assert_eq!(format_uptime(30), "0m");
        assert_eq!(format_uptime(59), "0m");
    }

    #[test]
    fn test_format_uptime_minutes() {
        assert_eq!(format_uptime(60), "1m");
        assert_eq!(format_uptime(120), "2m");
        assert_eq!(format_uptime(45 * 60), "45m");
        assert_eq!(format_uptime(59 * 60 + 59), "59m");
    }

    #[test]
    fn test_format_uptime_hours() {
        assert_eq!(format_uptime(3600), "1h 0m");
        assert_eq!(format_uptime(3600 + 30 * 60), "1h 30m");
        assert_eq!(format_uptime(5 * 3600 + 15 * 60), "5h 15m");
        assert_eq!(format_uptime(23 * 3600 + 59 * 60), "23h 59m");
    }

    #[test]
    fn test_format_uptime_days() {
        assert_eq!(format_uptime(86400), "1d 0h");
        assert_eq!(format_uptime(86400 + 3600), "1d 1h");
        assert_eq!(format_uptime(5 * 86400 + 3 * 3600), "5d 3h");
        assert_eq!(format_uptime(30 * 86400 + 12 * 3600), "30d 12h");
    }

    #[test]
    fn test_format_uptime_typical_values() {
        // Fresh boot
        assert_eq!(format_uptime(300), "5m");
        // Few hours
        assert_eq!(format_uptime(7200), "2h 0m");
        // A day
        assert_eq!(format_uptime(86400), "1d 0h");
        // A week
        assert_eq!(format_uptime(7 * 86400), "7d 0h");
        // A month
        assert_eq!(format_uptime(30 * 86400), "30d 0h");
    }

    #[test]
    fn test_format_uptime_ignores_extra_seconds() {
        // Seconds are dropped, only full minutes count
        assert_eq!(format_uptime(61), "1m");
        assert_eq!(format_uptime(119), "1m");
    }

    // =========================================================================
    // pressure_symbol tests
    // =========================================================================

    #[test]
    fn test_pressure_symbol_none() {
        assert_eq!(pressure_symbol(0.0), "");
        assert_eq!(pressure_symbol(0.5), "");
        assert_eq!(pressure_symbol(1.0), "");
    }

    #[test]
    fn test_pressure_symbol_low() {
        assert_eq!(pressure_symbol(1.5), "");
        assert_eq!(pressure_symbol(3.0), "");
        assert_eq!(pressure_symbol(5.0), "");
    }

    #[test]
    fn test_pressure_symbol_medium() {
        assert_eq!(pressure_symbol(5.1), "");
        assert_eq!(pressure_symbol(10.0), "");
        assert_eq!(pressure_symbol(20.0), "");
    }

    #[test]
    fn test_pressure_symbol_high() {
        assert_eq!(pressure_symbol(20.1), "");
        assert_eq!(pressure_symbol(30.0), "");
        assert_eq!(pressure_symbol(50.0), "");
    }

    #[test]
    fn test_pressure_symbol_critical() {
        assert_eq!(pressure_symbol(50.1), "▲▲");
        assert_eq!(pressure_symbol(75.0), "▲▲");
        assert_eq!(pressure_symbol(100.0), "▲▲");
    }

    #[test]
    fn test_pressure_symbol_boundary_values() {
        // Exact boundaries
        assert_eq!(pressure_symbol(1.0), ""); // <= 1.0
        assert_eq!(pressure_symbol(5.0), ""); // > 1.0, <= 5.0
        assert_eq!(pressure_symbol(20.0), ""); // > 5.0, <= 20.0
        assert_eq!(pressure_symbol(50.0), ""); // > 20.0, <= 50.0
    }

    // =========================================================================
    // port_to_service tests
    // =========================================================================

    #[test]
    fn test_port_to_service_ssh() {
        assert_eq!(port_to_service(22), "SSH");
    }

    #[test]
    fn test_port_to_service_http() {
        assert_eq!(port_to_service(80), "HTTP");
        assert_eq!(port_to_service(8080), "HTTP");
    }

    #[test]
    fn test_port_to_service_https() {
        assert_eq!(port_to_service(443), "HTTPS");
        assert_eq!(port_to_service(8443), "HTTPS");
    }

    #[test]
    fn test_port_to_service_dns() {
        assert_eq!(port_to_service(53), "DNS");
    }

    #[test]
    fn test_port_to_service_smtp() {
        assert_eq!(port_to_service(25), "SMTP");
    }

    #[test]
    fn test_port_to_service_ftp() {
        assert_eq!(port_to_service(21), "FTP");
    }

    #[test]
    fn test_port_to_service_databases() {
        assert_eq!(port_to_service(3306), "MySQL");
        assert_eq!(port_to_service(5432), "Pgsql");
        assert_eq!(port_to_service(6379), "Redis");
        assert_eq!(port_to_service(27017), "Mongo");
    }

    #[test]
    fn test_port_to_service_app_range() {
        assert_eq!(port_to_service(9000), "App");
        assert_eq!(port_to_service(9001), "App");
        assert_eq!(port_to_service(9500), "App");
        assert_eq!(port_to_service(9999), "App");
    }

    #[test]
    fn test_port_to_service_unknown() {
        assert_eq!(port_to_service(0), "");
        assert_eq!(port_to_service(1), "");
        assert_eq!(port_to_service(12345), "");
        assert_eq!(port_to_service(65535), "");
    }

    #[test]
    fn test_port_to_service_just_outside_app_range() {
        assert_eq!(port_to_service(8999), "");
        assert_eq!(port_to_service(10000), "");
    }

    // =========================================================================
    // Color constant tests
    // =========================================================================

    #[test]
    fn test_dim_color_is_gray() {
        assert_eq!(DIM_COLOR.r, 0.3);
        assert_eq!(DIM_COLOR.g, 0.3);
        assert_eq!(DIM_COLOR.b, 0.3);
        assert_eq!(DIM_COLOR.a, 1.0);
    }

    #[test]
    fn test_cached_color_is_cyan() {
        assert!(
            CACHED_COLOR.b > CACHED_COLOR.r,
            "Cached should be cyan (more blue than red)"
        );
        assert!(CACHED_COLOR.g > 0.7, "Cached should have high green");
    }

    #[test]
    fn test_free_color_is_blue() {
        assert!(FREE_COLOR.b > FREE_COLOR.r, "Free should be blue");
        assert!(
            FREE_COLOR.b > FREE_COLOR.g,
            "Free should be more blue than green"
        );
    }

    #[test]
    fn test_all_helper_colors_have_full_alpha() {
        assert_eq!(DIM_COLOR.a, 1.0);
        assert_eq!(CACHED_COLOR.a, 1.0);
        assert_eq!(FREE_COLOR.a, 1.0);
    }

    // =========================================================================
    // Edge case tests
    // =========================================================================

    #[test]
    fn test_format_bytes_max_u64() {
        // Should not panic and should return terabytes
        let result = format_bytes(u64::MAX);
        assert!(result.contains("T"), "Max u64 should be in terabytes");
    }

    #[test]
    fn test_format_bytes_rate_negative() {
        // Negative rates shouldn't happen but should be handled
        let result = format_bytes_rate(-100.0);
        assert!(result.contains("B"), "Negative should still format");
    }

    #[test]
    fn test_format_bytes_rate_infinity() {
        let result = format_bytes_rate(f64::INFINITY);
        assert!(
            result.contains("inf") || result.contains("G"),
            "Infinity should be handled"
        );
    }

    #[test]
    fn test_format_bytes_rate_nan() {
        let result = format_bytes_rate(f64::NAN);
        assert!(
            result.contains("NaN") || result.contains("nan"),
            "NaN should be handled"
        );
    }

    #[test]
    fn test_pressure_symbol_negative() {
        // Negative should be treated as "none"
        assert_eq!(pressure_symbol(-10.0), "");
    }

    #[test]
    fn test_pressure_symbol_very_high() {
        // Very high values should still be critical
        assert_eq!(pressure_symbol(1000.0), "▲▲");
    }
}