vm-curator 1.1.0

A TUI application to manage QEMU VM library
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
use ratatui::{
    prelude::*,
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
};

use crate::app::App;
use crate::config::Config;
use crate::vm::DiscoveredVm;

/// Menu item with name and description
#[derive(Debug, Clone)]
pub struct MenuItem {
    pub name: &'static str,
    pub description: &'static str,
    pub action: MenuAction,
}

/// Actions that can be performed from the management menu
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MenuAction {
    StopVm,
    BootOptions,
    Snapshots,
    UsbPassthrough,
    PciPassthrough,
    SharedFolders,
    NetworkSettings,
    MultiGpuPassthrough,
    SingleGpuPassthrough,
    ChangeDisplay,
    Toggle3dAccel,
    EditNotes,
    RenameVm,
    ResetVm,
    DeleteVm,
    EditRawConfig,
}

/// Get menu items based on config and VM state
pub fn get_menu_items(vm: &DiscoveredVm, config: &Config) -> Vec<MenuItem> {
    let mut items = vec![
        MenuItem {
            name: "Boot Options",
            description: "Normal, install, or custom ISO boot",
            action: MenuAction::BootOptions,
        },
        MenuItem {
            name: "Snapshots",
            description: "Create, restore, or delete snapshots",
            action: MenuAction::Snapshots,
        },
        MenuItem {
            name: "USB Passthrough",
            description: "Pass USB devices to the VM",
            action: MenuAction::UsbPassthrough,
        },
        MenuItem {
            name: "PCI Passthrough",
            description: "Pass PCI devices to the VM",
            action: MenuAction::PciPassthrough,
        },
        MenuItem {
            name: "Shared Folders",
            description: "Share host directories with the VM (9p)",
            action: MenuAction::SharedFolders,
        },
        MenuItem {
            name: "Network Settings",
            description: "Configure networking backend and port forwarding",
            action: MenuAction::NetworkSettings,
        },
    ];

    // Add Multi-GPU Passthrough option if enabled in settings
    if config.enable_multi_gpu_passthrough {
        items.push(MenuItem {
            name: "Multi-GPU Passthrough",
            description: "Pass a secondary GPU to the VM with Looking Glass",
            action: MenuAction::MultiGpuPassthrough,
        });
    }

    // Add Single GPU Passthrough option if enabled in settings
    if config.single_gpu_enabled {
        items.push(MenuItem {
            name: "Single GPU Passthrough",
            description: "Configure passthrough for your primary GPU",
            action: MenuAction::SingleGpuPassthrough,
        });
    }

    let gl_desc: &'static str = if vm.config.has_gl_acceleration() {
        "Currently ON - toggle off"
    } else {
        "Currently OFF - toggle on"
    };

    items.extend([
        MenuItem {
            name: "Change Display",
            description: "GTK, SDL, SPICE-app, or VNC output",
            action: MenuAction::ChangeDisplay,
        },
        MenuItem {
            name: "3D Acceleration (non-pass-through)",
            description: gl_desc,
            action: MenuAction::Toggle3dAccel,
        },
        MenuItem {
            name: "Edit Notes",
            description: "Add or edit personal notes for this VM",
            action: MenuAction::EditNotes,
        },
        MenuItem {
            name: "Rename VM",
            description: "Change the VM's display name",
            action: MenuAction::RenameVm,
        },
    ]);

    items.push(MenuItem {
        name: "Stop VM",
        description: "Shut down the running VM (ACPI poweroff)",
        action: MenuAction::StopVm,
    });

    // Add dangerous operations at the end
    items.extend([
        MenuItem {
            name: "Reset VM (recreate disk)",
            description: "Restore VM to fresh state",
            action: MenuAction::ResetVm,
        },
        MenuItem {
            name: "Delete VM",
            description: "Permanently remove this VM",
            action: MenuAction::DeleteVm,
        },
        MenuItem {
            name: "Edit Raw Configuration",
            description: "Edit the launch.sh script directly",
            action: MenuAction::EditRawConfig,
        },
    ]);

    // Check for GPU passthrough script
    let _has_gpu_script = vm.path.join("launch-with-gpu-passthrough.sh").exists();
    // Future: Add "Launch with GPU Passthrough" or "Remove GPU Passthrough" based on this

    items
}

/// Get the count of menu items (for navigation bounds)
pub fn menu_item_count(app: &App) -> usize {
    if let Some(vm) = app.selected_vm() {
        get_menu_items(vm, &app.config).len()
    } else {
        6 // Default count
    }
}

/// Default display options for VMs (used as fallback descriptions)
const DISPLAY_OPTIONS: &[(&str, &str)] = &[
    ("gtk", "GTK - Default windowed display"),
    ("sdl", "SDL - Better for 3D acceleration"),
    ("spice-app", "SPICE - Remote desktop (needs virt-viewer)"),
    ("vnc", "VNC - Network accessible display"),
    ("none", "None - Headless, no graphical output"),
];

/// Get dynamic display options based on detected emulator capabilities.
/// Falls back to DISPLAY_OPTIONS if detection is not available.
pub fn get_display_options(app: &App) -> Vec<(String, String)> {
    // Get the emulator for the currently selected VM
    let emulator = app
        .selected_vm()
        .map(|vm| vm.config.emulator.command())
        .unwrap_or("qemu-system-x86_64");

    let detected = app.get_display_options_for_emulator(emulator);

    // Map detected backends to (name, description) pairs using DISPLAY_OPTIONS for descriptions
    detected
        .iter()
        .map(|backend| {
            let desc = DISPLAY_OPTIONS
                .iter()
                .find(|(name, _)| *name == backend.as_str())
                .map(|(_, desc)| desc.to_string())
                .unwrap_or_else(|| format!("{} display", backend));
            (backend.clone(), desc)
        })
        .collect()
}

/// Render the management menu
pub fn render(app: &App, frame: &mut Frame) {
    let area = frame.area();

    // Get dynamic menu items
    let menu_items = if let Some(vm) = app.selected_vm() {
        get_menu_items(vm, &app.config)
    } else {
        Vec::new()
    };

    // Calculate dialog size - adjust height based on item count
    let dialog_width = 50.min(area.width.saturating_sub(4));
    let item_count = menu_items.len();
    let dialog_height = (6 + item_count * 2).min(area.height.saturating_sub(4) as usize) as u16;

    let dialog_area = centered_rect(dialog_width, dialog_height, area);

    // Clear the background
    frame.render_widget(Clear, dialog_area);

    let vm_name = app
        .selected_vm()
        .map(|vm| vm.display_name())
        .unwrap_or_else(|| "Unknown".to_string());

    let block = Block::default()
        .title(format!(" {} - Management ", vm_name))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    // Add horizontal margins
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(2), // Left margin
            Constraint::Min(1),    // Content
            Constraint::Length(2), // Right margin
        ])
        .split(inner);

    // Split content into padding, menu, and help
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Top padding
            Constraint::Min(4),    // Menu items
            Constraint::Length(2), // Help text
        ])
        .split(h_chunks[1]);

    // Create menu items with descriptions
    let items: Vec<ListItem> = menu_items
        .iter()
        .enumerate()
        .map(|(i, item)| {
            let style = if i == app.selected_menu_item {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::White)
            };

            let content = vec![
                Line::styled(format!("[{}] {}", i + 1, item.name), style),
                Line::styled(
                    format!("    {}", item.description),
                    Style::default().fg(Color::DarkGray),
                ),
            ];

            ListItem::new(content)
        })
        .collect();

    let mut state = ListState::default();
    state.select(Some(app.selected_menu_item));

    let list = List::new(items).highlight_symbol("> ");

    frame.render_stateful_widget(list, chunks[1], &mut state);

    // Help text
    let help = Paragraph::new("[Enter] Select  [Esc] Back")
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    frame.render_widget(help, chunks[2]);
}

/// Render boot options submenu
pub fn render_boot_options(app: &App, frame: &mut Frame) {
    let area = frame.area();
    let dialog_width = 50.min(area.width.saturating_sub(4));
    let dialog_height = 16.min(area.height.saturating_sub(4));

    let dialog_area = centered_rect(dialog_width, dialog_height, area);
    frame.render_widget(Clear, dialog_area);

    let block = Block::default()
        .title(" Boot Options ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    // Add horizontal margins
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(2), // Left margin
            Constraint::Min(1),    // Content
            Constraint::Length(2), // Right margin
        ])
        .split(inner);

    // Add top padding
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Top padding
            Constraint::Min(1),    // Content
        ])
        .split(h_chunks[1]);

    let boot_items = [
        ("Normal boot", "Start the VM normally"),
        ("Install mode", "Boot from installation media"),
        ("Boot with custom ISO", "Select an ISO file to boot"),
        (
            "Boot with recovery DMG",
            "Select a DMG file as recovery image",
        ),
        (
            "Boot with floppy image",
            "Select a floppy image (.img, .ima) to boot",
        ),
    ];

    let items: Vec<ListItem> = boot_items
        .iter()
        .enumerate()
        .map(|(i, (name, desc))| {
            let style = if i == app.selected_menu_item {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::White)
            };

            ListItem::new(vec![
                Line::styled(format!("[{}] {}", i + 1, name), style),
                Line::styled(
                    format!("    {}", desc),
                    Style::default().fg(Color::DarkGray),
                ),
            ])
        })
        .collect();

    let mut state = ListState::default();
    state.select(Some(app.selected_menu_item));

    let list = List::new(items);
    frame.render_stateful_widget(list, v_chunks[1], &mut state);
}

/// Render display options submenu
pub fn render_display_options(app: &App, frame: &mut Frame) {
    let area = frame.area();
    let dialog_width = 50.min(area.width.saturating_sub(4));
    let dialog_height = 16.min(area.height.saturating_sub(4));

    let dialog_area = centered_rect(dialog_width, dialog_height, area);
    frame.render_widget(Clear, dialog_area);

    // Get current display setting from VM
    let current_display = app
        .selected_vm()
        .map(|vm| extract_display_from_script(&vm.config.raw_script))
        .unwrap_or_else(|| "gtk".to_string());

    let block = Block::default()
        .title(format!(" Display Options (current: {}) ", current_display))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    // Add horizontal margins
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(2), // Left margin
            Constraint::Min(1),    // Content
            Constraint::Length(2), // Right margin
        ])
        .split(inner);

    // Add top padding and help area
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Top padding
            Constraint::Min(1),    // Content
            Constraint::Length(2), // Help
        ])
        .split(h_chunks[1]);

    let display_options = get_display_options(app);

    let items: Vec<ListItem> = display_options
        .iter()
        .enumerate()
        .map(|(i, (name, desc))| {
            let is_current = *name == current_display;
            let style = if i == app.selected_menu_item {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else if is_current {
                Style::default().fg(Color::Green)
            } else {
                Style::default().fg(Color::White)
            };

            let marker = if is_current { " *" } else { "" };

            ListItem::new(vec![
                Line::styled(format!("[{}] {}{}", i + 1, name, marker), style),
                Line::styled(
                    format!("    {}", desc),
                    Style::default().fg(Color::DarkGray),
                ),
            ])
        })
        .collect();

    let mut state = ListState::default();
    state.select(Some(app.selected_menu_item));

    let list = List::new(items);
    frame.render_stateful_widget(list, v_chunks[1], &mut state);

    // Help text
    let help = Paragraph::new("[Enter] Select  [Esc] Back")
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    frame.render_widget(help, v_chunks[2]);
}

/// Extract display setting from launch script
fn extract_display_from_script(script: &str) -> String {
    // Look for -display X pattern
    if let Some(pos) = script.find("-display ") {
        let rest = &script[pos + 9..];
        // Find the display value (ends at space, comma, or backslash)
        let end = rest
            .find(|c: char| c.is_whitespace() || c == ',' || c == '\\')
            .unwrap_or(rest.len());
        let display = rest[..end].trim();
        // Handle gl=on suffix
        if let Some(comma_pos) = display.find(',') {
            return display[..comma_pos].to_string();
        }
        return display.to_string();
    }
    "gtk".to_string() // Default
}

/// Render snapshot management submenu
pub fn render_snapshots(app: &App, frame: &mut Frame) {
    use ratatui::widgets::Wrap;

    let area = frame.area();
    let dialog_width = 55.min(area.width.saturating_sub(4));
    let dialog_height = 18.min(area.height.saturating_sub(4));

    let dialog_area = centered_rect(dialog_width, dialog_height, area);
    frame.render_widget(Clear, dialog_area);

    let supports_snapshots = app
        .selected_vm()
        .map(|vm| vm.config.supports_snapshots())
        .unwrap_or(false);

    let title = if supports_snapshots {
        format!(" Snapshots ({}) ", app.snapshots.len())
    } else {
        " Snapshots (not supported) ".to_string()
    };

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    // Add horizontal margins
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(2), // Left margin
            Constraint::Min(1),    // Content
            Constraint::Length(2), // Right margin
        ])
        .split(inner);

    // Add top padding
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Top padding
            Constraint::Min(1),    // Content
        ])
        .split(h_chunks[1]);

    let content_area = v_chunks[1];

    if !supports_snapshots {
        let msg = Paragraph::new("This VM uses a raw disk image which doesn't support snapshots.\n\nOnly qcow2 format disks support snapshots.")
            .style(Style::default().fg(Color::Yellow))
            .wrap(Wrap { trim: false });
        frame.render_widget(msg, content_area);
        return;
    }

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(4),
            Constraint::Length(2),
        ])
        .split(content_area);

    // Action buttons
    let actions = Paragraph::new(vec![Line::from(vec![
        Span::styled("[c]", Style::default().fg(Color::Yellow)),
        Span::raw(" Create new snapshot"),
    ])]);
    frame.render_widget(actions, chunks[0]);

    // Snapshot list
    if app.snapshots.is_empty() {
        let msg = Paragraph::new("No snapshots yet.")
            .style(Style::default().fg(Color::DarkGray))
            .alignment(Alignment::Center);
        frame.render_widget(msg, chunks[1]);
    } else {
        let items: Vec<ListItem> = app
            .snapshots
            .iter()
            .enumerate()
            .map(|(i, snap)| {
                let style = if i == app.selected_snapshot {
                    Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(Color::White)
                };

                ListItem::new(vec![
                    Line::styled(format!("  {}", snap.name), style),
                    Line::styled(
                        format!("    {} - {}", snap.date, snap.size),
                        Style::default().fg(Color::DarkGray),
                    ),
                ])
            })
            .collect();

        let mut state = ListState::default();
        state.select(Some(app.selected_snapshot));

        let list = List::new(items).highlight_symbol("> ");
        frame.render_stateful_widget(list, chunks[1], &mut state);
    }

    // Help
    let help = Paragraph::new("[r] Restore  [d] Delete  [Esc] Back")
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    frame.render_widget(help, chunks[2]);
}

fn centered_rect(width: u16, height: u16, area: Rect) -> Rect {
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    Rect::new(x, y, width, height)
}