broll 0.4.0

Terminal session recorder with searchable, timestamped output
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyModifiers, MouseEventKind};
use ratatui::{
    DefaultTerminal,
    layout::{Constraint, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState},
};
use std::collections::HashMap;
use std::time::Instant;

use super::clipboard;
use super::highlight::highlight_line;
use crate::storage::models::{Annotation, Chunk, ChunkKind};
use crate::storage::Database;

#[derive(PartialEq)]
enum Mode {
    Normal,
    SearchInput,
    Visual,
}

struct ViewApp {
    lines: Vec<Line<'static>>,
    title: String,
    scroll: usize,
    cursor: usize,
    visible_height: usize,
    mode: Mode,
    search_input: String,
    matches: Vec<usize>,
    current_match: Option<usize>,
    /// Line indices to highlight (e.g. the chunk that brought us here from search)
    highlight_lines: Vec<usize>,
    /// Whether this view was opened from the search TUI
    from_search: bool,
    /// Visual mode anchor line
    visual_anchor: usize,
    /// Pending 'y' keypress for yy combo
    pending_y: bool,
    /// Count prefix for yank (e.g. 3yy)
    count_prefix: Option<usize>,
    /// Status message shown temporarily
    status_message: Option<(String, Instant)>,
}

impl ViewApp {
    /// Extract plain text from a styled Line by concatenating span contents.
    fn plain_text(line: &Line) -> String {
        line.spans.iter().map(|s| s.content.as_ref()).collect()
    }

    fn search(&mut self) {
        self.matches.clear();
        self.current_match = None;
        if self.search_input.is_empty() {
            return;
        }
        let query = self.search_input.to_lowercase();
        for (i, line) in self.lines.iter().enumerate() {
            let text = Self::plain_text(line);
            if text.to_lowercase().contains(&query) {
                self.matches.push(i);
            }
        }
        if !self.matches.is_empty() {
            let first = self
                .matches
                .iter()
                .position(|&m| m >= self.scroll)
                .unwrap_or(0);
            self.current_match = Some(first);
            self.scroll = self.matches[first];
        }
    }

    fn next_match(&mut self) {
        if self.matches.is_empty() {
            return;
        }
        let next = match self.current_match {
            Some(i) => (i + 1) % self.matches.len(),
            None => 0,
        };
        self.current_match = Some(next);
        self.scroll = self.matches[next];
    }

    fn prev_match(&mut self) {
        if self.matches.is_empty() {
            return;
        }
        let prev = match self.current_match {
            Some(0) => self.matches.len() - 1,
            Some(i) => i - 1,
            None => 0,
        };
        self.current_match = Some(prev);
        self.scroll = self.matches[prev];
    }

    fn clear_search(&mut self) {
        self.search_input.clear();
        self.matches.clear();
        self.current_match = None;
    }

    fn yank_lines(&mut self, start: usize, end: usize) {
        let end = end.min(self.lines.len());
        if start >= end {
            return;
        }
        // Strip the [HH:MM:SS] timestamp prefix from yanked lines
        let text: String = self.lines[start..end]
            .iter()
            .map(|line| {
                let plain = Self::plain_text(line);
                if plain.starts_with('[')
                    && let Some(pos) = plain.find("] ")
                {
                    return plain[pos + 2..].to_string();
                }
                plain
            })
            .collect::<Vec<_>>()
            .join("\n");
        let msg = clipboard::yank_status(&text, end - start);
        self.status_message = Some((msg, Instant::now()));
    }

    fn yank_current(&mut self, count: usize) {
        let start = self.cursor;
        self.yank_lines(start, start + count);
    }

    fn ensure_cursor_visible(&mut self) {
        if self.cursor < self.scroll {
            self.scroll = self.cursor;
        } else if self.cursor >= self.scroll + self.visible_height {
            self.scroll = self.cursor.saturating_sub(self.visible_height - 1);
        }
    }

    fn move_cursor_down(&mut self, n: usize) {
        self.cursor = (self.cursor + n).min(self.lines.len().saturating_sub(1));
        self.ensure_cursor_visible();
    }

    fn move_cursor_up(&mut self, n: usize) {
        self.cursor = self.cursor.saturating_sub(n);
        self.ensure_cursor_visible();
    }
}

fn build_session_lines(
    chunks: &[Chunk],
    annotations: &[Annotation],
) -> (
    Vec<Line<'static>>,
    HashMap<i64, usize>,
) {
    let mut lines: Vec<Line<'static>> = Vec::new();
    let mut chunk_line_map: HashMap<i64, usize> = HashMap::new();

    if !annotations.is_empty() {
        let note_style = Style::default().fg(Color::Magenta);
        let label_style = Style::default()
            .fg(Color::Magenta)
            .add_modifier(Modifier::BOLD);
        let dim_style = Style::default().fg(Color::DarkGray);

        lines.push(Line::styled("── Notes ──", label_style));

        for ann in annotations {
            let time = ann.created_at.format("%Y-%m-%d %H:%M").to_string();
            lines.push(Line::from(vec![
                Span::styled(
                    format!("[{}] ", time),
                    dim_style,
                ),
                Span::styled(ann.content.clone(), note_style),
            ]));
        }

        lines.push(Line::styled(
            "─".repeat(40),
            dim_style,
        ));
        lines.push(Line::raw(""));
    }

    for chunk in chunks {
        let timestamp = chunk.timestamp.format("%H:%M:%S").to_string();
        let style = match chunk.kind {
            ChunkKind::Input => Style::default().fg(Color::Green).add_modifier(Modifier::BOLD),
            ChunkKind::Output => Style::default().fg(Color::White),
        };
        let prefix_style = Style::default().fg(Color::DarkGray);

        let clean = strip_ansi_escapes::strip_str(&chunk.content);
        let mut first = true;
        for line_text in clean.lines() {
            if line_text.trim().is_empty() {
                continue;
            }
            if first {
                chunk_line_map.insert(chunk.id, lines.len());
                first = false;
            }
            let is_input = chunk.kind == ChunkKind::Input;
            let mut spans = vec![Span::styled(format!("[{timestamp}] "), prefix_style)];
            spans.extend(highlight_line(line_text, style, is_input));
            lines.push(Line::from(spans));
        }
    }

    (lines, chunk_line_map)
}

pub fn run(session_id: &str) -> Result<()> {
    let db = Database::open()?;
    let full_id = db.resolve_session_id(session_id)?;
    let chunks = db.get_session_chunks(&full_id)?;

    if chunks.is_empty() {
        println!("No output recorded for session {session_id}.");
        return Ok(());
    }

    let session = db.get_session_by_id(&full_id)?;
    let title = match session.name {
        Some(name) => format!(" broll view — {} ({}) ", name, &full_id[..8]),
        None => format!(" broll view — session {} ", &full_id[..8]),
    };

    let annotations = db.get_annotations(&full_id)?;
    let (lines, _) = build_session_lines(&chunks, &annotations);
    let mut app = ViewApp {
        lines,
        title,
        scroll: 0,
        cursor: 0,
        visible_height: 0,
        mode: Mode::Normal,
        search_input: String::new(),
        matches: Vec::new(),
        current_match: None,
        highlight_lines: Vec::new(),
        from_search: false,
        visual_anchor: 0,
        pending_y: false,
        count_prefix: None,
        status_message: None,
    };

    let mut terminal = ratatui::init();
    crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture)?;
    let result = run_loop(&mut terminal, &mut app);
    crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture)?;
    ratatui::restore();
    result
}

/// Run view mode within an existing terminal (called from search TUI).
pub fn run_in_terminal(
    terminal: &mut DefaultTerminal,
    session_id: &str,
    scroll_to_chunk: Option<i64>,
) -> Result<()> {
    let db = Database::open()?;
    let full_id = db.resolve_session_id(session_id)?;
    let chunks = db.get_session_chunks(&full_id)?;

    if chunks.is_empty() {
        return Ok(());
    }

    let session = db.get_session_by_id(&full_id)?;
    let title = match session.name {
        Some(name) => format!(" broll view — {} ({}) ", name, &full_id[..8]),
        None => format!(" broll view — session {} ", &full_id[..8]),
    };

    let annotations = db.get_annotations(&full_id)?;
    let (lines, chunk_line_map) = build_session_lines(&chunks, &annotations);
    let initial_scroll = scroll_to_chunk
        .and_then(|cid| chunk_line_map.get(&cid).copied())
        .unwrap_or(0);

    // Find all line indices belonging to the target chunk for highlighting
    let highlight_lines = if let Some(cid) = scroll_to_chunk {
        if let Some(&start) = chunk_line_map.get(&cid) {
            // Find next chunk's start line to determine range
            let next_start = chunk_line_map
                .values()
                .filter(|&&v| v > start)
                .min()
                .copied()
                .unwrap_or(lines.len());
            (start..next_start).collect()
        } else {
            Vec::new()
        }
    } else {
        Vec::new()
    };

    let mut app = ViewApp {
        lines,
        title,
        scroll: initial_scroll,
        cursor: initial_scroll,
        visible_height: 0,
        mode: Mode::Normal,
        search_input: String::new(),
        matches: Vec::new(),
        current_match: None,
        highlight_lines,
        from_search: true,
        visual_anchor: 0,
        pending_y: false,
        count_prefix: None,
        status_message: None,
    };

    run_loop(terminal, &mut app)
}

fn apply_line_bg(line: &Line<'static>, bg: Color) -> Line<'static> {
    Line::from(
        line.spans
            .iter()
            .map(|s| Span::styled(s.content.clone(), s.style.bg(bg)))
            .collect::<Vec<_>>(),
    )
}

fn run_loop(terminal: &mut DefaultTerminal, app: &mut ViewApp) -> Result<()> {
    loop {
        // Expire status message after 3 seconds
        if let Some((_, when)) = &app.status_message
            && when.elapsed().as_secs() >= 3
        {
            app.status_message = None;
        }

        terminal.draw(|frame| {
            let area = frame.area();
            let total_lines = app.lines.len();

            let has_status_bar = app.mode == Mode::SearchInput || app.status_message.is_some();
            let layout = if has_status_bar {
                Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).split(area)
            } else {
                Layout::vertical([Constraint::Min(1)]).split(area)
            };

            let visible_height = layout[0].height.saturating_sub(2) as usize;
            app.visible_height = visible_height;
            let max_scroll = total_lines.saturating_sub(visible_height);
            app.scroll = app.scroll.min(max_scroll);
            app.cursor = app.cursor.min(total_lines.saturating_sub(1));

            // Determine visual selection range
            let visual_range = if app.mode == Mode::Visual {
                let a = app.visual_anchor.min(app.cursor);
                let b = app.visual_anchor.max(app.cursor);
                Some((a, b))
            } else {
                None
            };

            let has_search_matches = !app.matches.is_empty();
            let has_highlights = !app.highlight_lines.is_empty();
            // Only build the visible window. Cloning/restyling the whole buffer
            // every frame is O(total_lines); slicing keeps it O(visible_height)
            // regardless of session size. Rendered with no scroll offset since the
            // slice already starts at app.scroll.
            let start = app.scroll;
            let end = (start + visible_height).min(total_lines);
            let display_lines: Vec<Line> = app.lines[start..end]
                .iter()
                .enumerate()
                .map(|(li, line)| {
                    let i = start + li;
                    // Visual selection takes highest priority
                    if let Some((va, vb)) = visual_range
                        && i >= va && i <= vb
                    {
                        return apply_line_bg(line, Color::Rgb(60, 60, 100));
                    }
                    // Cursor line
                    if i == app.cursor && app.mode != Mode::SearchInput {
                        return apply_line_bg(line, Color::Rgb(30, 30, 50));
                    }
                    // Search matches
                    if has_search_matches && app.matches.binary_search(&i).is_ok() {
                        let is_current = app
                            .current_match
                            .map(|m| app.matches[m] == i)
                            .unwrap_or(false);
                        let bg = if is_current {
                            Color::Rgb(180, 140, 0)
                        } else {
                            Color::Rgb(50, 50, 70)
                        };
                        return apply_line_bg(line, bg);
                    }
                    // Chunk highlights from search jump
                    if has_highlights && app.highlight_lines.binary_search(&i).is_ok() {
                        return apply_line_bg(line, Color::Rgb(50, 50, 70));
                    }
                    line.clone()
                })
                .collect();

            let match_info = if !app.matches.is_empty() {
                let pos = app.current_match.map(|m| m + 1).unwrap_or(0);
                format!(" [{}/{}]", pos, app.matches.len())
            } else if !app.search_input.is_empty() && app.mode != Mode::SearchInput {
                " [no matches]".to_string()
            } else {
                String::new()
            };

            let exit_hint = if app.from_search {
                "Esc back"
            } else {
                "q quit"
            };

            let bottom_hint = match app.mode {
                Mode::SearchInput => " Enter confirm | Esc cancel ".to_string(),
                Mode::Visual => " V/Esc cancel | j/k select | y yank ".to_string(),
                Mode::Normal => {
                    if !app.matches.is_empty() {
                        format!(
                            " yy yank | V visual | n/N match | / search | {} {} ",
                            exit_hint, match_info
                        )
                    } else {
                        format!(" yy yank | V visual | / search | {} ", exit_hint)
                    }
                }
            };

            let block = Block::default()
                .title(app.title.as_str())
                .title_bottom(bottom_hint)
                .borders(Borders::ALL);

            // display_lines is already sliced to start at app.scroll, so no offset.
            let paragraph = Paragraph::new(display_lines).block(block);

            frame.render_widget(paragraph, layout[0]);

            // Scrollbar
            if max_scroll > 0 {
                let mut scrollbar_state =
                    ScrollbarState::new(max_scroll).position(app.scroll);
                frame.render_stateful_widget(
                    Scrollbar::new(ScrollbarOrientation::VerticalRight),
                    layout[0],
                    &mut scrollbar_state,
                );
            }

            // Bottom bar: search input or status message
            if has_status_bar {
                if app.mode == Mode::SearchInput {
                    let search_line = Line::from(vec![
                        Span::styled("/", Style::default().fg(Color::Yellow)),
                        Span::raw(app.search_input.as_str()),
                        Span::styled("â–ˆ", Style::default().fg(Color::Yellow)),
                    ]);
                    frame.render_widget(Paragraph::new(search_line), layout[1]);
                } else if let Some((ref msg, _)) = app.status_message {
                    let status_line = Line::from(Span::styled(
                        format!(" {msg}"),
                        Style::default().fg(Color::Green),
                    ));
                    frame.render_widget(Paragraph::new(status_line), layout[1]);
                }
            }
        })?;

        match event::read()? {
            Event::Mouse(mouse) => {
                match mouse.kind {
                    MouseEventKind::ScrollUp => {
                        app.move_cursor_up(3);
                    }
                    MouseEventKind::ScrollDown => {
                        app.move_cursor_down(3);
                    }
                    MouseEventKind::Down(crossterm::event::MouseButton::Left) => {
                        // Click to position cursor (row is relative to terminal, adjust for border)
                        let clicked_line = app.scroll + mouse.row.saturating_sub(1) as usize;
                        if clicked_line < app.lines.len() {
                            app.cursor = clicked_line;
                        }
                    }
                    _ => {}
                }
            }
            Event::Key(key) => {
            match app.mode {
                Mode::SearchInput => match key.code {
                    KeyCode::Enter => {
                        app.mode = Mode::Normal;
                        app.search();
                    }
                    KeyCode::Esc => {
                        app.mode = Mode::Normal;
                        app.clear_search();
                    }
                    KeyCode::Backspace => {
                        app.search_input.pop();
                    }
                    KeyCode::Char(c) => {
                        app.search_input.push(c);
                    }
                    _ => {}
                },
                Mode::Visual => match (key.code, key.modifiers) {
                    (KeyCode::Esc, _) | (KeyCode::Char('V'), _) | (KeyCode::Char('v'), _) => {
                        app.mode = Mode::Normal;
                    }
                    (KeyCode::Char('y'), _) => {
                        let a = app.visual_anchor.min(app.cursor);
                        let b = app.visual_anchor.max(app.cursor);
                        app.yank_lines(a, b + 1);
                        app.mode = Mode::Normal;
                    }
                    (KeyCode::Down, _) | (KeyCode::Char('j'), _) => {
                        app.move_cursor_down(1);
                    }
                    (KeyCode::Up, _) | (KeyCode::Char('k'), _) => {
                        app.move_cursor_up(1);
                    }
                    (KeyCode::Char('G'), _) | (KeyCode::End, _) => {
                        app.cursor = app.lines.len().saturating_sub(1);
                        app.ensure_cursor_visible();
                    }
                    (KeyCode::Char('g'), _) | (KeyCode::Home, _) => {
                        app.cursor = 0;
                        app.ensure_cursor_visible();
                    }
                    _ => {}
                },
                Mode::Normal => {
                    // Handle digit prefix for count (e.g. 3yy)
                    if let KeyCode::Char(c) = key.code
                        && c.is_ascii_digit() && !app.pending_y
                    {
                        let digit = c.to_digit(10).unwrap() as usize;
                        app.count_prefix = Some(
                            app.count_prefix.unwrap_or(0) * 10 + digit,
                        );
                        continue;
                    }

                    let count = app.count_prefix.take().unwrap_or(1);

                    if app.pending_y {
                        app.pending_y = false;
                        if key.code == KeyCode::Char('y') {
                            app.yank_current(count);
                            continue;
                        }
                        // Any other key after 'y' cancels the pending yank
                    }

                    match (key.code, key.modifiers) {
                        (KeyCode::Char('q'), _) | (KeyCode::Esc, _) => break,
                        (KeyCode::Char('c'), KeyModifiers::CONTROL) => break,
                        (KeyCode::Char('/'), _) => {
                            app.clear_search();
                            app.mode = Mode::SearchInput;
                        }
                        (KeyCode::Char('n'), KeyModifiers::NONE) => {
                            app.next_match();
                            app.cursor = app.scroll;
                        }
                        (KeyCode::Char('N'), _) => {
                            app.prev_match();
                            app.cursor = app.scroll;
                        }
                        (KeyCode::Char('y'), _) => {
                            app.pending_y = true;
                            app.count_prefix = if count > 1 { Some(count) } else { None };
                        }
                        (KeyCode::Char('Y'), _) => {
                            app.yank_current(count);
                        }
                        (KeyCode::Char('V'), _) => {
                            app.visual_anchor = app.cursor;
                            app.mode = Mode::Visual;
                        }
                        (KeyCode::Down, _) | (KeyCode::Char('j'), _) => {
                            app.move_cursor_down(count);
                        }
                        (KeyCode::Up, _) | (KeyCode::Char('k'), _) => {
                            app.move_cursor_up(count);
                        }
                        (KeyCode::PageDown, _) | (KeyCode::Char('d'), KeyModifiers::CONTROL) => {
                            app.move_cursor_down(20);
                        }
                        (KeyCode::PageUp, _) | (KeyCode::Char('u'), KeyModifiers::CONTROL) => {
                            app.move_cursor_up(20);
                        }
                        (KeyCode::Home, _) | (KeyCode::Char('g'), _) => {
                            app.cursor = 0;
                            app.scroll = 0;
                        }
                        (KeyCode::End, _) | (KeyCode::Char('G'), _) => {
                            app.cursor = app.lines.len().saturating_sub(1);
                            app.ensure_cursor_visible();
                        }
                        _ => {}
                    }
                },
            }
        }
            _ => {}
        }
    }

    Ok(())
}