morpheus_bootloader/tui/iso_manager/
renderer.rs

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
//! ISO Manager Renderer
//!
//! Renders the ISO manager TUI components.

use super::state::{IsoManagerState, ViewMode};
use crate::tui::renderer::{
    Screen, EFI_BLACK, EFI_DARKGRAY, EFI_GREEN, EFI_LIGHTGREEN, EFI_RED, EFI_WHITE, EFI_YELLOW,
};

/// Box drawing characters (ASCII fallback for UEFI)
const BOX_H: char = '-';
const BOX_V: char = '|';
const BOX_TL: char = '+';
const BOX_TR: char = '+';
const BOX_BL: char = '+';
const BOX_BR: char = '+';

/// Render the ISO manager UI
pub fn render(screen: &mut Screen, state: &IsoManagerState) {
    render_header(screen);

    match state.mode {
        ViewMode::List => render_list(screen, state),
        ViewMode::Details => render_details(screen, state),
        ViewMode::ConfirmDelete => {
            render_list(screen, state);
            render_confirm_dialog(screen, "Delete ISO?", state.selected_name());
        }
        ViewMode::ConfirmBoot => {
            render_list(screen, state);
            render_confirm_dialog(screen, "Boot ISO?", state.selected_name());
        }
    }

    render_footer(screen, state);
}

fn render_header(screen: &mut Screen) {
    let width = screen.width();

    // Title bar
    screen.set_cursor(0, 0);
    screen.set_colors(EFI_BLACK, EFI_GREEN);

    let title = " ISO MANAGER ";
    let padding = (width - title.len()) / 2;

    for _ in 0..padding {
        screen.print_char(' ');
    }
    screen.print(title);
    for _ in 0..(width - padding - title.len()) {
        screen.print_char(' ');
    }

    // Subtitle
    screen.set_cursor(0, 1);
    screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
    screen.print("  Manage downloaded ISO images");

    // Separator
    screen.set_cursor(0, 2);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    for _ in 0..width {
        screen.print_char(BOX_H);
    }
}

fn render_list(screen: &mut Screen, state: &IsoManagerState) {
    let start_row = 4;

    if state.count == 0 {
        screen.set_cursor(2, start_row);
        screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
        screen.print("No ISOs stored. Use Distro Downloader to download ISOs.");
        return;
    }

    // Column headers
    screen.set_cursor(2, start_row);
    screen.set_colors(EFI_GREEN, EFI_BLACK);
    screen.print("  NAME                                    SIZE      CHUNKS  STATUS");

    screen.set_cursor(2, start_row + 1);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    for _ in 0..70 {
        screen.print_char(BOX_H);
    }

    // List ISOs
    for i in 0..state.count {
        let row = start_row + 2 + i;
        screen.set_cursor(2, row);

        // Selection indicator
        if i == state.selected {
            screen.set_colors(EFI_BLACK, EFI_GREEN);
            screen.print("> ");
        } else {
            screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
            screen.print("  ");
        }

        // Name (max 40 chars)
        let name =
            core::str::from_utf8(&state.names[i][..state.name_lens[i].min(40)]).unwrap_or("???");
        screen.print(name);

        // Padding after name
        for _ in name.len()..40 {
            screen.print_char(' ');
        }

        // Size
        screen.print("  ");
        print_size_mb(screen, state.sizes_mb[i]);

        // Chunks
        screen.print("   ");
        let chunks = state.chunk_counts[i];
        if chunks < 10 {
            screen.print_char(' ');
        }
        print_number(screen, chunks as u64);
        screen.print("     ");

        // Status
        if state.complete[i] {
            screen.set_colors(EFI_GREEN, EFI_BLACK);
            screen.print("Ready");
        } else {
            screen.set_colors(EFI_YELLOW, EFI_BLACK);
            screen.print("Incomplete");
        }

        // Reset selection highlighting
        if i == state.selected {
            screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
        }
    }
}

fn render_details(screen: &mut Screen, state: &IsoManagerState) {
    let start_row = 4;

    if state.count == 0 {
        return;
    }

    // Title
    screen.set_cursor(2, start_row);
    screen.set_colors(EFI_GREEN, EFI_BLACK);
    screen.print("ISO Details");

    // Box around details
    let box_left = 2;
    let box_width = 60;
    let box_top = start_row + 1;

    // Top border
    screen.set_cursor(box_left, box_top);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    screen.print_char(BOX_TL);
    for _ in 0..box_width - 2 {
        screen.print_char(BOX_H);
    }
    screen.print_char(BOX_TR);

    // Name row
    screen.set_cursor(box_left, box_top + 1);
    screen.print_char(BOX_V);
    screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
    screen.print(" Name: ");
    screen.set_colors(EFI_WHITE, EFI_BLACK);
    let name = state.selected_name();
    screen.print(name);
    for _ in (7 + name.len())..box_width - 1 {
        screen.print_char(' ');
    }
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    screen.print_char(BOX_V);

    // Size row
    screen.set_cursor(box_left, box_top + 2);
    screen.print_char(BOX_V);
    screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
    screen.print(" Size: ");
    screen.set_colors(EFI_WHITE, EFI_BLACK);
    print_size_mb(screen, state.selected_size_mb());
    screen.print(" MB");
    for _ in 0..35 {
        screen.print_char(' ');
    }
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    screen.print_char(BOX_V);

    // Chunks row
    screen.set_cursor(box_left, box_top + 3);
    screen.print_char(BOX_V);
    screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
    screen.print(" Chunks: ");
    screen.set_colors(EFI_WHITE, EFI_BLACK);
    print_number(screen, state.selected_chunks() as u64);
    screen.print(" partitions");
    for _ in 0..34 {
        screen.print_char(' ');
    }
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    screen.print_char(BOX_V);

    // Status row
    screen.set_cursor(box_left, box_top + 4);
    screen.print_char(BOX_V);
    screen.set_colors(EFI_LIGHTGREEN, EFI_BLACK);
    screen.print(" Status: ");
    if state.selected_complete() {
        screen.set_colors(EFI_GREEN, EFI_BLACK);
        screen.print("Ready to boot");
    } else {
        screen.set_colors(EFI_YELLOW, EFI_BLACK);
        screen.print("Download incomplete");
    }
    for _ in 0..30 {
        screen.print_char(' ');
    }
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    screen.print_char(BOX_V);

    // Bottom border
    screen.set_cursor(box_left, box_top + 5);
    screen.print_char(BOX_BL);
    for _ in 0..box_width - 2 {
        screen.print_char(BOX_H);
    }
    screen.print_char(BOX_BR);

    // Actions hint
    screen.set_cursor(box_left, box_top + 7);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    if state.selected_complete() {
        screen.print("[B] Boot   [D] Delete   [ESC] Back");
    } else {
        screen.print("[D] Delete   [ESC] Back");
    }
}

fn render_confirm_dialog(screen: &mut Screen, title: &str, item_name: &str) {
    let width = screen.width();
    let height = screen.height();

    let dialog_width = 50;
    let dialog_height = 7;
    let left = (width - dialog_width) / 2;
    let top = (height - dialog_height) / 2;

    // Shadow
    screen.set_colors(EFI_BLACK, EFI_DARKGRAY);
    for row in 1..=dialog_height {
        screen.set_cursor(left + dialog_width, top + row);
        screen.print_char(' ');
    }
    screen.set_cursor(left + 1, top + dialog_height);
    for _ in 0..dialog_width {
        screen.print_char(' ');
    }

    // Dialog background
    screen.set_colors(EFI_WHITE, EFI_RED);
    for row in 0..dialog_height {
        screen.set_cursor(left, top + row);
        for _ in 0..dialog_width {
            screen.print_char(' ');
        }
    }

    // Title
    screen.set_cursor(left + 2, top + 1);
    screen.set_colors(EFI_WHITE, EFI_RED);
    screen.print(title);

    // Item name
    screen.set_cursor(left + 2, top + 3);
    let max_name = item_name.len().min(dialog_width - 4);
    screen.print(&item_name[..max_name]);

    // Buttons
    screen.set_cursor(left + 2, top + 5);
    screen.set_colors(EFI_BLACK, EFI_WHITE);
    screen.print(" [Y]es ");
    screen.set_colors(EFI_WHITE, EFI_RED);
    screen.print("  ");
    screen.set_colors(EFI_BLACK, EFI_WHITE);
    screen.print(" [N]o ");
}

fn render_footer(screen: &mut Screen, state: &IsoManagerState) {
    let height = screen.height();
    let width = screen.width();

    // Error message (if any)
    if let Some(msg) = state.error_msg {
        screen.set_cursor(2, height - 3);
        screen.set_colors(EFI_RED, EFI_BLACK);
        screen.print("Error: ");
        screen.print(msg);
    }

    // Separator
    screen.set_cursor(0, height - 2);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);
    for _ in 0..width {
        screen.print_char(BOX_H);
    }

    // Help text
    screen.set_cursor(2, height - 1);
    screen.set_colors(EFI_DARKGRAY, EFI_BLACK);

    match state.mode {
        ViewMode::List => {
            if state.count > 0 {
                screen.print("[UP/DOWN] Select  [ENTER] Details  [B] Boot  [D] Delete  [R] Refresh  [ESC] Back");
            } else {
                screen.print("[ESC] Back to main menu");
            }
        }
        ViewMode::Details => {
            screen.print("[B] Boot  [D] Delete  [ESC] Back to list");
        }
        _ => {}
    }
}

/// Print a number (no heap allocation)
fn print_number(screen: &mut Screen, n: u64) {
    if n == 0 {
        screen.print_char('0');
        return;
    }

    let mut buf = [0u8; 20];
    let mut i = 0;
    let mut val = n;

    while val > 0 {
        buf[i] = b'0' + (val % 10) as u8;
        val /= 10;
        i += 1;
    }

    while i > 0 {
        i -= 1;
        screen.print_char(buf[i] as char);
    }
}

/// Print size in MB with formatting
fn print_size_mb(screen: &mut Screen, mb: u64) {
    if mb >= 1024 {
        // Show in GB
        let gb = mb / 1024;
        let frac = (mb % 1024) / 100; // One decimal place
        print_number(screen, gb);
        screen.print_char('.');
        print_number(screen, frac);
        screen.print(" GB");
    } else {
        print_number(screen, mb);
        screen.print(" MB");
    }
}