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
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
//! Session list panel. Walks the explicit-membership `SidebarModel`,
//! emitting one row per visible entry (ungrouped session, section
//! header, or section member).

use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Paragraph};
use ratatui::Frame;

use crate::app::AppState;
use crate::sidebar::{Container, Section, VisibleEntry};
use crate::tmux::detector::Status;
use crate::tmux::session::SessionView;
use crate::ui::Theme;

pub fn render(frame: &mut Frame<'_>, area: Rect, state: &AppState, theme: &Theme) {
    let block = Block::default().style(Style::default().bg(theme.bg));

    let visible = state.sidebar.visible();

    if visible.is_empty() {
        let lines = vec![
            Line::from(""),
            Line::from(Span::styled(
                "  no tmux sessions",
                Style::default().fg(theme.text_muted),
            )),
            Line::from(Span::styled(
                "  (press n to create · g for a section)",
                Style::default().fg(theme.text_muted),
            )),
        ];
        frame.render_widget(Paragraph::new(lines).block(block), area);
        return;
    }

    // Track the section-index (0-based) as we walk the visible list so
    // section headers can show their numeric jump key.
    let mut section_idx: usize = 0;
    let mut out: Vec<Line<'_>> = Vec::with_capacity(visible.len() * 2);
    // In narrow mode the preview pane (and its tab strip) is hidden,
    // so multi-tab container membership is otherwise invisible. Emit
    // an extra "tabs:" line per container in that case.
    let narrow = state.term_size.0 < crate::ui::layout::PREVIEW_MIN_WIDTH;
    // For each entry, record the first/last line index it produced.
    // Used after the build to compute a scroll offset that keeps the
    // selected entry fully visible — without this, on small screens
    // (mobile / mosh / phone) the list never scrolls and entries past
    // the viewport bottom are unreachable.
    let mut entry_first_line: Vec<usize> = Vec::with_capacity(visible.len());
    let mut entry_last_line: Vec<usize> = Vec::with_capacity(visible.len());
    for (i, entry) in visible.iter().enumerate() {
        let selected = i == state.selected;
        let start = out.len();
        match entry {
            VisibleEntry::Ungrouped(c) => {
                let tabs = c.members.len() as u16;
                let bg_busy = background_activity(state, c);
                match state.session_by_name(&c.active) {
                    Some(v) => {
                        out.push(render_primary_line(
                            v, selected, false, tabs, bg_busy, area.width, theme,
                        ));
                        out.push(render_meta_line(v, selected, false, area.width, theme));
                        if narrow && tabs > 1 {
                            out.push(render_tabs_line(
                                c, state, selected, false, area.width, theme,
                            ));
                        }
                    }
                    None => {
                        let label = state.dead_display_for(&c.active);
                        out.push(render_missing_line(
                            &label, selected, false, area.width, theme,
                        ));
                    }
                }
            }
            VisibleEntry::SectionHeader(s) => {
                let jump_key = if section_idx < 9 {
                    Some((section_idx as u8 + b'1') as char)
                } else {
                    None
                };
                out.push(render_section_line(
                    s, selected, jump_key, area.width, theme,
                ));
                section_idx += 1;
            }
            VisibleEntry::Member { container, .. } => {
                let tabs = container.members.len() as u16;
                let bg_busy = background_activity(state, container);
                match state.session_by_name(&container.active) {
                    Some(v) => {
                        out.push(render_primary_line(
                            v, selected, true, tabs, bg_busy, area.width, theme,
                        ));
                        out.push(render_meta_line(v, selected, true, area.width, theme));
                        if narrow && tabs > 1 {
                            out.push(render_tabs_line(
                                container, state, selected, true, area.width, theme,
                            ));
                        }
                    }
                    None => {
                        let label = state.dead_display_for(&container.active);
                        out.push(render_missing_line(
                            &label, selected, true, area.width, theme,
                        ));
                    }
                }
            }
        }
        entry_first_line.push(start);
        entry_last_line.push(out.len().saturating_sub(1));
    }

    let total_lines = out.len();
    let viewport = area.height as usize;
    // Compute a scroll offset that keeps the selected entry fully
    // visible. No persistent scroll state — derive every frame from
    // the current selection so j/k, jump keys, and section toggles
    // all stay consistent without sync logic. Without this, on small
    // screens (mobile / mosh) entries past the viewport bottom were
    // unreachable.
    let scroll: u16 = if total_lines <= viewport || viewport == 0 {
        0
    } else {
        let sel = state.selected.min(entry_first_line.len().saturating_sub(1));
        let first = entry_first_line[sel];
        let last = entry_last_line[sel];
        let max_scroll = total_lines.saturating_sub(viewport);
        // Center-ish: aim for the selection at ~1/3 from the top so
        // there's context above and below as the user navigates.
        let target = viewport / 3;
        let mut s = first.saturating_sub(target);
        // Always keep the entry's last line on-screen — matters for
        // 2-line session entries near the bottom of the list.
        if last >= s + viewport {
            s = (last + 1).saturating_sub(viewport);
        }
        s.min(max_scroll) as u16
    };

    let p = Paragraph::new(out).block(block).scroll((scroll, 0));
    frame.render_widget(p, area);
}

/// Lines a single visible entry occupies in the rendered list.
/// Sessions render as two lines (primary + meta); section headers
/// and dead-row sessions are single-line. Kept as a small shared
/// helper so [`entry_at_row`] stays in lockstep with [`render`].
fn entry_line_count(state: &AppState, entry: &VisibleEntry<'_>) -> u16 {
    match entry {
        VisibleEntry::Ungrouped(c) => {
            if state.session_by_name(&c.active).is_some() {
                2
            } else {
                1
            }
        }
        VisibleEntry::SectionHeader(_) => 1,
        VisibleEntry::Member { container, .. } => {
            if state.session_by_name(&container.active).is_some() {
                2
            } else {
                1
            }
        }
    }
}

/// Same scroll heuristic as [`render`] but computed from line
/// counts only — no rendered `Line`s needed. Centers the selected
/// entry at roughly the top third of the viewport while keeping
/// its last line on-screen.
fn compute_scroll(counts: &[u16], selected: usize, viewport: u16) -> u16 {
    let total_lines: u16 = counts.iter().copied().fold(0u16, u16::saturating_add);
    if total_lines <= viewport || viewport == 0 {
        return 0;
    }
    let sel = selected.min(counts.len().saturating_sub(1));
    let first: u16 = counts
        .iter()
        .take(sel)
        .copied()
        .fold(0u16, u16::saturating_add);
    let last = first.saturating_add(counts.get(sel).copied().unwrap_or(0).saturating_sub(1));
    let max_scroll = total_lines.saturating_sub(viewport);
    let target = viewport / 3;
    let mut s = first.saturating_sub(target);
    if last >= s.saturating_add(viewport) {
        s = last.saturating_add(1).saturating_sub(viewport);
    }
    s.min(max_scroll)
}

/// Map an absolute terminal row (mouse Y coord) to a visible-entry
/// index in the sidebar. Returns `None` when the row is outside the
/// list rect or past the last entry's last line. Used by the click
/// handler so a mouse-down on a session row jumps the selection
/// straight to it (same effect as pressing j/k until the cursor
/// lands).
pub fn entry_at_row(state: &AppState, list_area: Rect, abs_row: u16) -> Option<usize> {
    if abs_row < list_area.y || abs_row >= list_area.y.saturating_add(list_area.height) {
        return None;
    }
    let visible = state.sidebar.visible();
    if visible.is_empty() {
        return None;
    }
    let counts: Vec<u16> = visible.iter().map(|e| entry_line_count(state, e)).collect();
    let scroll = compute_scroll(&counts, state.selected, list_area.height);
    let local_row = abs_row - list_area.y;
    let target_line = local_row.saturating_add(scroll);
    let mut acc: u16 = 0;
    for (i, c) in counts.iter().enumerate() {
        if target_line >= acc && target_line < acc + c {
            return Some(i);
        }
        acc = acc.saturating_add(*c);
    }
    None
}

fn status_color(status: Status, theme: &Theme) -> Color {
    match status {
        Status::Running => theme.status_running,
        Status::Waiting => theme.status_waiting,
        Status::Idle | Status::Unknown => theme.status_idle,
        Status::Error => theme.status_error,
    }
}

fn row_bg(selected: bool, theme: &Theme) -> Color {
    if selected {
        theme.selection_bg
    } else {
        theme.panel
    }
}

fn render_section_line(
    section: &Section,
    selected: bool,
    jump_key: Option<char>,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let bg = row_bg(selected, theme);
    let marker = if selected { "" } else { " " };
    let marker_style = if selected {
        Style::default().fg(theme.accent).bg(bg)
    } else {
        Style::default().fg(bg).bg(bg)
    };
    let title_style = Style::default()
        .fg(theme.accent)
        .bg(bg)
        .add_modifier(Modifier::BOLD);
    let key_style = Style::default().fg(theme.text_muted).bg(bg);

    let key_prefix = match jump_key {
        Some(k) => format!("{} ", k),
        None => "  ".to_string(),
    };
    let count_label = format!("  ({})", section.members.len());
    let count_style = Style::default().fg(theme.text_muted).bg(bg);

    // Disclosure glyph follows the standard tree convention: ▸ when
    // the section is closed (members hidden), ▾ when open. An empty
    // section always shows ▸ since there's nothing to collapse.
    let glyph = if section.collapsed || section.members.is_empty() {
        ""
    } else {
        ""
    };
    let label = format!("{glyph} {}", section.name.to_uppercase());
    let used = 3 + key_prefix.chars().count() + label.chars().count() + count_label.chars().count();
    let pad = (width as usize).saturating_sub(used);

    Line::from(vec![
        Span::styled(format!(" {} ", marker), marker_style),
        Span::styled(key_prefix, key_style),
        Span::styled(label, title_style),
        Span::styled(count_label, count_style),
        Span::styled(" ".repeat(pad), Style::default().bg(bg)),
    ])
}

/// True iff at least one **background** tab (anything other than
/// `container.active`) is currently Running or Waiting — i.e. a
/// tab the user can't see is doing work right now. Used to render
/// a small accent dot on the sidebar row so multi-tab containers
/// surface background activity without the user having to cycle
/// through tabs.
fn background_activity(state: &AppState, container: &crate::sidebar::Container) -> bool {
    use crate::tmux::detector::Status;
    container
        .members
        .iter()
        .filter(|m| *m != &container.active)
        .filter_map(|m| state.session_by_name(m))
        .any(|v| matches!(v.status, Status::Running | Status::Waiting))
}

fn render_primary_line(
    view: &SessionView,
    selected: bool,
    indented: bool,
    tabs: u16,
    bg_busy: bool,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let marker = if selected { "" } else { " " };
    let bg = row_bg(selected, theme);

    let marker_style = if selected {
        Style::default().fg(theme.accent).bg(bg)
    } else {
        Style::default().fg(bg).bg(bg)
    };
    let name_style = if selected {
        Style::default()
            .fg(theme.text)
            .bg(bg)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(theme.text).bg(bg)
    };
    let status_style = Style::default().fg(status_color(view.status, theme)).bg(bg);

    let glyph = view.status.glyph().to_string();
    let name = view.display().to_string();
    // `(N)` tab-count badge for multi-tab containers. Hidden when
    // tabs <= 1 so single-tab rows render identically to the
    // pre-tabs sidebar.
    let tab_label = if tabs > 1 {
        format!("  ({})", tabs)
    } else {
        String::new()
    };
    // Single accent dot when a non-active tab is busy. Same row,
    // right after the (N) badge, so the user can tell at a glance
    // whether a background tab is doing work.
    let activity_label = if tabs > 1 && bg_busy { "" } else { "" };
    let windows_label = format!("  {}w", view.session.windows);
    let attached_label = if view.session.attached {
        "  •attached"
    } else {
        ""
    };
    let indent = if indented { "  " } else { "" };

    let used = 3
        + indent.chars().count()
        + glyph.chars().count()
        + 2
        + name.chars().count()
        + tab_label.chars().count()
        + activity_label.chars().count()
        + windows_label.chars().count()
        + attached_label.chars().count();
    let pad = (width as usize).saturating_sub(used);

    let mut spans = vec![
        Span::styled(format!(" {} ", marker), marker_style),
        Span::styled(indent, Style::default().bg(bg)),
        Span::styled(glyph, status_style),
        Span::styled("  ", Style::default().bg(bg)),
        Span::styled(name, name_style),
    ];
    if !tab_label.is_empty() {
        spans.push(Span::styled(
            tab_label,
            Style::default().fg(theme.text_muted).bg(bg),
        ));
    }
    if !activity_label.is_empty() {
        spans.push(Span::styled(
            activity_label,
            Style::default().fg(theme.accent).bg(bg),
        ));
    }
    spans.push(Span::styled(
        windows_label,
        Style::default().fg(theme.text_muted).bg(bg),
    ));
    if view.session.attached {
        spans.push(Span::styled(
            attached_label,
            Style::default().fg(theme.status_running).bg(bg),
        ));
    }
    spans.push(Span::styled(" ".repeat(pad), Style::default().bg(bg)));

    Line::from(spans)
}

fn render_meta_line(
    view: &SessionView,
    selected: bool,
    indented: bool,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let bg = row_bg(selected, theme);
    let meta_style = Style::default().fg(theme.text_muted).bg(bg);

    // Base indent aligns under the session name ("  ▌  ○  " = 3+1+2=6 cols
    // of leading padding after the marker); indented rows add 2 more.
    let base_indent: &str = "       ";
    let extra = if indented { "  " } else { "" };

    let agent = view.session.agent.as_deref();
    let path = view.session.best_path();

    let body = match (agent, path) {
        (Some(a), Some(p)) => format!("{a} · {}", shorten_path(p)),
        (Some(a), None) => a.to_string(),
        (None, Some(p)) => shorten_path(p),
        (None, None) => String::new(),
    };

    let lead = base_indent.chars().count() + extra.chars().count();
    let max_body = (width as usize).saturating_sub(lead).saturating_sub(1);
    let body = truncate_to(&body, max_body);

    let used = lead + body.chars().count();
    let pad = (width as usize).saturating_sub(used);

    Line::from(vec![
        Span::styled(base_indent, Style::default().bg(bg)),
        Span::styled(extra, Style::default().bg(bg)),
        Span::styled(body, meta_style),
        Span::styled(" ".repeat(pad), Style::default().bg(bg)),
    ])
}

/// Narrow-mode tab listing under a multi-tab container row. The
/// preview pane (and its tab strip) is hidden when the terminal is
/// below `PREVIEW_MIN_WIDTH`, so without this line a user on mobile
/// can't tell what tabs a container actually holds. Active tab gets
/// the accent background so it stands out at a glance.
fn render_tabs_line(
    container: &Container,
    state: &AppState,
    selected: bool,
    indented: bool,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let bg = row_bg(selected, theme);
    let muted = Style::default().fg(theme.text_muted).bg(bg);
    let active_style = Style::default().fg(theme.on(theme.accent)).bg(theme.accent);

    let base_indent: &str = "       ";
    let extra = if indented { "  " } else { "" };
    let lead = base_indent.chars().count() + extra.chars().count();
    let max_body = (width as usize).saturating_sub(lead).saturating_sub(1);

    let mut spans: Vec<Span<'static>> = Vec::with_capacity(container.members.len() * 2 + 2);
    spans.push(Span::styled(base_indent, Style::default().bg(bg)));
    spans.push(Span::styled(extra, Style::default().bg(bg)));

    let mut used = 0usize;
    let names: Vec<(String, bool)> = container
        .members
        .iter()
        .map(|m| {
            let name = state
                .session_by_name(m)
                .map(|v| v.display().to_string())
                .unwrap_or_else(|| m.clone());
            (name, m == &container.active)
        })
        .collect();

    for (i, (name, active)) in names.iter().enumerate() {
        if i > 0 {
            let sep = " · ";
            if used + sep.chars().count() >= max_body {
                break;
            }
            spans.push(Span::styled(sep.to_string(), muted));
            used += sep.chars().count();
        }
        let remaining = max_body.saturating_sub(used);
        let label = truncate_to(name, remaining);
        let label_len = label.chars().count();
        if *active {
            spans.push(Span::styled(format!(" {label} "), active_style));
            used += label_len + 2;
        } else {
            spans.push(Span::styled(label, muted));
            used += label_len;
        }
    }

    let pad = (width as usize).saturating_sub(lead + used);
    spans.push(Span::styled(" ".repeat(pad), Style::default().bg(bg)));
    Line::from(spans)
}

fn render_missing_line(
    label: &str,
    selected: bool,
    indented: bool,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let bg = row_bg(selected, theme);
    let extra = if indented { "  " } else { "" };
    // Label is the resolved display name (from Recents lookup) or
    // a slug fallback — see `AppState::dead_display_for`. `R` on this
    // row recreates the session from its persisted spec.
    let body = format!("  ? {}", label);
    let used = extra.chars().count() + body.chars().count();
    let pad = (width as usize).saturating_sub(used);
    Line::from(vec![
        Span::styled(extra, Style::default().bg(bg)),
        Span::styled(body, Style::default().fg(theme.text_muted).bg(bg)),
        Span::styled(" ".repeat(pad), Style::default().bg(bg)),
    ])
}

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()
    }
}

fn truncate_to(s: &str, max: usize) -> String {
    let len = s.chars().count();
    if len <= max {
        return s.to_string();
    }
    if max <= 1 {
        return "".to_string();
    }
    let tail: String = s.chars().skip(len - (max - 1)).collect();
    format!("{tail}")
}