use std::path::PathBuf;
use crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::Rect;
use crate::config::Config;
use crate::runtime::backend;
use crate::tui::data::{ContainerStatus, InstanceRow, Node};
use crate::tui::settings::{SETTINGS, Settings};
use super::{App, Tab};
const HELP_BODY: &str = "\
Global
q, ctrl-c quit
tab / S-tab switch tab 1-4 jump to tab
: command prompt ? settings & help
drag select (copies on release with \"copy on select\" on, above)
{copy}
shift-drag the outer terminal's own selection (whole rows, across panes)
Tables (Instances / Services)
↑/k ↓/j move selection
→/space expand (sandbox instances, instance processes)
← collapse / jump to parent
enter, e open config explorer
r run (sandbox) / o open in VS Code (Instances)
rename (instance)
s stop / start l logs (Instances tab)
(stops running / starts exited; s-start is a bare start —
:start runs services + postStartCommand too; a drifted
exited instance is rebuilt instead)
p forward a port (instance / service selection prefills the prompt)
d remove the instance now, no prompt (Instances; like :rm,
so a dirty worktree still refuses)
Ports tab
↑/k ↓/j move selection
d stop the selected forward
Inbox tab (dispatcher threads + devsbd notify records)
The list is on the left, the selected thread on the right; selecting a
thread marks it read. esc steps back: input → thread → list.
Inbox list
↑/k ↓/j move selection
← / → previous / next view: Needs you / Active / Done / All
(Needs you: threads asking for you + unread notify records;
leaving the tab marks the notify records it showed read)
enter focus the thread
r / i focus the reply input (when the thread takes replies)
d dismiss a notify record; on a dispatcher thread, mark it done
u reopen a done thread
D clear all notify records (dispatcher threads stay)
o t l p VS Code / terminal / logs / forward prompt on the thread's
child (or its sender when it has none)
Inbox thread (shadows the dashboard keys while focused)
The header (title, state, child, action row) stays put; the feed
under it is newest first and scrolls on its own.
↑/k ↓/j scroll pgup/pgdn page g / G newest / oldest
enter open the thread's link (↗)
1-9 run an action: host verbs (⌂) run now; the rest is an
event the dispatcher pulls with `devsbd events` (✓ = done)
r / i focus the reply input
tab focus the open forms (pinned under the header), else the
reply input
d / u mark done / reopen (each also an event for the dispatcher;
also marks / clears the thread's child done)
o t l p as in the list (the [o] [t] [l] [p] buttons)
m markdown: rendered / raw source (every thread, this session)
esc back to the list q quit : prompt ? help
Inbox form (the open forms pinned under the thread's header)
tab / S-tab next / previous question (across every open form)
↑ ↓ ← → move over the options
space pick (several-pick: toggle; a text question: edit)
e edit a text question in the composer box; enter or esc
keeps the edit, alt-enter inserts a newline
enter confirm: the summary (or what's missing); enter again
submits, esc cancels
r / i the reply input esc back to the thread
(every pick and kept edit is saved as the form's draft)
Inbox reply input
enter send (the input stays, empty, for the next message)
esc back to the thread
Process rows (expanded instance)
← jump to the parent instance
t SIGTERM K SIGKILL
(the instance shortcuts above are disabled on a process row;
signals need `ps` in the image — a `top` fallback listing hides them)
Terminals
t open terminal (instance / service)
T force a new terminal for the same target
[ / ] previous / next terminal tab
x close the active terminal
ctrl-] / F12 focus terminal ⇄ back to dashboard
(while focused, all other keys go to the shell)
Config modal
t toggle original / resolved
tab switch pane (config ⇄ inspect)
< / > resize the divider (drag it too)
↑/k ↓/j scroll focused pane pgup/pgdn page
g / G top / bottom
esc, q close
Logs modal
↑/k ↓/j scroll pgup/pgdn page
g / G top / bottom
esc, q close
Command prompt (:)
run <sandbox> [--name n] [--branch b] [--base ref]
exec <instance> <cmd…>
code <instance> [--goto path[:line[:col]]]
rm <instance> rename <instance> <new-name>
stop <instance> start <instance>
port <instance> [--service s] [--address a] <[host:]port>
(an instance is required; a global service is reached by
naming any instance that references it)
tab complete / cycle
↑ ↓ history
ctrl-u clear line ctrl-w delete word
esc cancel";
fn help_body(copy_key_intercepted: bool) -> String {
let copy = if copy_key_intercepted {
" ctrl-c / cmd-c copy the selection to the clipboard (OSC 52)\n \
(ctrl-c not in a focused terminal: the shell gets it there)"
} else {
" ctrl-shift-c / cmd-c copy the selection to the clipboard (OSC 52)\n \
(ctrl-c too over a dashboard selection: legacy terminals)"
};
HELP_BODY.replacen("{copy}", copy, 1)
}
pub struct TextModal {
pub title: String,
pub body: String,
pub scroll: u16,
}
impl TextModal {
pub fn new(title: String, body: String) -> Self {
Self { title, body, scroll: 0 }
}
fn on_scroll_key(&mut self, key: KeyEvent) -> bool {
scroll_key(&mut self.scroll, line_count(&self.body), key)
}
pub(super) fn scroll_by(&mut self, delta: i16) -> u16 {
self.scroll = apply_delta(self.scroll, delta, line_count(&self.body).saturating_sub(1));
self.scroll
}
}
pub struct HelpModal {
pub text: TextModal,
pub cursor: usize,
}
fn line_count(body: &str) -> u16 {
body.lines().count().min(u16::MAX as usize) as u16
}
pub(super) fn scroll_key(scroll: &mut u16, lines: u16, key: KeyEvent) -> bool {
let max = lines.saturating_sub(1);
match key.code {
KeyCode::Up | KeyCode::Char('k') => *scroll = scroll.saturating_sub(1),
KeyCode::Down | KeyCode::Char('j') => *scroll = (*scroll).saturating_add(1).min(max),
KeyCode::PageUp => *scroll = scroll.saturating_sub(20),
KeyCode::PageDown => *scroll = (*scroll).saturating_add(20).min(max),
KeyCode::Char('g') => *scroll = 0,
KeyCode::Char('G') => *scroll = max,
_ => return false,
}
true
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Side {
Original,
Resolved,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Pane {
Config,
Inspect,
}
const SPLIT_MIN: u16 = 20;
const SPLIT_MAX: u16 = 80;
pub struct ConfigView {
pub title: String,
pub original: String,
pub resolved: String,
pub showing: Side,
pub scroll: u16,
pub hash: String,
pub inspect: String,
pub inspect_scroll: u16,
pub inspect_container: String,
pub focus: Pane,
pub split_pct: u16,
}
impl ConfigView {
pub fn body(&self) -> &str {
match self.showing {
Side::Original => &self.original,
Side::Resolved => &self.resolved,
}
}
fn line_count(&self) -> u16 {
line_count(self.body())
}
fn clamp_scroll(&mut self) {
let max = self.line_count().saturating_sub(1);
if self.scroll > max {
self.scroll = max;
}
}
fn scroll_focused(&mut self, key: KeyEvent) {
match self.focus {
Pane::Config => {
let lines = self.line_count();
scroll_key(&mut self.scroll, lines, key);
}
Pane::Inspect => {
let lines = line_count(&self.inspect);
scroll_key(&mut self.inspect_scroll, lines, key);
}
}
}
pub(super) fn scroll_pane(&mut self, pane: Pane, delta: i16) -> u16 {
match pane {
Pane::Config => {
self.scroll = apply_delta(self.scroll, delta, self.line_count().saturating_sub(1));
self.scroll
}
Pane::Inspect => {
let max = line_count(&self.inspect).saturating_sub(1);
self.inspect_scroll = apply_delta(self.inspect_scroll, delta, max);
self.inspect_scroll
}
}
}
fn resize(&mut self, delta: i16) {
self.split_pct = clamp_split(self.split_pct as i16 + delta);
}
}
fn clamp_split(pct: i16) -> u16 {
pct.clamp(SPLIT_MIN as i16, SPLIT_MAX as i16) as u16
}
pub fn divider_pct(col: u16, width: u16) -> u16 {
if width == 0 {
return SPLIT_MIN;
}
let pct = (col as u32 * 100 / width as u32) as i16;
clamp_split(pct)
}
pub(super) fn col_near(col: u16, divider: u16) -> bool {
col.abs_diff(divider) <= 1
}
pub(super) fn point_in(rect: Rect, col: u16, row: u16) -> bool {
col >= rect.x
&& col < rect.x + rect.width
&& row >= rect.y
&& row < rect.y + rect.height
}
fn apply_delta(scroll: u16, delta: i16, max: u16) -> u16 {
let next = scroll as i32 + delta as i32;
next.clamp(0, max as i32) as u16
}
pub enum Modal {
None,
Config(ConfigView),
Help(HelpModal),
Logs(TextModal),
}
impl App {
pub(super) fn wheel_scroll(view: &mut ConfigView, col: u16, divider: u16, delta: i16) {
view.scroll_pane(if col < divider { Pane::Config } else { Pane::Inspect }, delta);
}
pub(super) fn on_key_modal(&mut self, key: KeyEvent) {
match &mut self.modal {
Modal::None => {}
Modal::Config(view) => match key.code {
KeyCode::Esc | KeyCode::Char('q') => self.modal = Modal::None,
KeyCode::Char('t') => {
view.showing = match view.showing {
Side::Original => Side::Resolved,
Side::Resolved => Side::Original,
};
view.clamp_scroll();
}
KeyCode::Tab => {
view.focus = match view.focus {
Pane::Config => Pane::Inspect,
Pane::Inspect => Pane::Config,
};
}
KeyCode::Char('<') => view.resize(-5),
KeyCode::Char('>') => view.resize(5),
_ => view.scroll_focused(key),
},
Modal::Help(view) => {
let n = SETTINGS.len();
match key.code {
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('?') => self.modal = Modal::None,
KeyCode::Tab if n > 0 => view.cursor = (view.cursor + 1) % n,
KeyCode::BackTab if n > 0 => view.cursor = (view.cursor + n - 1) % n,
KeyCode::Char(' ') | KeyCode::Enter => {
let i = view.cursor;
self.toggle_setting(i);
}
_ => {
view.text.on_scroll_key(key);
}
}
}
Modal::Logs(view) => {
if matches!(key.code, KeyCode::Esc | KeyCode::Char('q')) {
self.modal = Modal::None;
} else {
view.on_scroll_key(key);
}
}
}
}
pub(super) fn open_help(&mut self) {
let text = TextModal::new(" settings & help ".to_string(), help_body(self.copy_key_intercepted));
self.modal = Modal::Help(HelpModal { text, cursor: 0 });
}
pub(super) fn toggle_setting(&mut self, i: usize) {
if i >= SETTINGS.len() {
return;
}
self.settings.toggle(i);
self.settings_dirty = true;
if let Modal::Help(view) = &mut self.modal {
view.cursor = i;
}
}
pub fn take_settings_save(&mut self) -> Option<Settings> {
std::mem::take(&mut self.settings_dirty).then_some(self.settings)
}
pub(super) fn help_mouse(&mut self, ev: &MouseEvent, area: Rect) {
if ev.kind != MouseEventKind::Down(MouseButton::Left) || !matches!(self.modal, Modal::Help(_)) {
return;
}
if let Some(i) = crate::tui::ui::help_setting_hit(area, ev.column, ev.row) {
self.toggle_setting(i);
}
}
pub(super) fn open_logs(&mut self) {
if self.tab != Tab::Instances {
return;
}
let Some(i) = self.selected_instance_index() else {
return;
};
let Some(snapshot) = &self.snapshot else {
return;
};
let Some(row) = snapshot.instances.get(i) else {
return;
};
let container = row.container.clone();
self.open_logs_for(&container);
}
pub(super) fn open_logs_for(&mut self, container: &str) {
let body = match backend().logs_tail(container, 50) {
Ok(out) if out.trim().is_empty() => "(no log output)".to_string(),
Ok(out) => out,
Err(e) => format!("{e:#}"),
};
let title = format!(" logs — {container} (last 50) ");
self.modal = Modal::Logs(TextModal::new(title, body));
}
pub(super) fn open_config(&mut self) {
let Some(snapshot) = &self.snapshot else {
return;
};
let (mut view, target) = match self.tab {
Tab::Instances => {
let (name, target) = match self.selected_node() {
Some(Node::Sandbox(i)) | Some(Node::Empty(i)) => {
let name = snapshot.sandboxes.get(i).map(|s| s.name.clone());
let target = name
.as_deref()
.and_then(|n| inspect_target(&snapshot.instances, Target::Sandbox(n)));
(name, target)
}
Some(Node::Instance(i)) => {
let name = snapshot.instances.get(i).map(|r| r.sandbox.clone());
let target = inspect_target(&snapshot.instances, Target::Instance(i));
(name, target)
}
Some(Node::Proc { instance, .. }) => {
let name = snapshot.instances.get(instance).map(|r| r.sandbox.clone());
let target =
inspect_target(&snapshot.instances, Target::Instance(instance));
(name, target)
}
Some(Node::Orphans) | None => (None, None),
};
let Some(name) = name else {
return;
};
(Self::build_sandbox_view(&self.dir, &name), target)
}
Tab::Services => {
let Some(row) = snapshot.services.get(self.selected()) else {
return;
};
let target = service_inspect_target(&row.containers);
(Self::build_service_view(&self.dir, &row.name), target)
}
Tab::Ports | Tab::Inbox => return,
};
let placeholder = match self.tab {
Tab::Instances => "(no running instance)",
Tab::Services => "(no containers)",
Tab::Ports | Tab::Inbox => "",
};
set_inspect(&mut view, target, placeholder);
self.modal = Modal::Config(view);
}
fn build_sandbox_view(dir: &PathBuf, name: &str) -> ConfigView {
let mut view = ConfigView {
title: name.to_string(),
original: String::new(),
resolved: String::new(),
showing: Side::Original,
scroll: 0,
hash: String::new(),
inspect: String::new(),
inspect_scroll: 0,
inspect_container: String::new(),
focus: Pane::Config,
split_pct: 50,
};
match Config::load(dir) {
Ok(cfg) => {
view.original = match cfg.sandbox_table(name) {
Ok(t) => to_toml(t),
Err(e) => format!("{e:#}"),
};
match cfg.resolved_table(name) {
Ok((t, hash)) => {
view.resolved = to_toml(&t);
view.hash = hash;
}
Err(e) => view.resolved = format!("{e:#}"),
}
}
Err(e) => {
let msg = format!("{e:#}");
view.original = msg.clone();
view.resolved = msg;
}
}
view
}
fn build_service_view(dir: &PathBuf, name: &str) -> ConfigView {
let body = match Config::load(dir) {
Ok(cfg) => match cfg.services.get(name) {
Some(t) => to_toml(t),
None => format!("unknown service `{name}`"),
},
Err(e) => format!("{e:#}"),
};
ConfigView {
title: name.to_string(),
original: body.clone(),
resolved: body,
showing: Side::Original,
scroll: 0,
hash: String::new(),
inspect: String::new(),
inspect_scroll: 0,
inspect_container: String::new(),
focus: Pane::Config,
split_pct: 50,
}
}
}
enum Target<'a> {
Instance(usize),
Sandbox(&'a str),
}
fn inspect_target(instances: &[InstanceRow], target: Target) -> Option<String> {
match target {
Target::Instance(i) => instances.get(i).map(|r| r.container.clone()),
Target::Sandbox(name) => instances
.iter()
.find(|r| r.sandbox == name && matches!(r.status, ContainerStatus::Running(_)))
.map(|r| r.container.clone()),
}
}
fn service_inspect_target(containers: &[(String, ContainerStatus)]) -> Option<String> {
containers
.iter()
.find(|(_, s)| matches!(s, ContainerStatus::Running(_)))
.or_else(|| containers.first())
.map(|(name, _)| name.clone())
}
fn set_inspect(view: &mut ConfigView, target: Option<String>, placeholder: &str) {
let Some(container) = target else {
view.inspect = placeholder.to_string();
return;
};
view.inspect_container = container.clone();
view.inspect = match backend().inspect_json(&container) {
Ok(out) => crate::commands::inspect::pretty_inspect(&out),
Err(e) => format!("{e:#}"),
};
}
fn to_toml(table: &toml::Table) -> String {
match toml::to_string_pretty(table) {
Ok(s) => s,
Err(e) => format!("cannot serialize config: {e}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tui::app::test_support::*;
use crate::tui::app::*;
#[test]
fn modal_toggles_side_and_closes() {
let mut app = new_app();
open_modal(&mut app);
assert_eq!(view(&app).showing, Side::Original);
app.on_key(key(KeyCode::Char('t')));
assert_eq!(view(&app).showing, Side::Resolved);
app.on_key(key(KeyCode::Char('t')));
assert_eq!(view(&app).showing, Side::Original);
app.on_key(key(KeyCode::Tab));
assert_eq!(view(&app).showing, Side::Original);
app.on_key(key(KeyCode::Esc));
assert!(matches!(app.modal, Modal::None));
open_modal(&mut app);
app.on_key(key(KeyCode::Char('q')));
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn modal_scroll_clamps() {
let mut app = new_app();
open_modal(&mut app);
app.on_key(key(KeyCode::Char('t')));
app.on_key(key(KeyCode::Up));
assert_eq!(view(&app).scroll, 0);
app.on_key(key(KeyCode::Down));
assert_eq!(view(&app).scroll, 1);
app.on_key(key(KeyCode::Char('G')));
assert_eq!(view(&app).scroll, 9);
app.on_key(key(KeyCode::PageDown));
assert_eq!(view(&app).scroll, 9);
app.on_key(key(KeyCode::Char('g')));
assert_eq!(view(&app).scroll, 0);
}
#[test]
fn modal_swallows_dashboard_keys() {
let mut app = new_app();
open_modal(&mut app);
app.on_key(key(KeyCode::Char('2')));
assert_eq!(app.tab, Tab::Instances);
assert!(matches!(app.modal, Modal::Config(_)));
}
#[test]
fn help_overlay_opens_and_closes() {
let mut app = new_app();
app.on_key(key(KeyCode::Char('?')));
assert!(matches!(app.modal, Modal::Help(_)));
app.on_key(key(KeyCode::Char('2')));
assert_eq!(app.tab, Tab::Instances);
assert!(matches!(app.modal, Modal::Help(_)));
app.on_key(key(KeyCode::Char('?')));
assert!(matches!(app.modal, Modal::None));
app.on_key(key(KeyCode::Char('?')));
app.on_key(key(KeyCode::Esc));
assert!(matches!(app.modal, Modal::None));
app.on_key(key(KeyCode::Char('?')));
app.on_key(key(KeyCode::Char('q')));
assert!(matches!(app.modal, Modal::None));
}
fn help(app: &App) -> &HelpModal {
match &app.modal {
Modal::Help(v) => v,
_ => panic!("help not open"),
}
}
#[test]
fn settings_toggle_in_the_help_modal_and_are_marked_for_saving() {
let mut app = new_app();
app.on_key(key(KeyCode::Char('?')));
assert_eq!(app.take_settings_save(), None, "opening changes nothing");
assert_eq!(help(&app).cursor, 0);
app.on_key(key(KeyCode::Char(' ')));
assert!(!app.settings.copy_on_select, "on by default");
assert_eq!(app.take_settings_save(), Some(app.settings));
assert_eq!(app.take_settings_save(), None, "once per change");
app.on_key(key(KeyCode::Tab));
assert_eq!(help(&app).cursor, 1);
app.on_key(key(KeyCode::Enter));
assert!(!app.settings.terminal_clipboard);
app.on_key(key(KeyCode::Tab));
assert_eq!(help(&app).cursor, 0);
app.on_key(key(KeyCode::BackTab));
assert_eq!(help(&app).cursor, 1);
app.on_key(key(KeyCode::Down));
assert_eq!((help(&app).cursor, help(&app).text.scroll), (1, 1));
assert_eq!(
app.take_settings_save(),
Some(Settings { copy_on_select: false, terminal_clipboard: false })
);
}
#[test]
fn the_help_names_ctrl_c_where_the_terminal_keeps_ctrl_shift_c() {
let body = |intercepted| {
let mut app = new_app();
app.copy_key_intercepted = intercepted;
app.on_key(key(KeyCode::Char('?')));
help(&app).text.body.clone()
};
let plain = body(false);
let line = |b: &str, start: &str| b.lines().find(|l| l.starts_with(start)).map(str::to_string);
let copy = line(&plain, " ctrl-shift-c / cmd-c").expect("copy line");
assert!(!plain.contains("{copy}"));
let moved = body(true);
assert!(line(&moved, " ctrl-shift-c").is_none(), "{moved}");
let ctrl_c = line(&moved, " ctrl-c / cmd-c ").expect("ctrl-c line");
let col = |l: &str| l.find("copy the selection").unwrap();
assert_eq!(col(&ctrl_c), col(©));
assert_eq!(plain.lines().count(), moved.lines().count());
}
#[test]
fn a_click_on_a_settings_row_toggles_it() {
let mut app = new_app();
app.on_key(key(KeyCode::Char('?')));
let press = |col, row| mouse_at(MouseEventKind::Down(MouseButton::Left), col, row);
app.on_mouse(&press(5, 2), FRAME);
assert!(!app.settings.terminal_clipboard);
assert_eq!(help(&app).cursor, 1, "the cursor follows");
assert!(app.take_settings_save().is_some());
for (col, row) in [(5, 0), (0, 1), (5, 3), (5, 10)] {
app.on_mouse(&press(col, row), FRAME);
assert_eq!(app.take_settings_save(), None, "({col}, {row})");
}
app.on_key(key(KeyCode::Esc));
app.on_mouse(&press(5, 1), FRAME);
assert_eq!(app.settings, Settings { copy_on_select: true, terminal_clipboard: false });
}
#[test]
fn text_modal_scroll_shares_config_logic() {
let mut m = TextModal::new("t".into(), (0..10).map(|i| format!("l{i}\n")).collect());
assert_eq!(m.scroll, 0);
assert!(m.on_scroll_key(key(KeyCode::Up))); assert_eq!(m.scroll, 0);
assert!(m.on_scroll_key(key(KeyCode::Down)));
assert_eq!(m.scroll, 1);
assert!(m.on_scroll_key(key(KeyCode::Char('G'))));
assert_eq!(m.scroll, 9);
assert!(m.on_scroll_key(key(KeyCode::PageDown))); assert_eq!(m.scroll, 9);
assert!(m.on_scroll_key(key(KeyCode::Char('g'))));
assert_eq!(m.scroll, 0);
assert!(!m.on_scroll_key(key(KeyCode::Char('x'))));
}
#[test]
fn logs_modal_opens_on_selected_instance() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char('l')));
match &app.modal {
Modal::Logs(v) => {
assert!(v.title.contains("devsandbox-inst0"));
assert!(!v.body.is_empty());
}
_ => panic!("logs modal not open"),
}
app.on_key(key(KeyCode::Char('q')));
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn logs_key_ignored_on_services_tab() {
let mut app = new_app();
app.set_snapshot(snapshot_with(1));
app.tab = Tab::Services;
app.on_key(key(KeyCode::Char('l')));
assert!(matches!(app.modal, Modal::None));
}
#[test]
fn scroll_reclamps_on_side_toggle() {
let mut app = new_app();
open_modal(&mut app);
app.on_key(key(KeyCode::Char('t')));
app.on_key(key(KeyCode::Char('G')));
assert_eq!(view(&app).scroll, 9);
app.on_key(key(KeyCode::Char('t')));
assert_eq!(view(&app).showing, Side::Original);
assert_eq!(view(&app).scroll, 1);
}
#[test]
fn tab_switches_focus_between_panes() {
let mut app = new_app();
open_modal(&mut app);
assert_eq!(view(&app).focus, Pane::Config);
app.on_key(key(KeyCode::Tab));
assert_eq!(view(&app).focus, Pane::Inspect);
app.on_key(key(KeyCode::Tab));
assert_eq!(view(&app).focus, Pane::Config);
app.on_key(key(KeyCode::Char('t')));
assert_eq!(view(&app).focus, Pane::Config);
}
#[test]
fn scroll_routes_to_focused_pane() {
let mut app = new_app();
open_modal(&mut app);
app.on_key(key(KeyCode::Char('t')));
app.on_key(key(KeyCode::Down));
assert_eq!(view(&app).scroll, 1);
assert_eq!(view(&app).inspect_scroll, 0);
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Down));
assert_eq!(view(&app).scroll, 1);
assert_eq!(view(&app).inspect_scroll, 1);
app.on_key(key(KeyCode::Char('G')));
assert_eq!(view(&app).inspect_scroll, 19);
}
#[test]
fn split_pct_clamps_via_resize_keys() {
let mut app = new_app();
open_modal(&mut app);
assert_eq!(view(&app).split_pct, 50);
app.on_key(key(KeyCode::Char('<')));
assert_eq!(view(&app).split_pct, 45);
app.on_key(key(KeyCode::Char('>')));
app.on_key(key(KeyCode::Char('>')));
assert_eq!(view(&app).split_pct, 55);
for _ in 0..20 {
app.on_key(key(KeyCode::Char('<')));
}
assert_eq!(view(&app).split_pct, SPLIT_MIN);
for _ in 0..40 {
app.on_key(key(KeyCode::Char('>')));
}
assert_eq!(view(&app).split_pct, SPLIT_MAX);
}
#[test]
fn divider_pct_maps_column_to_clamped_percentage() {
assert_eq!(divider_pct(50, 100), 50);
assert_eq!(divider_pct(0, 100), SPLIT_MIN);
assert_eq!(divider_pct(100, 100), SPLIT_MAX);
assert_eq!(divider_pct(5, 100), SPLIT_MIN);
assert_eq!(divider_pct(95, 100), SPLIT_MAX);
assert_eq!(divider_pct(10, 0), SPLIT_MIN);
}
#[test]
fn mouse_drag_resizes_divider() {
let mut app = new_app();
open_modal(&mut app);
app.on_mouse(&mouse(MouseEventKind::Down(MouseButton::Left), 50), MODAL_AREA);
app.on_mouse(&mouse(MouseEventKind::Drag(MouseButton::Left), 30), MODAL_AREA);
assert_eq!(view(&app).split_pct, 30);
app.on_mouse(&mouse(MouseEventKind::Up(MouseButton::Left), 30), MODAL_AREA);
app.on_mouse(&mouse(MouseEventKind::Drag(MouseButton::Left), 70), MODAL_AREA);
assert_eq!(view(&app).split_pct, 30);
}
#[test]
fn mouse_press_far_from_divider_ignored() {
let mut app = new_app();
open_modal(&mut app);
app.on_mouse(&mouse(MouseEventKind::Down(MouseButton::Left), 10), MODAL_AREA);
app.on_mouse(&mouse(MouseEventKind::Drag(MouseButton::Left), 70), MODAL_AREA);
assert_eq!(view(&app).split_pct, 50);
}
#[test]
fn wheel_scrolls_pane_under_cursor() {
let mut app = new_app();
open_modal(&mut app);
app.on_key(key(KeyCode::Char('t'))); app.on_mouse(&mouse(MouseEventKind::ScrollDown, 10), MODAL_AREA);
assert_eq!(view(&app).scroll, 3);
assert_eq!(view(&app).inspect_scroll, 0);
app.on_mouse(&mouse(MouseEventKind::ScrollDown, 90), MODAL_AREA);
assert_eq!(view(&app).inspect_scroll, 3);
app.on_mouse(&mouse(MouseEventKind::ScrollUp, 10), MODAL_AREA);
app.on_mouse(&mouse(MouseEventKind::ScrollUp, 10), MODAL_AREA);
assert_eq!(view(&app).scroll, 0);
}
#[test]
fn inspect_target_picks_per_node_kind() {
use crate::tui::data::{ContainerStatus, InstanceRow};
let mk = |name: &str, sandbox: &str, status: ContainerStatus| InstanceRow {
name: name.into(),
sandbox: sandbox.into(),
container: format!("devsandbox-{name}"),
status,
uptime_secs: 0,
cpu: None,
mem: None,
folder: "/f".into(),
worktree: false,
services: Vec::new(),
workspace: "/w".into(),
remote_user: None,
remote_env_len: 0,
base_folder: "/f".into(),
drift: false,
instance_id: String::new(),
dispatcher: None,
done: false,
};
let rows = vec![
mk("a", "s", ContainerStatus::Exited("x".into())),
mk("b", "s", ContainerStatus::Running("Up".into())),
mk("c", "t", ContainerStatus::Missing),
];
assert_eq!(
inspect_target(&rows, Target::Instance(0)),
Some("devsandbox-a".into()),
);
assert_eq!(
inspect_target(&rows, Target::Sandbox("s")),
Some("devsandbox-b".into()),
);
assert_eq!(inspect_target(&rows, Target::Sandbox("t")), None);
assert_eq!(inspect_target(&rows, Target::Instance(9)), None);
}
#[test]
fn service_inspect_target_prefers_running() {
use crate::tui::data::ContainerStatus;
let running = vec![
("svc-1".to_string(), ContainerStatus::Exited("x".into())),
("svc-2".to_string(), ContainerStatus::Running("Up".into())),
];
assert_eq!(service_inspect_target(&running), Some("svc-2".into()));
let none_running = vec![
("svc-1".to_string(), ContainerStatus::Exited("x".into())),
("svc-2".to_string(), ContainerStatus::Missing),
];
assert_eq!(service_inspect_target(&none_running), Some("svc-1".into()));
assert_eq!(service_inspect_target(&[]), None);
}
}