devsandbox 0.3.4

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
//! Dashboard state machine. Deliberately free of terminal I/O so key handling
//! and selection logic stay unit-testable; `mod.rs` owns the crossterm/ratatui
//! side and feeds decoded key events in here.

use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;

use super::data::{visible_nodes, Node, Snapshot};
use super::procs::ProcState;
use super::prompt::{Prompt, PromptAction};
use super::term::TermTabs;

mod actions;
mod command_line;
mod procs;
mod terminal;
#[cfg(test)]
mod test_support;
mod tree;
mod view;

pub use procs::{PendingSignal, Signal};
pub use view::{ConfigView, Modal, Pane, Side, TextModal};
use view::{col_near, divider_pct};

/// The three top-level views.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Tab {
    Instances,
    Services,
    Ports,
}

impl Tab {
    pub const ALL: [Tab; 3] = [Tab::Instances, Tab::Services, Tab::Ports];

    pub fn title(self) -> &'static str {
        match self {
            Tab::Instances => "Instances",
            Tab::Services => "Services",
            Tab::Ports => "Ports",
        }
    }

    fn index(self) -> usize {
        match self {
            Tab::Instances => 0,
            Tab::Services => 1,
            Tab::Ports => 2,
        }
    }
}

/// Where keystrokes go. `Dashboard` is the classic table/tree navigation;
/// `Terminal` forwards nearly every key to the active integrated terminal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Focus {
    Dashboard,
    Terminal,
}

/// One row of the Ports tab: a live forward, projected from the forwarder's
/// `ForwardStatus` by the event loop (step 10). Plain data with no dependency on
/// the unix-only forward module, so [`App`] compiles on every platform.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PortRow {
    /// Stable forward id, used to target a `d` (stop) at the right forward.
    pub id: u64,
    /// Host side, e.g. `"127.0.0.1:3000"`.
    pub local: String,
    /// Route label, e.g. `"api:3000"` or `"postgres:5432 (via instance api)"`.
    /// Already carries the `(via …)` suffix, so the table needs no VIA column.
    pub target: String,
    /// Listening process (`node (pid 412)`), when known.
    pub process: Option<String>,
    /// Coarse state: `"active"`, `"connecting"`, or `"error: …"`.
    pub state: String,
    /// Open connection count.
    pub conns: usize,
}

/// A forward the user asked for, for the event loop (step 10) to start on its
/// worker thread. Mirrors the CLI's `(instance, service, address, spec)` inputs;
/// `spec` is a validated `[host:]port` string.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PortRequest {
    pub instance: String,
    pub service: Option<String>,
    pub address: Option<String>,
    pub spec: String,
}

pub struct App {
    pub dir: PathBuf,
    pub tab: Tab,
    /// Selected row per tab, indexed by `Tab::index`.
    selected: [usize; 3],
    /// Collapsed tree groups on the Instances tab, keyed by sandbox name (and
    /// [`ORPHANS_NAME`](super::data::ORPHANS_NAME) for the orphan group). Empty
    /// means all expanded; survives snapshot refreshes.
    collapsed: BTreeSet<String>,
    /// Instances (by name) whose process layer is expanded on the Instances tab.
    /// Default empty (all collapsed); survives snapshot refreshes.
    expanded_procs: BTreeSet<String>,
    /// Cached process state per instance name. Kept across collapse (stale is
    /// fine); a fetch overwrites only the keys it fetched. Absent = not yet
    /// fetched, which renders as a placeholder row.
    pub procs: BTreeMap<String, ProcState>,
    /// Set when an instance's procs are freshly expanded so the event loop
    /// fetches immediately instead of waiting for the next proc tick; the loop
    /// clears it.
    pub needs_proc_fetch: bool,
    /// Latest data collected off-thread; `None` until the first snapshot lands.
    pub snapshot: Option<Snapshot>,
    /// Active overlay, if any.
    pub modal: Modal,
    /// Command prompt, when open (`:`). Occupies the bottom bar and swallows keys.
    pub prompt: Option<Prompt>,
    /// Action awaiting execution by the event loop (it owns the terminal
    /// suspend + command call, keeping [`App`] I/O-free).
    pub pending_action: Option<PromptAction>,
    /// Instance name whose stop the event loop should spawn on a background
    /// thread (the `s` shortcut on a running instance; runs without suspending
    /// the TUI).
    pub pending_stop: Option<String>,
    /// Instances with a stop in flight, so repeated `s` presses don't spawn a
    /// second stop for the same instance. Cleared by the loop on completion.
    pub stopping: BTreeSet<String>,
    /// Instance name whose start the event loop should spawn on a background
    /// thread (the `s` shortcut on an exited instance).
    pub pending_start: Option<String>,
    /// Instances with a start in flight (same dedup rule as `stopping`).
    pub starting: BTreeSet<String>,
    /// A `kill` the event loop should spawn on a background thread (the
    /// SIGTERM/SIGKILL shortcuts on a process row). Runs without suspending.
    pub pending_signal: Option<PendingSignal>,
    /// Live forwards shown on the Ports tab, fed by the forwarder worker (step
    /// 10). Empty until the worker reports; set via [`Self::set_ports`].
    pub ports: Vec<PortRow>,
    /// A forward the event loop should start on its worker thread (the `p`
    /// shortcut / `port` prompt). Consumed by step 10; sits here until then.
    pub pending_port: Option<PortRequest>,
    /// Id of a forward the event loop should stop (the `d` shortcut on the Ports
    /// tab). Consumed by step 10.
    pub pending_unport: Option<u64>,
    /// One-line status shown in the help-bar area (e.g. `code` launch outcome).
    pub status: Option<String>,
    /// True while the config-modal divider is being dragged with the mouse.
    dragging_divider: bool,
    /// Whether keys drive the dashboard or the active terminal.
    pub focus: Focus,
    /// Open integrated-terminal tabs, shared across both top-level tabs. Empty
    /// until the user opens one with `t`.
    pub terms: TermTabs,
    pub should_quit: bool,
}

impl App {
    pub fn new(dir: PathBuf) -> Self {
        Self {
            dir,
            tab: Tab::Instances,
            selected: [0, 0, 0],
            collapsed: BTreeSet::new(),
            expanded_procs: BTreeSet::new(),
            procs: BTreeMap::new(),
            needs_proc_fetch: false,
            snapshot: None,
            modal: Modal::None,
            prompt: None,
            pending_action: None,
            pending_stop: None,
            stopping: BTreeSet::new(),
            pending_start: None,
            starting: BTreeSet::new(),
            pending_signal: None,
            ports: Vec::new(),
            pending_port: None,
            pending_unport: None,
            status: None,
            dragging_divider: false,
            focus: Focus::Dashboard,
            terms: TermTabs::default(),
            should_quit: false,
        }
    }

    /// Install a freshly collected snapshot and re-clamp the Instances selection
    /// in case rows shrank. I/O-free: the caller does the collecting.
    pub fn set_snapshot(&mut self, snapshot: Snapshot) {
        self.snapshot = Some(snapshot);
        self.clamp_selection();
    }

    /// Install the forwarder's latest rows and re-clamp the Ports selection in
    /// case it shrank. I/O-free: the event loop (step 10) does the collecting.
    #[cfg_attr(not(unix), allow(dead_code))] // fed by the unix-only forward worker
    pub fn set_ports(&mut self, ports: Vec<PortRow>) {
        self.ports = ports;
        self.clamp_selection();
    }

    /// Clamp every tab's selection into `[0, row_count)` (or 0 when empty).
    fn clamp_selection(&mut self) {
        for tab in Tab::ALL {
            let rows = self.row_count_for(tab);
            let slot = tab.index();
            if rows == 0 {
                self.selected[slot] = 0;
            } else if self.selected[slot] >= rows {
                self.selected[slot] = rows - 1;
            }
        }
    }

    /// Selected row index for the active tab.
    pub fn selected(&self) -> usize {
        self.selected[self.tab.index()]
    }

    /// Row count for the active tab. Zero means "nothing to select".
    fn row_count(&self) -> usize {
        self.row_count_for(self.tab)
    }

    fn row_count_for(&self, tab: Tab) -> usize {
        match tab {
            Tab::Instances => self.visible_nodes().len(),
            Tab::Services => self.snapshot.as_ref().map_or(0, |s| s.services.len()),
            Tab::Ports => self.ports.len(),
        }
    }

    /// Flattened Instances-tree nodes for the current snapshot + collapse state.
    /// Empty until a snapshot lands. Cheap; recomputed on demand.
    pub fn visible_nodes(&self) -> Vec<Node> {
        match &self.snapshot {
            Some(s) => visible_nodes(
                &s.sandboxes,
                &s.instances,
                &self.collapsed,
                &self.expanded_procs,
                &self.procs,
            ),
            None => Vec::new(),
        }
    }

    /// Whether a tree group (sandbox name /
    /// [`ORPHANS_NAME`](super::data::ORPHANS_NAME)) is collapsed. Used by the
    /// renderer to pick the ▸/▾ marker.
    pub fn is_collapsed_group(&self, key: &str) -> bool {
        self.collapsed.contains(key)
    }

    /// The tree node under the Instances-tab cursor, if any.
    fn selected_node(&self) -> Option<Node> {
        self.visible_nodes()
            .get(self.selected[Tab::Instances.index()])
            .copied()
    }

    pub fn next_tab(&mut self) {
        self.tab = match self.tab {
            Tab::Instances => Tab::Services,
            Tab::Services => Tab::Ports,
            Tab::Ports => Tab::Instances,
        };
    }

    pub fn prev_tab(&mut self) {
        self.tab = match self.tab {
            Tab::Instances => Tab::Ports,
            Tab::Services => Tab::Instances,
            Tab::Ports => Tab::Services,
        };
    }

    /// Apply a key event to the state. No terminal I/O here (the modal open path
    /// reads config/fs, which is local and user-triggered — see [`Self::open_config`]).
    pub fn on_key(&mut self, key: KeyEvent) {
        // The prompt swallows every key while open, ahead of the modal and the
        // dashboard bindings.
        if self.prompt.is_some() {
            self.on_key_prompt(key);
            return;
        }
        // The modal swallows every key while it is up; the tab bar and tables
        // must not react underneath it.
        if !matches!(self.modal, Modal::None) {
            self.on_key_modal(key);
            return;
        }
        // A focused terminal is next in line, ahead of the dashboard bindings:
        // nearly every key belongs to the shell, not the tables.
        if self.focus == Focus::Terminal {
            self.on_key_terminal(key);
            return;
        }
        // Any dashboard key dismisses a lingering status line.
        self.status = None;
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        // A selected process row has its own action set: signals only. The
        // instance-forwarding shortcuts (rename/vscode/stop/logs/config/terminal)
        // are disabled here, leaving navigation and the SIGTERM/SIGKILL keys.
        let on_proc = self.tab == Tab::Instances
            && matches!(self.selected_node(), Some(Node::Proc { .. }));
        match key.code {
            KeyCode::Char('q') => self.should_quit = true,
            KeyCode::Char('c') if ctrl => self.should_quit = true,
            KeyCode::Char(':') => self.open_prompt(),
            KeyCode::Tab => self.next_tab(),
            KeyCode::BackTab => self.prev_tab(),
            KeyCode::Char('1') => self.tab = Tab::Instances,
            KeyCode::Char('2') => self.tab = Tab::Services,
            KeyCode::Char('3') => self.tab = Tab::Ports,
            // Enter a terminal: ctrl-] or F12. No-op with a status hint when none
            // are open.
            KeyCode::Char(']') if ctrl => self.enter_terminal(),
            KeyCode::F(12) => self.enter_terminal(),
            // Process-row signals; `t` doubles as SIGTERM there, otherwise it
            // opens a terminal.
            KeyCode::Char('t') if on_proc => self.signal_selected_proc(Signal::Term),
            KeyCode::Char('K') if on_proc => self.signal_selected_proc(Signal::Kill),
            KeyCode::Char('t') => self.open_terminal(false),
            KeyCode::Char('T') if !on_proc => self.open_terminal(true),
            KeyCode::Char(']') => self.terms.next(),
            KeyCode::Char('[') => self.terms.prev(),
            KeyCode::Char('x') => self.terms.close_active(),
            KeyCode::Up | KeyCode::Char('k') => self.select_up(),
            KeyCode::Down | KeyCode::Char('j') => self.select_down(),
            KeyCode::Right if self.tab == Tab::Instances => self.tree_expand(),
            KeyCode::Char(' ') if self.tab == Tab::Instances => self.tree_toggle(),
            KeyCode::Left if self.tab == Tab::Instances => self.tree_collapse(),
            KeyCode::Enter | KeyCode::Char('e') if !on_proc => self.open_config(),
            KeyCode::Char('r') if self.tab == Tab::Instances && !on_proc => {
                self.open_rename_or_run_prompt()
            }
            KeyCode::Char('o') if self.tab == Tab::Instances && !on_proc => self.attach_code(),
            KeyCode::Char('s') if self.tab == Tab::Instances && !on_proc => {
                self.stop_or_start_instance()
            }
            KeyCode::Char('l') if !on_proc => self.open_logs(),
            // Forwarding: `p` opens the `port` prompt prefilled from the selected
            // instance (Instances, not a process row) or service (Services); `d`
            // stops the selected forward on the Ports tab.
            KeyCode::Char('p') if self.tab == Tab::Instances && !on_proc => {
                self.open_port_prompt_instance()
            }
            KeyCode::Char('p') if self.tab == Tab::Services => self.open_port_prompt_service(),
            KeyCode::Char('d') if self.tab == Tab::Ports => self.stop_selected_forward(),
            KeyCode::Char('?') => self.open_help(),
            _ => {}
        }
    }

    /// Apply a mouse event over the full frame `area`. The config modal (when
    /// open) owns the mouse: a press/drag on or near the divider column resizes
    /// the split; the scroll wheel scrolls the pane under the cursor. Otherwise,
    /// with terminals open, clicks and the wheel drive the terminal panel
    /// ([`Self::terminal_mouse`]). I/O-free.
    pub fn on_mouse(&mut self, ev: &MouseEvent, area: Rect) {
        let Modal::Config(view) = &mut self.modal else {
            self.dragging_divider = false;
            self.terminal_mouse(ev, area);
            return;
        };
        // Divider column: the boundary between the left (config) pane and the
        // right (inspect) pane, i.e. split_pct of the modal width.
        let divider = (area.width as u32 * view.split_pct as u32 / 100) as u16;
        match ev.kind {
            MouseEventKind::Down(MouseButton::Left) => {
                if col_near(ev.column, divider) {
                    self.dragging_divider = true;
                    view.split_pct = divider_pct(ev.column, area.width);
                }
            }
            MouseEventKind::Drag(MouseButton::Left) => {
                if self.dragging_divider {
                    view.split_pct = divider_pct(ev.column, area.width);
                }
            }
            MouseEventKind::Up(MouseButton::Left) => {
                self.dragging_divider = false;
            }
            MouseEventKind::ScrollDown => {
                Self::wheel_scroll(view, ev.column, divider, 3);
            }
            MouseEventKind::ScrollUp => {
                Self::wheel_scroll(view, ev.column, divider, -3);
            }
            _ => {}
        }
    }
}

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

    #[test]
    fn tab_switching() {
        let mut app = new_app();
        assert_eq!(app.tab, Tab::Instances);

        // `tab` cycles forward over all three tabs and wraps.
        app.on_key(key(KeyCode::Tab));
        assert_eq!(app.tab, Tab::Services);
        app.on_key(key(KeyCode::Tab));
        assert_eq!(app.tab, Tab::Ports);
        app.on_key(key(KeyCode::Tab));
        assert_eq!(app.tab, Tab::Instances);

        // `S-tab` cycles backward and wraps (no longer a toggle).
        app.on_key(key(KeyCode::BackTab));
        assert_eq!(app.tab, Tab::Ports);
        app.on_key(key(KeyCode::BackTab));
        assert_eq!(app.tab, Tab::Services);
        app.on_key(key(KeyCode::BackTab));
        assert_eq!(app.tab, Tab::Instances);

        // Number keys jump directly.
        app.on_key(key(KeyCode::Char('2')));
        assert_eq!(app.tab, Tab::Services);
        app.on_key(key(KeyCode::Char('3')));
        assert_eq!(app.tab, Tab::Ports);
        app.on_key(key(KeyCode::Char('1')));
        assert_eq!(app.tab, Tab::Instances);
    }

    #[test]
    fn set_ports_reclamps_selection() {
        let mut app = new_app();
        app.tab = Tab::Ports;
        app.set_ports(port_rows(3));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down)); // last row (idx 2)
        assert_eq!(app.selected(), 2);

        // Shrinking to one row re-clamps to the last valid index.
        app.set_ports(port_rows(1));
        assert_eq!(app.selected(), 0);

        // Emptying re-clamps to 0.
        app.set_ports(port_rows(0));
        assert_eq!(app.selected(), 0);
    }

    #[test]
    fn quit_keys() {
        let mut app = new_app();
        app.on_key(key(KeyCode::Char('q')));
        assert!(app.should_quit);

        let mut app = new_app();
        app.on_key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
        assert!(app.should_quit);

        // Plain 'c' must not quit.
        let mut app = new_app();
        app.on_key(key(KeyCode::Char('c')));
        assert!(!app.should_quit);
    }
}