bohay 0.9.3

Mission control for your AI coding agents
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
//! Keybindings — the prefix-mode command registry. After `Ctrl+Space`, a key
//! triggers a [`Cmd`]. Defaults are listed here; users can rebind any command to
//! a different key in Settings → Keys (persisted to `config.keybindings`). A few
//! fixed aliases (vim `hjkl`, `Tab`/`⇧Tab`, `q`) are always available too.

use std::collections::HashMap;

use ratatui::crossterm::event::{KeyCode, KeyEvent};

use super::*;

/// A prefix-mode command — the thing a key triggers after `Ctrl+Space`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Cmd {
    FocusLeft,
    FocusDown,
    FocusUp,
    FocusRight,
    SplitRight,
    SplitDown,
    ForkSession,
    ClosePane,
    ZoomPane,
    ResizeMode,
    NewTab,
    NextTab,
    PrevTab,
    NewWorkspace,
    CloseWorkspace,
    NextWorkspace,
    PrevWorkspace,
    NewWorktree,
    OpenGit,
    OpenBoard,
    OpenSettings,
    ToggleSidebar,
    ToggleRightSidebar,
    ToggleAgents,
    ToggleFiles,
    Switcher,
    Detach,
}

impl Cmd {
    /// Every command, grouped for the Settings → Keys list.
    pub const ALL: &'static [Cmd] = &[
        Cmd::FocusLeft,
        Cmd::FocusDown,
        Cmd::FocusUp,
        Cmd::FocusRight,
        Cmd::SplitRight,
        Cmd::SplitDown,
        Cmd::ForkSession,
        Cmd::ClosePane,
        Cmd::ZoomPane,
        Cmd::ResizeMode,
        Cmd::NewTab,
        Cmd::NextTab,
        Cmd::PrevTab,
        Cmd::NewWorkspace,
        Cmd::CloseWorkspace,
        Cmd::NextWorkspace,
        Cmd::PrevWorkspace,
        Cmd::NewWorktree,
        Cmd::OpenGit,
        Cmd::OpenBoard,
        Cmd::OpenSettings,
        Cmd::ToggleSidebar,
        Cmd::ToggleRightSidebar,
        Cmd::ToggleAgents,
        Cmd::ToggleFiles,
        Cmd::Switcher,
        Cmd::Detach,
    ];

    /// Stable id used as the config key (must never change once shipped).
    pub fn id(self) -> &'static str {
        match self {
            Cmd::FocusLeft => "focus_left",
            Cmd::FocusDown => "focus_down",
            Cmd::FocusUp => "focus_up",
            Cmd::FocusRight => "focus_right",
            Cmd::SplitRight => "split_right",
            Cmd::SplitDown => "split_down",
            Cmd::ForkSession => "fork_session",
            Cmd::ClosePane => "close_pane",
            Cmd::ZoomPane => "zoom_pane",
            Cmd::ResizeMode => "resize_mode",
            Cmd::NewTab => "new_tab",
            Cmd::NextTab => "next_tab",
            Cmd::PrevTab => "prev_tab",
            Cmd::NewWorkspace => "new_node",
            Cmd::CloseWorkspace => "close_node",
            Cmd::NextWorkspace => "next_node",
            Cmd::PrevWorkspace => "prev_node",
            Cmd::NewWorktree => "new_worktree",
            Cmd::OpenGit => "open_git",
            Cmd::OpenBoard => "open_board",
            Cmd::OpenSettings => "open_settings",
            Cmd::ToggleSidebar => "toggle_sidebar",
            Cmd::ToggleRightSidebar => "toggle_right_sidebar",
            Cmd::ToggleAgents => "toggle_agents",
            Cmd::ToggleFiles => "toggle_files",
            Cmd::Switcher => "switcher",
            Cmd::Detach => "detach",
        }
    }

    /// Human label shown in the Keys list / cheat-sheet, in the active language
    /// (docs/21). `id()` stays the stable English key; only this display label
    /// is localized.
    pub fn label(self, cat: &crate::i18n::Catalog) -> &'static str {
        match self {
            Cmd::FocusLeft => cat.cmd_focus_left,
            Cmd::FocusDown => cat.cmd_focus_down,
            Cmd::FocusUp => cat.cmd_focus_up,
            Cmd::FocusRight => cat.cmd_focus_right,
            Cmd::SplitRight => cat.cmd_split_right,
            Cmd::SplitDown => cat.cmd_split_down,
            Cmd::ForkSession => cat.cmd_fork_session,
            Cmd::ClosePane => cat.cmd_close_pane,
            Cmd::ZoomPane => cat.cmd_zoom_pane,
            Cmd::ResizeMode => cat.cmd_resize_mode,
            Cmd::NewTab => cat.cmd_new_tab,
            Cmd::NextTab => cat.cmd_next_tab,
            Cmd::PrevTab => cat.cmd_prev_tab,
            Cmd::NewWorkspace => cat.cmd_new_workspace,
            Cmd::CloseWorkspace => cat.cmd_close_workspace,
            Cmd::NextWorkspace => cat.cmd_next_workspace,
            Cmd::PrevWorkspace => cat.cmd_prev_workspace,
            Cmd::NewWorktree => cat.cmd_new_worktree,
            Cmd::OpenGit => cat.cmd_open_git,
            Cmd::OpenBoard => cat.cmd_open_board,
            Cmd::OpenSettings => cat.cmd_open_settings,
            Cmd::ToggleSidebar => cat.cmd_toggle_sidebar,
            Cmd::ToggleRightSidebar => cat.cmd_toggle_right_sidebar,
            Cmd::ToggleAgents => cat.cmd_toggle_agents,
            Cmd::ToggleFiles => cat.cmd_toggle_files,
            Cmd::Switcher => cat.cmd_switcher,
            Cmd::Detach => cat.cmd_detach,
        }
    }

    /// Group heading for the Settings → Keys list. `Cmd::ALL` is ordered so each
    /// group is contiguous; the Keys tab prints this label when it changes. Kept
    /// English to match the tab's (English) how-to intro; the per-command
    /// `label()` stays localized.
    pub fn section(self) -> &'static str {
        match self {
            Cmd::FocusLeft
            | Cmd::FocusDown
            | Cmd::FocusUp
            | Cmd::FocusRight
            | Cmd::SplitRight
            | Cmd::SplitDown
            | Cmd::ForkSession
            | Cmd::ClosePane
            | Cmd::ZoomPane
            | Cmd::ResizeMode => "Panes",
            Cmd::NewTab | Cmd::NextTab | Cmd::PrevTab => "Tabs",
            Cmd::NewWorkspace
            | Cmd::CloseWorkspace
            | Cmd::NextWorkspace
            | Cmd::PrevWorkspace
            | Cmd::NewWorktree => "Nodes & worktrees",
            Cmd::OpenGit
            | Cmd::OpenBoard
            | Cmd::OpenSettings
            | Cmd::ToggleSidebar
            | Cmd::ToggleRightSidebar
            | Cmd::ToggleAgents
            | Cmd::ToggleFiles => "Views & panels",
            Cmd::Switcher | Cmd::Detach => "Session",
        }
    }

    /// Default key (a [`key_string`] value).
    pub fn default_key(self) -> &'static str {
        match self {
            Cmd::FocusLeft => "",
            Cmd::FocusDown => "",
            Cmd::FocusUp => "",
            Cmd::FocusRight => "",
            Cmd::SplitRight => "v",
            Cmd::SplitDown => "s",
            Cmd::ForkSession => "f",
            Cmd::ClosePane => "x",
            Cmd::ZoomPane => "z",
            Cmd::ResizeMode => "r",
            Cmd::NewTab => "c",
            Cmd::NextTab => "n",
            Cmd::PrevTab => "p",
            Cmd::NewWorkspace => "N",
            Cmd::CloseWorkspace => "D",
            Cmd::NextWorkspace => "w",
            Cmd::PrevWorkspace => "W",
            Cmd::NewWorktree => "G",
            Cmd::OpenGit => "g",
            Cmd::OpenBoard => "o",
            Cmd::OpenSettings => ",",
            Cmd::ToggleSidebar => "b",
            Cmd::ToggleRightSidebar => "B",
            Cmd::ToggleAgents => "a",
            Cmd::ToggleFiles => "e",
            Cmd::Switcher => "m",
            Cmd::Detach => "d",
        }
    }
}

/// Read-only reference blocks shown below the rebindable commands in Settings →
/// Keys: the fixed keys and the context-specific shortcuts (scroll mode, the git
/// tab, the task board, the folder picker, the mouse). Kept in one table so the
/// count is authoritative for cursor bounds and the renderer just draws it. Each
/// entry is `(section heading, &[(keys, what it does)])`. English, like the tab's
/// how-to intro; the rebindable command labels stay localized.
pub const KEY_REFERENCE: &[(&str, &[(&str, &str)])] = &[
    (
        "Always on (not rebindable)",
        &[
            ("h j k l", "focus panes (vim aliases)"),
            ("q", "detach, leave the server running"),
            ("X", "close pane"),
            ("-", "split down"),
            ("⇥ / ⇧⇥", "next / previous tab"),
            ("⌃Space ⌃Space", "send a literal Ctrl+Space"),
        ],
    ),
    (
        "Scroll history  (no prefix)",
        &[
            ("Shift+↑", "enter scroll mode on the focused pane"),
            ("j / k", "line down / up"),
            ("Space / b", "page down / up"),
            ("g / G", "top of history / back to live"),
            ("1–9", "jump through history (1 oldest, 9 newest)"),
            ("q  esc", "back to live"),
        ],
    ),
    (
        "Resize mode  (⌃Space r)",
        &[
            ("arrows  hjkl", "resize the focused pane"),
            ("Shift+arrow", "bigger step"),
            ("=  0", "equalize splits"),
            ("esc", "exit resize mode"),
        ],
    ),
    (
        "Git tab  (⌃Space g)",
        &[
            ("1–6", "Commits Flow Branches PRs Issues Status"),
            ("⇥ / ⇧⇥", "next / previous view"),
            ("j / k", "scroll the list"),
            ("/", "filter the list"),
            ("d  c", "diff / create a PR"),
            ("m", "scope: this repo or my work"),
            ("o", "open on GitHub"),
            ("r  q", "refresh / close the tab"),
        ],
    ),
    (
        "Task board  (⌃Space o)",
        &[
            ("a", "new task"),
            ("s  d  m", "start / done / merge"),
            ("x  D", "release / delete"),
            ("o  ⏎", "detail / jump to worker pane"),
            ("j / k", "move the cursor"),
            ("q", "close the board"),
        ],
    ),
    (
        "Folder picker  (⌃Space n)",
        &[
            ("j / k", "move"),
            ("→ / ←", "enter folder / go up"),
            ("", "open the folder as a node"),
            ("n  w", "new folder / open as a worktree"),
            ("esc", "cancel"),
        ],
    ),
    (
        "Copy & paste",
        &[
            ("drag", "select text; on release it copies to the clipboard"),
            (
                "shift+drag",
                "select inside a mouse-aware app (e.g. an agent)",
            ),
            (
                "⌘V  Ctrl+⇧V",
                "your terminal's paste, into the focused pane",
            ),
        ],
    ),
    (
        "Mouse",
        &[
            ("click", "focus a pane, or hit a row / button"),
            ("right-click", "context menu: pane, node, agent, tab"),
            ("wheel", "scroll the pane's history, or a list"),
            ("drag divider", "resize the split"),
            ("click branch", "open that node's git tab"),
            ("tap pane", "zoom it (touch / mobile)"),
        ],
    ),
];

/// Total reference rows (not counting the section headings) — the authoritative
/// count for the Keys-tab cursor, which steps through commands then these.
pub fn key_reference_rows() -> usize {
    KEY_REFERENCE.iter().map(|(_, rows)| rows.len()).sum()
}

/// Canonical string for a key in prefix mode (the opening `Ctrl` is already
/// consumed). Used both to match presses and to display/store bindings.
pub fn key_string(key: &KeyEvent) -> Option<String> {
    Some(match key.code {
        KeyCode::Char(c) => c.to_string(),
        KeyCode::Left => "".into(),
        KeyCode::Right => "".into(),
        KeyCode::Up => "".into(),
        KeyCode::Down => "".into(),
        KeyCode::Tab => "".into(),
        KeyCode::BackTab => "⇧⇥".into(),
        _ => return None,
    })
}

/// Build the active `key → Cmd` map from the (id → key) config overrides, on top
/// of the defaults, plus the always-on fixed aliases.
pub fn build_keymap(overrides: &HashMap<String, String>) -> HashMap<String, Cmd> {
    let mut m = HashMap::new();
    for &cmd in Cmd::ALL {
        let key = match overrides.get(cmd.id()) {
            Some(k) if k.is_empty() => continue, // explicitly unbound
            Some(k) => k.clone(),
            None => cmd.default_key().to_string(),
        };
        m.insert(key, cmd);
    }
    // Fixed aliases — only if the slot isn't taken by a user binding.
    for (k, cmd) in [
        ("h", Cmd::FocusLeft),
        ("j", Cmd::FocusDown),
        ("k", Cmd::FocusUp),
        ("l", Cmd::FocusRight),
        ("q", Cmd::Detach),
        ("X", Cmd::ClosePane),
        ("-", Cmd::SplitDown),
        ("", Cmd::NextTab),
        ("⇧⇥", Cmd::PrevTab),
    ] {
        m.entry(k.to_string()).or_insert(cmd);
    }
    m
}

impl App {
    /// The key currently bound to `cmd` (override or default), for display.
    pub fn key_for(&self, cmd: Cmd) -> String {
        self.config
            .keybindings
            .get(cmd.id())
            .cloned()
            .unwrap_or_else(|| cmd.default_key().to_string())
    }

    /// Rebind `cmd` to `key`, persist, and rebuild the active keymap. If `key`
    /// was used by another command, that one is cleared (so it can be rebound).
    pub fn rebind(&mut self, cmd: Cmd, key: String) {
        // Drop any other command that currently uses this key.
        let others: Vec<Cmd> = Cmd::ALL
            .iter()
            .copied()
            .filter(|c| *c != cmd && self.key_for(*c) == key)
            .collect();
        for other in others {
            self.config
                .keybindings
                .insert(other.id().to_string(), String::new());
        }
        self.config.keybindings.insert(cmd.id().to_string(), key);
        self.keymap = build_keymap(&self.config.keybindings);
        crate::config::save(&self.config);
    }

    /// Reset `cmd` to its default key (drop any override), persist, and rebuild.
    pub fn reset_binding(&mut self, cmd: Cmd) {
        self.config.keybindings.remove(cmd.id());
        self.keymap = build_keymap(&self.config.keybindings);
        crate::config::save(&self.config);
    }

    /// Run a prefix command.
    pub fn run_cmd(&mut self, cmd: Cmd) {
        match cmd {
            Cmd::FocusLeft => self.focus_dir(Dir::Left),
            Cmd::FocusDown => self.focus_dir(Dir::Down),
            Cmd::FocusUp => self.focus_dir(Dir::Up),
            Cmd::FocusRight => self.focus_dir(Dir::Right),
            Cmd::SplitRight => self.split(Axis::Col),
            Cmd::SplitDown => self.split(Axis::Row),
            // Fork the focused agent pane's session into a new pane (no-op if it
            // isn't a fork-capable agent).
            Cmd::ForkSession => {
                self.fork_pane(self.layout().focus);
            }
            Cmd::ClosePane => {
                // A git tab / orchestration board has no real pane — close the
                // dashboard tab instead.
                if self.active_is_git() {
                    self.close_git_tab();
                } else if self.active_is_orch() {
                    self.close_orch_board();
                } else {
                    self.close_pane(self.layout().focus);
                }
            }
            Cmd::ZoomPane => self.zoomed = !self.zoomed,
            Cmd::ResizeMode => self.enter_resize_mode(),
            Cmd::NewTab => self.new_tab(),
            Cmd::NextTab => self.cycle_tab(1),
            Cmd::PrevTab => self.cycle_tab(-1),
            Cmd::NewWorkspace => self.open_folder_picker(),
            Cmd::CloseWorkspace => {
                let i = self.active_ws;
                self.close_workspace(i);
            }
            Cmd::NextWorkspace => self.cycle_workspace(1),
            Cmd::PrevWorkspace => self.cycle_workspace(-1),
            Cmd::NewWorktree => self.open_worktree_prompt(),
            Cmd::OpenGit => self.open_git_tab_active(),
            Cmd::OpenBoard => self.open_orch_board(),
            Cmd::OpenSettings => self.open_settings(),
            Cmd::ToggleSidebar => self.toggle_side(crate::app::Side::Left),
            Cmd::ToggleRightSidebar => self.toggle_side(crate::app::Side::Right),
            Cmd::ToggleAgents => self.agents_active_only = !self.agents_active_only,
            Cmd::ToggleFiles => self.toggle_files_dock(),
            Cmd::Switcher => self.toggle_switcher(),
            Cmd::Detach => self.detach_requested = true,
        }
    }
}

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

    #[test]
    fn defaults_and_aliases_resolve() {
        let m = build_keymap(&HashMap::new());
        assert_eq!(m.get(""), Some(&Cmd::FocusLeft));
        assert_eq!(m.get("h"), Some(&Cmd::FocusLeft)); // vim alias
        assert_eq!(m.get(""), Some(&Cmd::NextTab));
        assert_eq!(m.get("N"), Some(&Cmd::NewWorkspace));
        // every command is reachable by its default key
        for &c in Cmd::ALL {
            assert!(m.values().any(|v| *v == c), "{c:?} bound");
        }
    }

    #[test]
    fn rebind_moves_the_key() {
        let mut o = HashMap::new();
        o.insert(Cmd::NewTab.id().to_string(), "t".to_string());
        let m = build_keymap(&o);
        assert_eq!(m.get("t"), Some(&Cmd::NewTab));
        assert_ne!(m.get("c"), Some(&Cmd::NewTab)); // old default freed
    }

    #[test]
    fn prefix_question_opens_help_and_any_key_closes() {
        use crate::event::AppEvent;
        use ratatui::crossterm::event::KeyModifiers;
        let prefix = || AppEvent::Key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::CONTROL));
        let ch = |c| AppEvent::Key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        assert!(!app.help_open);
        app.handle_event(prefix());
        app.handle_event(ch('?')); // Ctrl+Space ? opens the cheat-sheet
        assert!(app.help_open, "? opened the help overlay");
        app.handle_event(ch('x')); // any key dismisses it (and is swallowed)
        assert!(!app.help_open, "next key closed the overlay");
        // The swallowed key must not have acted (e.g. closed a pane).
        assert_eq!(app.panes.len(), 1);
    }

    #[test]
    fn command_works_as_both_two_step_and_held_chord() {
        let _env = crate::persist::test_env("two-step-chord");
        use crate::event::AppEvent;
        use ratatui::crossterm::event::KeyModifiers;
        let prefix = || AppEvent::Key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::CONTROL));
        let key = |c, m| AppEvent::Key(KeyEvent::new(KeyCode::Char(c), m));

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        let tabs = app.ws().tabs.len();

        // Two-step: Ctrl+Space, release, then plain `c`.
        app.handle_event(prefix());
        app.handle_event(key('c', KeyModifiers::NONE));
        assert_eq!(app.ws().tabs.len(), tabs + 1, "two-step prefix opens a tab");

        // Held chord: Ctrl+Space then Ctrl+c (Ctrl never released).
        app.handle_event(prefix());
        app.handle_event(key('c', KeyModifiers::CONTROL));
        assert_eq!(app.ws().tabs.len(), tabs + 2, "held chord opens a tab too");

        // The same held-chord works for `v` (split): Ctrl+Space+Ctrl+v.
        let panes = app.layout().len();
        app.handle_event(prefix());
        app.handle_event(key('v', KeyModifiers::CONTROL));
        assert_eq!(
            app.layout().len(),
            panes + 1,
            "Ctrl+Space+v splits the pane"
        );
    }
}