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
//! GPU Compute Monitor Example
//!
//! Demonstrates GPU utilization monitoring for CUDA/ML workloads
//! with memory, compute, and temperature visualization.
//!
//! Run with: cargo run -p presentar-terminal --example gpu_compute

use presentar_core::{Canvas, Color, Point, Rect, TextStyle, Widget};
use presentar_terminal::direct::{CellBuffer, DiffRenderer, DirectTerminalCanvas};
use presentar_terminal::{BrailleGraph, ColorMode, GraphMode};

fn main() {
    println!("=== GPU Compute Monitor ===\n");

    // Simulate GPU metrics
    let gpus = vec![
        GpuInfo::new(0, "NVIDIA RTX 4090", 89.5, 18.2, 24.0, 72, 350, 450, 2520),
        GpuInfo::new(1, "NVIDIA RTX 4090", 92.3, 20.1, 24.0, 78, 380, 450, 2505),
        GpuInfo::new(2, "NVIDIA RTX 4090", 45.2, 12.8, 24.0, 58, 220, 450, 2490),
        GpuInfo::new(3, "NVIDIA RTX 4090", 78.6, 16.5, 24.0, 68, 310, 450, 2510),
    ];

    let compute_history = simulate_compute_usage(60);
    let memory_history = simulate_memory_usage(60);
    let power_history = simulate_power(60);

    // Create buffer
    let mut buffer = CellBuffer::new(80, 24);
    let mut renderer = DiffRenderer::with_color_mode(ColorMode::TrueColor);

    {
        let mut canvas = DirectTerminalCanvas::new(&mut buffer);

        // Background
        canvas.fill_rect(
            Rect::new(0.0, 0.0, 80.0, 24.0),
            Color::new(0.02, 0.02, 0.05, 1.0),
        );

        // Title
        let title_style = TextStyle {
            color: Color::new(0.4, 0.9, 0.4, 1.0),
            ..Default::default()
        };
        canvas.draw_text(
            "GPU Compute Cluster Monitor",
            Point::new(2.0, 1.0),
            &title_style,
        );

        // Compute utilization graph
        draw_utilization_graph(
            &mut canvas,
            &compute_history,
            Rect::new(2.0, 3.0, 50.0, 5.0),
        );

        // Memory usage graph
        draw_memory_graph(
            &mut canvas,
            &memory_history,
            Rect::new(54.0, 3.0, 24.0, 5.0),
        );

        // GPU cards info
        draw_gpu_cards(&mut canvas, &gpus, 2.0, 9.0);

        // Power consumption
        draw_power_graph(&mut canvas, &power_history, Rect::new(2.0, 17.0, 50.0, 4.0));

        // Summary stats
        draw_cluster_summary(&mut canvas, &gpus, 54.0, 17.0);

        // Footer
        draw_footer(&mut canvas);
    }

    // Render
    let mut output = Vec::with_capacity(8192);
    let cells_written = renderer.flush(&mut buffer, &mut output).unwrap();

    println!("Buffer: {}x{}", buffer.width(), buffer.height());
    println!("Cells written: {}", cells_written);
    println!("Output bytes: {}\n", output.len());

    println!("Rendered output:");
    println!("{}", "".repeat(82));
    std::io::Write::write_all(&mut std::io::stdout(), &output).unwrap();
    println!();
    println!("{}", "".repeat(82));
}

struct GpuInfo {
    id: u32,
    name: String,
    compute_pct: f64,
    mem_used: f64,
    mem_total: f64,
    temp: u32,
    power: u32,
    power_limit: u32,
    clock_mhz: u32,
}

impl GpuInfo {
    fn new(
        id: u32,
        name: &str,
        compute: f64,
        mem_used: f64,
        mem_total: f64,
        temp: u32,
        power: u32,
        power_limit: u32,
        clock: u32,
    ) -> Self {
        Self {
            id,
            name: name.to_string(),
            compute_pct: compute,
            mem_used,
            mem_total,
            temp,
            power,
            power_limit,
            clock_mhz: clock,
        }
    }

    fn compute_color(&self) -> Color {
        if self.compute_pct > 90.0 {
            Color::new(0.3, 1.0, 0.5, 1.0) // Green - fully utilized
        } else if self.compute_pct > 70.0 {
            Color::new(0.9, 0.9, 0.3, 1.0) // Yellow - good
        } else if self.compute_pct > 30.0 {
            Color::new(0.9, 0.6, 0.3, 1.0) // Orange - underutilized
        } else {
            Color::new(0.5, 0.5, 0.5, 1.0) // Gray - idle
        }
    }

    fn temp_color(&self) -> Color {
        if self.temp > 80 {
            Color::new(1.0, 0.3, 0.3, 1.0) // Red - critical
        } else if self.temp > 70 {
            Color::new(1.0, 0.7, 0.2, 1.0) // Orange - warm
        } else {
            Color::new(0.3, 0.8, 1.0, 1.0) // Blue - cool
        }
    }
}

fn draw_utilization_graph(canvas: &mut DirectTerminalCanvas<'_>, history: &[f64], bounds: Rect) {
    let label_style = TextStyle {
        color: Color::new(0.6, 0.6, 0.6, 1.0),
        ..Default::default()
    };
    canvas.draw_text(
        "Cluster Compute Utilization",
        Point::new(bounds.x, bounds.y),
        &label_style,
    );

    let current = history.last().copied().unwrap_or(0.0);
    let color = if current > 80.0 {
        Color::new(0.3, 1.0, 0.5, 1.0)
    } else if current > 50.0 {
        Color::new(0.9, 0.9, 0.3, 1.0)
    } else {
        Color::new(0.9, 0.6, 0.3, 1.0)
    };

    let value_style = TextStyle {
        color,
        ..Default::default()
    };
    canvas.draw_text(
        &format!("{:5.1}%", current),
        Point::new(bounds.x + 30.0, bounds.y),
        &value_style,
    );

    let mut graph = BrailleGraph::new(history.to_vec())
        .with_color(color)
        .with_range(0.0, 100.0)
        .with_mode(GraphMode::Braille);

    graph.layout(Rect::new(
        bounds.x,
        bounds.y + 1.0,
        bounds.width,
        bounds.height - 1.0,
    ));
    graph.paint(canvas);
}

fn draw_memory_graph(canvas: &mut DirectTerminalCanvas<'_>, history: &[f64], bounds: Rect) {
    let label_style = TextStyle {
        color: Color::new(0.6, 0.6, 0.6, 1.0),
        ..Default::default()
    };
    canvas.draw_text("VRAM", Point::new(bounds.x, bounds.y), &label_style);

    let current = history.last().copied().unwrap_or(0.0);
    let total = 96.0; // 4x 24GB
    let _pct = (current / total) * 100.0;

    let color = Color::new(0.6, 0.3, 1.0, 1.0);
    let value_style = TextStyle {
        color,
        ..Default::default()
    };
    canvas.draw_text(
        &format!("{:.0}/{:.0}GB", current, total),
        Point::new(bounds.x + 6.0, bounds.y),
        &value_style,
    );

    let pct_history: Vec<f64> = history.iter().map(|&v| (v / total) * 100.0).collect();
    let mut graph = BrailleGraph::new(pct_history)
        .with_color(color)
        .with_range(0.0, 100.0)
        .with_mode(GraphMode::Block);

    graph.layout(Rect::new(
        bounds.x,
        bounds.y + 1.0,
        bounds.width,
        bounds.height - 1.0,
    ));
    graph.paint(canvas);
}

fn draw_gpu_cards(canvas: &mut DirectTerminalCanvas<'_>, gpus: &[GpuInfo], x: f32, y: f32) {
    let header_style = TextStyle {
        color: Color::new(0.5, 0.5, 0.5, 1.0),
        ..Default::default()
    };
    canvas.draw_text(
        "GPU  Model              Compute    Memory        Temp   Power    Clock",
        Point::new(x, y),
        &header_style,
    );
    canvas.draw_text(&"".repeat(76), Point::new(x, y + 1.0), &header_style);

    for (i, gpu) in gpus.iter().enumerate() {
        let row_y = y + 2.0 + i as f32;

        // GPU ID
        let id_style = TextStyle {
            color: Color::new(0.9, 0.9, 0.9, 1.0),
            ..Default::default()
        };
        canvas.draw_text(&format!("[{}]", gpu.id), Point::new(x, row_y), &id_style);

        // Model name (truncated)
        let name_style = TextStyle {
            color: Color::new(0.7, 0.7, 0.7, 1.0),
            ..Default::default()
        };
        let short_name: String = gpu.name.chars().take(16).collect();
        canvas.draw_text(
            &format!("{:<16}", short_name),
            Point::new(x + 5.0, row_y),
            &name_style,
        );

        // Compute usage with bar
        let compute_style = TextStyle {
            color: gpu.compute_color(),
            ..Default::default()
        };
        let bar_width = 8;
        let filled = ((gpu.compute_pct / 100.0) * bar_width as f64).round() as usize;
        let mut bar = String::with_capacity(bar_width);
        for j in 0..bar_width {
            bar.push(if j < filled { '' } else { '' });
        }
        canvas.draw_text(&bar, Point::new(x + 22.0, row_y), &compute_style);
        canvas.draw_text(
            &format!("{:>3.0}%", gpu.compute_pct),
            Point::new(x + 31.0, row_y),
            &compute_style,
        );

        // Memory
        let _mem_pct = (gpu.mem_used / gpu.mem_total) * 100.0;
        let mem_style = TextStyle {
            color: Color::new(0.6, 0.3, 1.0, 1.0),
            ..Default::default()
        };
        canvas.draw_text(
            &format!("{:.0}/{:.0}GB", gpu.mem_used, gpu.mem_total),
            Point::new(x + 37.0, row_y),
            &mem_style,
        );

        // Temperature
        let temp_style = TextStyle {
            color: gpu.temp_color(),
            ..Default::default()
        };
        canvas.draw_text(
            &format!("{:>3}°C", gpu.temp),
            Point::new(x + 50.0, row_y),
            &temp_style,
        );

        // Power
        let power_style = TextStyle {
            color: Color::new(0.9, 0.7, 0.3, 1.0),
            ..Default::default()
        };
        canvas.draw_text(
            &format!("{:>3}/{}W", gpu.power, gpu.power_limit),
            Point::new(x + 57.0, row_y),
            &power_style,
        );

        // Clock
        let clock_style = TextStyle {
            color: Color::new(0.7, 0.7, 0.7, 1.0),
            ..Default::default()
        };
        canvas.draw_text(
            &format!("{}MHz", gpu.clock_mhz),
            Point::new(x + 68.0, row_y),
            &clock_style,
        );
    }
}

fn draw_power_graph(canvas: &mut DirectTerminalCanvas<'_>, history: &[f64], bounds: Rect) {
    let label_style = TextStyle {
        color: Color::new(0.6, 0.6, 0.6, 1.0),
        ..Default::default()
    };
    canvas.draw_text(
        "Total Power Draw",
        Point::new(bounds.x, bounds.y),
        &label_style,
    );

    let current = history.last().copied().unwrap_or(0.0);
    let max_power = 1800.0; // 4x 450W

    let color = Color::new(0.9, 0.7, 0.3, 1.0);
    let value_style = TextStyle {
        color,
        ..Default::default()
    };
    canvas.draw_text(
        &format!("{:.0}W / {:.0}W", current, max_power),
        Point::new(bounds.x + 20.0, bounds.y),
        &value_style,
    );

    let mut graph = BrailleGraph::new(history.to_vec())
        .with_color(color)
        .with_range(0.0, max_power)
        .with_mode(GraphMode::Braille);

    graph.layout(Rect::new(
        bounds.x,
        bounds.y + 1.0,
        bounds.width,
        bounds.height - 1.0,
    ));
    graph.paint(canvas);
}

fn draw_cluster_summary(canvas: &mut DirectTerminalCanvas<'_>, gpus: &[GpuInfo], x: f32, y: f32) {
    let label_style = TextStyle {
        color: Color::new(0.5, 0.5, 0.5, 1.0),
        ..Default::default()
    };
    let value_style = TextStyle {
        color: Color::new(0.8, 0.8, 0.8, 1.0),
        ..Default::default()
    };

    let avg_compute: f64 = gpus.iter().map(|g| g.compute_pct).sum::<f64>() / gpus.len() as f64;
    let total_mem: f64 = gpus.iter().map(|g| g.mem_used).sum();
    let max_temp: u32 = gpus.iter().map(|g| g.temp).max().unwrap_or(0);
    let total_power: u32 = gpus.iter().map(|g| g.power).sum();

    canvas.draw_text("Cluster Stats:", Point::new(x, y), &label_style);
    canvas.draw_text(
        &format!("Avg Util: {:.0}%", avg_compute),
        Point::new(x, y + 1.0),
        &value_style,
    );
    canvas.draw_text(
        &format!("VRAM: {:.0} GB", total_mem),
        Point::new(x, y + 2.0),
        &value_style,
    );
    canvas.draw_text(
        &format!("Max Temp: {}°C", max_temp),
        Point::new(x, y + 3.0),
        &value_style,
    );
    canvas.draw_text(
        &format!("Power: {} W", total_power),
        Point::new(x, y + 4.0),
        &value_style,
    );
}

fn draw_footer(canvas: &mut DirectTerminalCanvas<'_>) {
    let key_style = TextStyle {
        color: Color::new(0.4, 0.4, 0.4, 1.0),
        ..Default::default()
    };
    canvas.draw_text(
        "[q] quit  [r] refresh  [p] processes  [f] fans  [h] help",
        Point::new(2.0, 22.0),
        &key_style,
    );
}

fn simulate_compute_usage(count: usize) -> Vec<f64> {
    (0..count)
        .map(|i| {
            let base = 75.0 + 15.0 * (i as f64 / 10.0).sin();
            let noise = ((i * 7919) % 20) as f64;
            (base + noise).clamp(20.0, 98.0)
        })
        .collect()
}

fn simulate_memory_usage(count: usize) -> Vec<f64> {
    (0..count)
        .map(|i| {
            let base = 65.0 + 10.0 * (i as f64 / 15.0).sin();
            let noise = ((i * 6971) % 10) as f64;
            base + noise
        })
        .collect()
}

fn simulate_power(count: usize) -> Vec<f64> {
    (0..count)
        .map(|i| {
            let base = 1200.0 + 200.0 * (i as f64 / 12.0).sin();
            let noise = ((i * 1103) % 100) as f64;
            (base + noise).clamp(400.0, 1700.0)
        })
        .collect()
}