bosun-tmux 2.0.5

Tmux-native orchestrator for AI agent sessions
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
//! QuickJumpModal — fast type-ahead session switcher.
//!
//! Opened from the main session list with `/`. Shows every live
//! bosun-managed session with a live filter. Type to narrow, ↑/↓ to
//! highlight, Enter to attach, Esc to cancel.
//!
//! On Enter the modal returns `Close(Some(Command::Attach { name }))`;
//! the app loop intercepts `Command::Attach` from a closed modal and
//! sets `pending_attach` instead of forwarding it to the tmux actor
//! (the actor doesn't handle attach — the app loop does the
//! tty-handover dance inline).

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Widget};
use ratatui::Frame;

use crate::events::Command;
use crate::ui::Theme;

use super::{center_rect, Modal, ModalResult};

const MODAL_WIDTH: u16 = 70;
const MODAL_MIN_HEIGHT: u16 = 10;
const MODAL_MAX_HEIGHT: u16 = 30;
/// Title + blank + filter + blank + 2-row inset = 6 chrome rows.
const MODAL_CHROME_ROWS: u16 = 6;

/// One row in the picker. Carries both the internal tmux name (what
/// we attach to) and the human-readable bits the user sees and filters
/// on.
#[derive(Debug, Clone)]
pub struct QuickJumpRow {
    pub internal: String,
    pub display: String,
    pub agent: Option<String>,
    pub path: Option<String>,
    pub attached: bool,
}

pub struct QuickJumpModal {
    rows: Vec<QuickJumpRow>,
    filter: String,
    /// Index into the current filtered view. Clamped on every filter
    /// change / nav key. 0 (top-most match) on open and on every
    /// keystroke that grows the filter.
    selected: usize,
}

impl QuickJumpModal {
    pub fn new(rows: Vec<QuickJumpRow>) -> Self {
        Self {
            rows,
            filter: String::new(),
            selected: 0,
        }
    }

    /// Indices into `self.rows` that pass the current filter, in the
    /// same order as `rows`. Empty filter ⇒ everyone.
    fn filtered_indices(&self) -> Vec<usize> {
        if self.filter.is_empty() {
            return (0..self.rows.len()).collect();
        }
        let needle = self.filter.to_lowercase();
        self.rows
            .iter()
            .enumerate()
            .filter(|(_, r)| row_matches(r, &needle))
            .map(|(i, _)| i)
            .collect()
    }

    fn selected_row(&self) -> Option<&QuickJumpRow> {
        let indices = self.filtered_indices();
        indices.get(self.selected).and_then(|i| self.rows.get(*i))
    }

    fn clamp_selection(&mut self) {
        let len = self.filtered_indices().len();
        if len == 0 {
            self.selected = 0;
        } else if self.selected >= len {
            self.selected = len - 1;
        }
    }

    fn move_up(&mut self) {
        self.selected = self.selected.saturating_sub(1);
    }

    fn move_down(&mut self) {
        let len = self.filtered_indices().len();
        if len > 0 && self.selected + 1 < len {
            self.selected += 1;
        }
    }
}

impl Modal for QuickJumpModal {
    fn id(&self) -> &'static str {
        "quickjump"
    }

    fn handle(&mut self, key: KeyEvent) -> ModalResult {
        // Ctrl+C always closes — matches other modals.
        if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
            return ModalResult::Close(None);
        }

        match key.code {
            KeyCode::Esc => ModalResult::Close(None),
            KeyCode::Enter => match self.selected_row() {
                Some(r) => ModalResult::Close(Some(Command::Attach {
                    name: r.internal.clone(),
                })),
                None => ModalResult::Consumed,
            },
            KeyCode::Up => {
                self.move_up();
                ModalResult::Consumed
            }
            KeyCode::Down => {
                self.move_down();
                ModalResult::Consumed
            }
            KeyCode::Backspace => {
                self.filter.pop();
                self.clamp_selection();
                ModalResult::Consumed
            }
            KeyCode::Char(c)
                if !key.modifiers.contains(KeyModifiers::CONTROL)
                    && !key.modifiers.contains(KeyModifiers::ALT) =>
            {
                self.filter.push(c);
                // Always jump to the top-most match on filter change —
                // matches fzf/skim behavior and is what users expect.
                self.selected = 0;
                ModalResult::Consumed
            }
            _ => ModalResult::Consumed,
        }
    }

    fn render(&self, frame: &mut Frame<'_>, area: Rect, theme: &Theme) {
        let desired = (self.rows.len() as u16).saturating_add(MODAL_CHROME_ROWS);
        let height = desired
            .clamp(MODAL_MIN_HEIGHT, MODAL_MAX_HEIGHT)
            .min(area.height.saturating_sub(2).max(MODAL_MIN_HEIGHT));
        let rect = center_rect(area, MODAL_WIDTH, height);
        let body_bg = theme.panel_alt;
        let buf = frame.buffer_mut();

        // Shadow behind the modal — same pattern as RecentsModal.
        if rect.x + rect.width < area.x + area.width && rect.y + rect.height < area.y + area.height
        {
            let shadow = Rect::new(rect.x + 1, rect.y + 1, rect.width, rect.height);
            let style = Style::default().bg(theme.shadow);
            for y in shadow.top()..shadow.bottom() {
                for x in shadow.left()..shadow.right() {
                    buf[(x, y)].set_style(style);
                }
            }
        }

        // Body fill.
        let body_style = Style::default().bg(body_bg);
        for y in rect.top()..rect.bottom() {
            for x in rect.left()..rect.right() {
                let cell = &mut buf[(x, y)];
                cell.set_char(' ');
                cell.set_style(body_style);
            }
        }

        // Left accent bar.
        let accent_style = Style::default().bg(theme.accent);
        for y in rect.top()..rect.bottom() {
            let cell = &mut buf[(rect.left(), y)];
            cell.set_char(' ');
            cell.set_style(accent_style);
        }

        let inner = Rect::new(
            rect.x + 3,
            rect.y + 1,
            rect.width.saturating_sub(4),
            rect.height.saturating_sub(2),
        );

        let filtered = self.filtered_indices();

        let mut lines: Vec<Line<'static>> = Vec::with_capacity(16);
        lines.push(Line::from(vec![
            Span::styled(
                "Quick switch",
                Style::default()
                    .fg(theme.text)
                    .bg(body_bg)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                "   esc · ↑↓ select · ↵ attach",
                Style::default().fg(theme.text_muted).bg(body_bg),
            ),
        ]));
        lines.push(Line::from(""));

        let filter_display = format!(" filter: {}", self.filter);
        lines.push(Line::from(vec![Span::styled(
            filter_display,
            Style::default().fg(theme.text).bg(theme.bg),
        )]));
        lines.push(Line::from(""));

        let max_rows = (inner.height as usize).saturating_sub(4);
        if self.rows.is_empty() {
            lines.push(Line::from(Span::styled(
                "  (no sessions to switch to)",
                Style::default().fg(theme.text_muted).bg(body_bg),
            )));
        } else if filtered.is_empty() {
            lines.push(Line::from(Span::styled(
                "  (no matches)",
                Style::default().fg(theme.text_muted).bg(body_bg),
            )));
        } else {
            for (vi, row_idx) in filtered.iter().enumerate().take(max_rows) {
                let r = &self.rows[*row_idx];
                lines.push(render_row(r, vi == self.selected, inner.width, theme));
            }
        }

        Paragraph::new(lines)
            .style(Style::default().bg(body_bg))
            .render(inner, frame.buffer_mut());
    }
}

/// Case-insensitive substring match across the bits a user is likely
/// to type — display name first, then agent, then path. Matches
/// RecentsModal's matching contract so the two pickers feel
/// identical.
fn row_matches(r: &QuickJumpRow, needle: &str) -> bool {
    if r.display.to_lowercase().contains(needle) {
        return true;
    }
    if let Some(a) = &r.agent {
        if a.to_lowercase().contains(needle) {
            return true;
        }
    }
    if let Some(p) = &r.path {
        if p.to_lowercase().contains(needle) {
            return true;
        }
    }
    false
}

fn render_row(r: &QuickJumpRow, selected: bool, width: u16, theme: &Theme) -> Line<'static> {
    let marker = if selected { "" } else { " " };
    let row_bg = if selected {
        theme.selection_bg
    } else {
        theme.panel_alt
    };

    let marker_style = if selected {
        Style::default().fg(theme.accent).bg(row_bg)
    } else {
        Style::default().fg(row_bg).bg(row_bg)
    };
    let name_style = if selected {
        Style::default()
            .fg(theme.text)
            .bg(row_bg)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme.text).bg(row_bg)
    };
    let meta_style = Style::default().fg(theme.text_muted).bg(row_bg);
    let attached_style = Style::default().fg(theme.status_running).bg(row_bg);

    let path_short = r.path.as_deref().map(shorten_path).unwrap_or_default();
    let agent = r.agent.as_deref().unwrap_or("");
    let attached_label = if r.attached { "  •attached" } else { "" };

    let mut spans = vec![
        Span::styled(format!(" {} ", marker), marker_style),
        Span::styled(r.display.clone(), name_style),
    ];
    if !agent.is_empty() {
        spans.push(Span::styled(format!("  · {}", agent), meta_style));
    }
    if !path_short.is_empty() {
        spans.push(Span::styled(format!("  · {}", path_short), meta_style));
    }
    if !attached_label.is_empty() {
        spans.push(Span::styled(attached_label, attached_style));
    }

    let used: usize = spans.iter().map(|s| s.content.chars().count()).sum();
    let pad = (width as usize).saturating_sub(used);
    spans.push(Span::styled(" ".repeat(pad), Style::default().bg(row_bg)));

    Line::from(spans)
}

fn shorten_path(p: &str) -> String {
    let home = std::env::var("HOME").unwrap_or_default();
    if !home.is_empty() && p.starts_with(&home) {
        format!("~{}", &p[home.len()..])
    } else {
        p.to_string()
    }
}

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

    fn row(internal: &str, display: &str, agent: &str, path: &str) -> QuickJumpRow {
        QuickJumpRow {
            internal: internal.into(),
            display: display.into(),
            agent: Some(agent.into()),
            path: Some(path.into()),
            attached: false,
        }
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    #[test]
    fn empty_list_enter_is_noop() {
        let mut m = QuickJumpModal::new(vec![]);
        assert!(m.selected_row().is_none());
        assert!(matches!(
            m.handle(key(KeyCode::Enter)),
            ModalResult::Consumed
        ));
    }

    #[test]
    fn typing_narrows_filter_across_display_agent_path() {
        let mut m = QuickJumpModal::new(vec![
            row("bosun-work-1", "Work", "claude", "/tmp/proj"),
            row("bosun-play-1", "Play", "codex", "/tmp/play"),
            row("bosun-api-1", "API", "claude", "/srv/api"),
        ]);
        // "cod" matches only the codex entry by agent.
        for c in "cod".chars() {
            m.handle(key(KeyCode::Char(c)));
        }
        assert_eq!(m.filtered_indices().len(), 1);
        assert_eq!(m.selected_row().unwrap().internal, "bosun-play-1");

        // Backspace widens.
        for _ in 0..3 {
            m.handle(key(KeyCode::Backspace));
        }
        assert_eq!(m.filtered_indices().len(), 3);
    }

    #[test]
    fn enter_on_selection_returns_attach_command() {
        let mut m = QuickJumpModal::new(vec![row("bosun-work-1", "Work", "claude", "/tmp")]);
        match m.handle(key(KeyCode::Enter)) {
            ModalResult::Close(Some(Command::Attach { name })) => {
                assert_eq!(name, "bosun-work-1");
            }
            _ => panic!("expected Close(Some(Command::Attach))"),
        }
    }

    #[test]
    fn nav_keys_clamp_at_bounds() {
        let mut m = QuickJumpModal::new(vec![
            row("bosun-a-1", "alpha", "claude", "/tmp"),
            row("bosun-b-1", "beta", "claude", "/tmp"),
        ]);
        assert_eq!(m.selected_row().unwrap().display, "alpha");
        m.handle(key(KeyCode::Down));
        assert_eq!(m.selected_row().unwrap().display, "beta");
        m.handle(key(KeyCode::Down));
        // Clamps at end.
        assert_eq!(m.selected_row().unwrap().display, "beta");
        m.handle(key(KeyCode::Up));
        assert_eq!(m.selected_row().unwrap().display, "alpha");
        m.handle(key(KeyCode::Up));
        // Clamps at start.
        assert_eq!(m.selected_row().unwrap().display, "alpha");
    }

    #[test]
    fn typing_resets_selection_to_top_match() {
        let mut m = QuickJumpModal::new(vec![
            row("bosun-a-1", "alpha", "claude", "/tmp"),
            row("bosun-b-1", "beta", "claude", "/tmp"),
        ]);
        m.handle(key(KeyCode::Down));
        assert_eq!(m.selected, 1);
        // Typing a character that still matches both — selection goes
        // back to 0 (the new top match).
        m.handle(key(KeyCode::Char('a')));
        assert_eq!(m.selected, 0);
    }

    #[test]
    fn esc_closes_with_no_command() {
        let mut m = QuickJumpModal::new(vec![row("bosun-a-1", "alpha", "claude", "/tmp")]);
        assert!(matches!(
            m.handle(key(KeyCode::Esc)),
            ModalResult::Close(None)
        ));
    }
}