Skip to main content

tess/
viewport.rs

1use std::ops::Range;
2
3use regex::Regex;
4
5use crate::filter::{CompiledFilter, FilterMatch};
6use crate::grep::GrepPredicate;
7use crate::or::OrGroups;
8use crate::line_index::LineIndex;
9use crate::render::{count_rows, render_line, Cell, RenderOpts};
10use crate::source::Source;
11
12/// Maximum number of lines to walk backwards when reconstructing SGR state
13/// for a scroll-up. Picked to comfortably cover a screen-height plus
14/// headroom; bounds cost so that scrolling in huge files stays snappy.
15const MAX_RECONSTRUCT_LINES: usize = 256;
16
17/// Reconstruct the SGR state at the start of `target_line` by walking up
18/// to MAX_RECONSTRUCT_LINES lines back and replaying byte-by-byte through
19/// the ANSI parser. Lines beyond the cap are skipped: if there's an
20/// unclosed SGR more than 256 lines above the top, the reconstruction starts
21/// from default — first visible lines may render in default colors until a
22/// reset appears (rare for normal log files).
23fn reconstruct_render_state(
24    src: &dyn Source,
25    idx: &crate::line_index::LineIndex,
26    target_line: usize,
27) -> crate::render::RenderState {
28    let start = target_line.saturating_sub(MAX_RECONSTRUCT_LINES);
29    let mut state = crate::render::RenderState::default();
30    for line_no in start..target_line {
31        let range = idx.line_range(line_no, src);
32        let raw = src.bytes(range);
33        for &b in raw.as_ref() {
34            let _ = crate::ansi::step(
35                &mut state.parse,
36                &mut state.style,
37                &mut state.hyperlink,
38                b,
39            );
40        }
41    }
42    state
43}
44
45/// Build the rendered text of a display row plus a `starts` table mapping
46/// each char index in that text back to its starting cell column. The last
47/// entry is a sentinel pointing one past the row's width, so a match's
48/// `[char_start, char_end)` translates to the cell range
49/// `starts[char_start]..starts[char_end]`.
50fn row_text_and_starts(row: &[Cell]) -> (String, Vec<usize>) {
51    let mut text = String::new();
52    let mut starts: Vec<usize> = Vec::with_capacity(row.len() + 1);
53    for (col, cell) in row.iter().enumerate() {
54        match cell {
55            Cell::Char { ch, .. } => {
56                starts.push(col);
57                text.push(*ch);
58            }
59            Cell::Empty => {
60                starts.push(col);
61                text.push(' ');
62            }
63            Cell::Continuation => {}
64        }
65    }
66    starts.push(row.len());
67    (text, starts)
68}
69
70/// Find every regex match in the rendered text of a row, translating each
71/// True when the byte slice contains only whitespace (space, tab, CR, LF)
72/// or is empty. Used by `-s` / `--squeeze-blank-lines` to detect runs of
73/// blank lines at frame-composition time.
74fn line_is_blank(bytes: &[u8]) -> bool {
75    bytes.iter().all(|&b| b == b' ' || b == b'\t' || b == b'\r' || b == b'\n')
76}
77
78/// to a cell column range. Empty matches are dropped. Trailing-padding
79/// spaces on a row would otherwise satisfy patterns like `\s+`; we trim
80/// those by clamping match ends to where actual content stops.
81fn find_row_highlights(row: &[Cell], regex: &Regex) -> Vec<Range<usize>> {
82    if row.is_empty() {
83        return Vec::new();
84    }
85    let last_content_col = row
86        .iter()
87        .enumerate()
88        .rev()
89        .find_map(|(c, cell)| match cell {
90            Cell::Char { width, .. } => Some(c + *width as usize),
91            Cell::Continuation => Some(c + 1),
92            Cell::Empty => None,
93        })
94        .unwrap_or(0);
95    if last_content_col == 0 {
96        return Vec::new();
97    }
98    let (text, starts) = row_text_and_starts(row);
99    let mut out = Vec::new();
100    for m in regex.find_iter(&text) {
101        if m.start() == m.end() {
102            continue;
103        }
104        let char_start = text[..m.start()].chars().count();
105        let char_end = text[..m.end()].chars().count();
106        if char_start >= starts.len() - 1 || char_end <= char_start {
107            continue;
108        }
109        let col_start = starts[char_start];
110        let col_end = starts[char_end].min(last_content_col);
111        if col_end > col_start {
112            out.push(col_start..col_end);
113        }
114    }
115    out
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119pub enum RowStyle {
120    Normal,
121    /// Render with a reduced-emphasis terminal attribute. Used by `--dim` to
122    /// keep filtered-out lines visible as context.
123    Dim,
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub enum SearchDirection {
128    Forward,
129    Backward,
130}
131
132/// How `--grep`, `--filter ~/!~`, `/`, `?`, and `:tag` patterns interpret
133/// case. `Smart` matches less / ripgrep / vim `smartcase`: a pattern with
134/// no uppercase characters is treated as case-insensitive; one with any
135/// uppercase character is case-sensitive.
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
137pub enum CaseMode {
138    #[default]
139    Sensitive,
140    Smart,
141    Insensitive,
142}
143
144/// Controls auto-exit on end-of-file. `Off` (default) never quits.
145/// `Second` (less `-e`) quits on the second forward-motion that lands at
146/// EOF in a row. `First` (less `-E`) quits the moment a forward motion
147/// lands at EOF.
148#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
149pub enum QuitAtEof {
150    #[default]
151    Off,
152    Second,
153    First,
154}
155
156impl CaseMode {
157    /// Compile this case policy into a regex pattern by prepending the
158    /// `(?i)` inline flag when case-insensitive matching is desired.
159    pub fn apply_to_pattern(self, pattern: &str) -> String {
160        match self {
161            CaseMode::Sensitive => pattern.to_string(),
162            CaseMode::Insensitive => format!("(?i){pattern}"),
163            CaseMode::Smart => {
164                if pattern.chars().any(|c| c.is_uppercase()) {
165                    pattern.to_string()
166                } else {
167                    format!("(?i){pattern}")
168                }
169            }
170        }
171    }
172}
173
174#[derive(Debug, Clone)]
175pub struct SearchState {
176    pub raw: String,
177    pub regex: Regex,
178    pub direction: SearchDirection,
179}
180
181#[derive(Debug, Clone)]
182pub struct Frame {
183    pub body: Vec<Vec<Cell>>,        // exactly (rows-1) entries
184    pub row_styles: Vec<RowStyle>,   // parallel to body
185    /// Per-row column ranges to render with reverse-video. Used by `/`
186    /// search to highlight just the matched phrase rather than the whole row.
187    /// Indexed parallel to `body`; each inner Vec holds column ranges in
188    /// `[start, end)` form (cell columns).
189    pub highlights: Vec<Vec<std::ops::Range<usize>>>,
190    pub status: String,
191    /// Style applied to the status row by the writer.
192    pub status_style: crate::ansi::Style,
193    /// `AnsiMode::Raw` passthrough hints — parallel to `body`. `Some(bytes)`
194    /// on a row instructs the writer to emit those original source bytes
195    /// instead of rendering the cell grid (lets escape sequences pass to
196    /// the terminal verbatim). `Some(empty)` skips emission (continuation
197    /// row of a wrapped line whose first row already wrote the bytes).
198    /// `None` means "render cells normally". Only populated when the
199    /// viewport's ansi_mode is Raw.
200    pub raw_rows: Vec<Option<Vec<u8>>>,
201    /// When `Some`, the whole body is a single terminal-graphics escape blob
202    /// (Sixel/Kitty). `write_frame` clears the body once, positions the cursor
203    /// at (0,0), and writes these bytes verbatim instead of the per-row cell
204    /// loop. `None` for all text/hex/ASCII-image frames.
205    pub image_blob: Option<Vec<u8>>,
206}
207
208/// How images are rendered to the terminal. `Ascii` is the default (colored
209/// ASCII / half-block art via `image_render`); `Kitty` / `Sixel` emit native
210/// terminal-graphics escape blobs via `image_protocol`.
211#[cfg(feature = "image")]
212#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
213pub enum ImageProtocol {
214    #[default]
215    Ascii,
216    Kitty,
217    Sixel,
218}
219
220pub struct Viewport {
221    top_line: usize,
222    top_row: usize,
223    /// Horizontal scroll offset (display columns). Only meaningful when not
224    /// wrapping (chop text / image). Clamped per-frame; reset on new file /
225    /// wrap-on. See `hscroll_active`.
226    left_col: usize,
227    cols: u16,
228    rows: u16,
229    pub opts: RenderOpts,
230    pub show_line_numbers: bool,
231    pub source_label: String,
232    follow_mode: bool,
233    live_mode: bool,
234    prettify_label: Option<String>,
235    format_label: Option<String>,
236    filter: Option<CompiledFilter>,
237    grep: Option<GrepPredicate>,
238    or_groups: OrGroups,
239    dim_mode: bool,
240    /// In hide mode (filter active, !dim), maps visible position → logical line
241    /// index. Empty otherwise.
242    visible_lines: Vec<usize>,
243    /// How many logical lines we've evaluated for filter membership. Used by
244    /// `extend_visible_lines` to avoid re-scanning lines on every tick.
245    visible_scanned: usize,
246    search: Option<SearchState>,
247    /// Active display template + format regex. When set, lines are rendered
248    /// through the template before being shown, searched, or counted for wraps.
249    /// Filtering still operates on the raw line (it uses captures, not text).
250    display: Option<crate::format::DisplayRenderer>,
251    hex_mode: bool,
252    #[cfg(feature = "image")]
253    image: Option<image::RgbaImage>,
254    #[cfg(feature = "image")]
255    animation: Option<crate::anim::AnimationState>,
256    #[cfg(feature = "image")]
257    image_protocol: ImageProtocol,
258    /// Pixel dimensions of one terminal cell (w, h), used by protocol rendering
259    /// to scale the image to fit terminal width and map text rows to pixels.
260    #[cfg(feature = "image")]
261    cell_px: (u16, u16),
262    /// Width-scaled image cache. The `u16` is the scaled WIDTH the cache was
263    /// built for; reused across vertical scrolls until the target width changes.
264    #[cfg(feature = "image")]
265    image_scaled: Option<(u16, image::RgbaImage)>,
266    image_mode: bool,
267    image_no_color: bool,
268    #[cfg_attr(not(feature = "image"), allow(dead_code))]
269    image_format: String,
270    #[cfg(feature = "image")]
271    image_style: crate::image_render::AsciiStyle,
272    #[cfg_attr(not(feature = "image"), allow(dead_code))]
273    image_width: Option<usize>,
274    /// Bytes per hex group in `--hex` mode. One of 1, 2, 4, 8, 16.
275    /// Default 2 (matches the historical `xxd` 2-byte / 4-char grouping).
276    hex_group_size: usize,
277    /// Custom status-line prompt template. When set, replaces the built-in
278    /// format_status output with the template rendered against PromptContext.
279    prompt: Option<crate::prompt::ParsedPrompt>,
280    /// Error message from a failed preprocessor run. When set, surfaces
281    /// a `[preprocess-failed: ...]` tag in the status line.
282    preprocess_failure: Option<String>,
283    /// When `count > 1`, status line shows `<label>  [current+1/count]`.
284    file_index: Option<(usize, usize)>,
285    /// When set, status line and prompt context include `[tag: <name> (N/M)]`.
286    tag_active: Option<(String, usize, usize)>,  // (name, cursor+1, total)
287    /// ANSI interpretation mode, resolved from --no-color / -r / env at startup.
288    ansi_mode: crate::render::AnsiMode,
289    /// Style applied to the status row at the writer level. Default
290    /// `reverse` for backwards-compat. Overridden by --status-style /
291    /// --prompt-style / per-format prompt_style.
292    status_style: crate::ansi::Style,
293    /// Transient status message shown for a few ticks (e.g. "(F reopened)"
294    /// after a file rotation). The `u32` is the remaining tick count; the
295    /// app loop decrements via `tick_flash` and the formatter renders the
296    /// message as long as it's non-empty.
297    status_flash: Option<(String, u32)>,
298    /// Ticks since the line index last grew. Used to render `(F idle)`
299    /// instead of `(F)` after a few seconds with no new bytes. Reset to
300    /// 0 in `note_growth`, incremented in `tick_idle`. 20 ticks ≈ 5s at
301    /// the 250 ms poll cadence.
302    ticks_since_growth: u32,
303    /// Case-sensitivity policy for search / filter / grep regex compile.
304    /// Resolved from -i / -I CLI flags at startup; mutated by the `:case`
305    /// colon command at runtime.
306    case_mode: CaseMode,
307    /// When false, search-match highlighting is suppressed in frame
308    /// composition (but search navigation still works). Toggled by
309    /// `-G` / `--no-hilite-search` and `:hlsearch` / `:nohlsearch`.
310    hilite_search: bool,
311    /// Auto-exit-on-EOF policy resolved from `-e` / `-E` at startup.
312    quit_at_eof: QuitAtEof,
313    /// Counter for `QuitAtEof::Second`: number of consecutive forward
314    /// motions that landed at EOF. Reset by any backward motion.
315    eof_hits: u8,
316    /// `-s` / `--squeeze-blank-lines`: collapse runs of blank lines to
317    /// a single blank line at display time. Real line numbers / counts
318    /// in `idx` are preserved.
319    squeeze_blanks: bool,
320    /// `--header=L,C`: pin the top `L` source lines at the top of the
321    /// viewport and the left `C` columns at the left. The cols dimension
322    /// is currently inert (no horizontal scroll yet); wired so future
323    /// horizontal-scroll support can opt into it without re-plumbing.
324    header_lines: usize,
325    header_cols: usize,
326    /// `-z` / `--window=N`: PageDown / PageUp step size in lines. `None`
327    /// (default) means "use body_rows" — full-screen page step. Half-page
328    /// commands always use body_rows/2 regardless.
329    page_size: Option<u16>,
330    /// Cached SGR/hyperlink state at the start of `render_state_for`.
331    /// Invalidated when top_line changes or source grows; reconstructed
332    /// by walking up to MAX_RECONSTRUCT_LINES lines back.
333    render_state: crate::render::RenderState,
334    /// Line number that `render_state` matches the start of. Sentinel
335    /// `usize::MAX` means "invalid, must reconstruct".
336    render_state_for: usize,
337    /// `--incsearch` / `:incsearch`: when on, each keystroke in the `/`/`?`
338    /// search prompt previews the first match (jump + highlight) from the
339    /// position the prompt opened at. Esc restores; Enter commits. Default off.
340    incsearch: bool,
341    /// `-J` / `--status-column`: when on, a 1-column gutter is drawn at the far
342    /// left (left of the line-number gutter) showing a mark letter, else `*`
343    /// on lines with a current-search match. Default off. No-op in hex/raw/image.
344    status_column: bool,
345    /// Per-frame input: current file's marks keyed by line → mark letter.
346    /// Set by the app loop before composing each frame when `status_column`
347    /// is on. Drives the mark glyph in the status column.
348    status_marks: std::collections::HashMap<usize, char>,
349}
350
351impl Viewport {
352    pub fn new(cols: u16, rows: u16, source_label: String) -> Self {
353        let opts = RenderOpts { cols, ..RenderOpts::default() };
354        Self {
355            top_line: 0,
356            top_row: 0,
357            left_col: 0,
358            cols,
359            rows,
360            opts,
361            show_line_numbers: false,
362            source_label,
363            follow_mode: false,
364            live_mode: false,
365            prettify_label: None,
366            format_label: None,
367            filter: None,
368            grep: None,
369            or_groups: OrGroups::default(),
370            dim_mode: false,
371            visible_lines: Vec::new(),
372            visible_scanned: 0,
373            search: None,
374            display: None,
375            hex_mode: false,
376            #[cfg(feature = "image")]
377            image: None,
378            #[cfg(feature = "image")]
379            animation: None,
380            #[cfg(feature = "image")]
381            image_protocol: ImageProtocol::Ascii,
382            #[cfg(feature = "image")]
383            cell_px: (8, 16),
384            #[cfg(feature = "image")]
385            image_scaled: None,
386            image_mode: false,
387            image_no_color: false,
388            image_format: String::new(),
389            #[cfg(feature = "image")]
390            image_style: crate::image_render::AsciiStyle::Ramp,
391            image_width: None,
392            hex_group_size: 2,
393            prompt: None,
394            preprocess_failure: None,
395            file_index: None,
396            tag_active: None,
397            ansi_mode: crate::render::AnsiMode::Strict,
398            status_style: crate::ansi::Style { reverse: true, ..Default::default() },
399            status_flash: None,
400            ticks_since_growth: 0,
401            case_mode: CaseMode::default(),
402            hilite_search: true,
403            quit_at_eof: QuitAtEof::default(),
404            eof_hits: 0,
405            squeeze_blanks: false,
406            header_lines: 0,
407            header_cols: 0,
408            page_size: None,
409            render_state: crate::render::RenderState::default(),
410            render_state_for: usize::MAX,
411            incsearch: false,
412            status_column: false,
413            status_marks: std::collections::HashMap::new(),
414        }
415    }
416
417    pub fn status_column(&self) -> bool { self.status_column }
418
419    pub fn set_status_column(&mut self, on: bool) { self.status_column = on; }
420
421    /// Provide the current file's marks (line → mark letter) for the next
422    /// frame. Cheap no-op cost when status_column is off (the frame never
423    /// reads it then), but the app gates the call anyway.
424    pub fn set_status_marks(&mut self, marks: std::collections::HashMap<usize, char>) {
425        self.status_marks = marks;
426    }
427
428    /// Width of the far-left status column (1 if `-J` active, else 0). Forced
429    /// to 0 in raw passthrough since `-J` is a no-op there (hex/image use
430    /// separate frame paths and never reach the text composition).
431    fn status_col_width(&self) -> u16 {
432        if self.status_column && self.ansi_mode != crate::render::AnsiMode::Raw { 1 } else { 0 }
433    }
434
435    /// A single status-column cell carrying `glyph`. Styled plainly to match
436    /// the line-number gutter label cells.
437    fn status_cell(glyph: char) -> Cell {
438        Cell::Char { ch: glyph, width: 1, style: crate::ansi::Style::default(), hyperlink: None }
439    }
440
441    /// Glyph for the status column on source line `line_n`'s first display row:
442    /// a mark letter (precedence) if the line is marked, else `*` when the line
443    /// contains a current-search match, else a blank.
444    fn status_glyph(&self, line_n: usize, has_match: bool) -> char {
445        if let Some(&ch) = self.status_marks.get(&line_n) {
446            ch
447        } else if has_match {
448            '*'
449        } else {
450            ' '
451        }
452    }
453
454    pub fn case_mode(&self) -> CaseMode { self.case_mode }
455
456    pub fn hilite_search(&self) -> bool { self.hilite_search }
457
458    pub fn set_hilite_search(&mut self, on: bool) { self.hilite_search = on; }
459
460    pub fn incsearch(&self) -> bool { self.incsearch }
461
462    pub fn set_incsearch(&mut self, on: bool) { self.incsearch = on; }
463
464    pub fn top_row(&self) -> usize { self.top_row }
465
466    /// Set the scroll position directly (logical line + wrap-row within it).
467    pub fn set_top(&mut self, line: usize, row: usize) {
468        self.top_line = line;
469        self.top_row = row;
470    }
471
472    /// Preview an incremental-search pattern from `origin` (a (top_line, top_row)
473    /// captured when the prompt opened), scrolling to the first match. Empty or
474    /// invalid patterns are a silent no-op. Used by `--incsearch`.
475    pub fn incsearch_preview(&mut self, src: &dyn Source, idx: &mut LineIndex,
476                             pattern: &str, direction: SearchDirection,
477                             origin: (usize, usize)) {
478        if pattern.is_empty() { return; }
479        self.set_top(origin.0, origin.1);
480        if self.set_search(pattern.to_string(), direction).is_ok() {
481            self.search_repeat(src, idx, false);
482        }
483    }
484
485    pub fn set_quit_at_eof(&mut self, mode: QuitAtEof) {
486        self.quit_at_eof = mode;
487        self.eof_hits = 0;
488    }
489
490    pub fn set_squeeze_blanks(&mut self, on: bool) { self.squeeze_blanks = on; }
491    pub fn squeeze_blanks(&self) -> bool { self.squeeze_blanks }
492
493    pub fn set_header(&mut self, lines: usize, cols: usize) {
494        self.header_lines = lines;
495        self.header_cols = cols;
496        // Don't let top_line land inside the pinned region — the scrolling
497        // window starts at line `header_lines` once the feature is on.
498        if self.top_line < self.header_lines {
499            self.top_line = self.header_lines;
500        }
501    }
502    pub fn header_lines(&self) -> usize { self.header_lines }
503    pub fn header_cols(&self) -> usize { self.header_cols }
504
505    pub fn set_page_size(&mut self, n: Option<u16>) { self.page_size = n; }
506    pub fn page_size(&self) -> Option<u16> { self.page_size }
507
508    /// Notify the EOF state machine of a motion. Returns `true` when the
509    /// caller should quit. `forward = true` for any motion that could
510    /// advance past EOF; `false` for backward motions (which reset the
511    /// hit counter under `QuitAtEof::Second`).
512    pub fn note_motion_for_eof(&mut self, forward: bool, src: &dyn Source, idx: &LineIndex) -> bool {
513        match self.quit_at_eof {
514            QuitAtEof::Off => false,
515            QuitAtEof::First if forward && self.is_at_bottom(src, idx) => true,
516            QuitAtEof::Second if forward && self.is_at_bottom(src, idx) => {
517                self.eof_hits = self.eof_hits.saturating_add(1);
518                self.eof_hits >= 2
519            }
520            _ => {
521                if !forward { self.eof_hits = 0; }
522                false
523            }
524        }
525    }
526
527    /// Switch the case-mode policy. Re-compiles any active search so the
528    /// new policy takes effect on the next frame without the user having
529    /// to retype the pattern.
530    pub fn set_case_mode(&mut self, mode: CaseMode) {
531        self.case_mode = mode;
532        if let Some(s) = self.search.clone() {
533            let _ = self.set_search(s.raw, s.direction);
534        }
535    }
536
537    pub fn set_status_style(&mut self, style: crate::ansi::Style) {
538        self.status_style = style;
539    }
540
541    pub fn status_style(&self) -> crate::ansi::Style {
542        self.status_style
543    }
544
545    /// Show `msg` in the status row for the next `ticks` calls to the
546    /// timeout branch (~250 ms each). Overrides the normal status during
547    /// that window.
548    pub fn flash(&mut self, msg: impl Into<String>, ticks: u32) {
549        self.status_flash = Some((msg.into(), ticks));
550    }
551
552    /// Decrement the flash countdown by one tick. Clears the flash when
553    /// it reaches zero.
554    pub fn tick_flash(&mut self) {
555        if let Some((_, n)) = &mut self.status_flash {
556            *n = n.saturating_sub(1);
557            if *n == 0 {
558                self.status_flash = None;
559            }
560        }
561    }
562
563    /// Reset the idle counter; the source just produced fresh bytes.
564    pub fn note_growth(&mut self) {
565        self.ticks_since_growth = 0;
566    }
567
568    /// Increment the idle counter. Called in the timeout branch when the
569    /// line index didn't grow.
570    pub fn tick_idle(&mut self) {
571        self.ticks_since_growth = self.ticks_since_growth.saturating_add(1);
572    }
573
574    /// True when the source has been quiet long enough to surface
575    /// `(F idle)` instead of `(F)`. Threshold: 20 ticks ≈ 5s.
576    pub fn is_idle(&self) -> bool {
577        self.ticks_since_growth >= 20
578    }
579
580    pub fn set_display(&mut self, renderer: Option<crate::format::DisplayRenderer>) {
581        self.display = renderer;
582    }
583
584    pub fn set_hex_mode(&mut self, on: bool) {
585        self.hex_mode = on;
586    }
587
588    /// Returns whether `--hex` rendering is active.
589    pub fn hex_mode(&self) -> bool {
590        self.hex_mode
591    }
592
593    #[cfg(feature = "image")]
594    fn reset_image_view(&mut self, format: &str, style: crate::image_render::AsciiStyle, width: Option<usize>) {
595        self.image_format = format.to_string();
596        self.image_style = style;
597        self.image_width = width;
598        self.image_mode = true;
599        self.top_line = 0;
600        self.top_row = 0;
601        self.image_scaled = None;
602    }
603
604    #[cfg(feature = "image")]
605    pub fn set_image(&mut self, img: image::RgbaImage, format: &str, style: crate::image_render::AsciiStyle, width: Option<usize>) {
606        self.reset_image_view(format, style, width);
607        self.image = Some(img);
608        self.animation = None;
609    }
610
611    #[cfg(feature = "image")]
612    pub fn set_animation(&mut self, anim: crate::image_render::Animation, format: &str,
613                         style: crate::image_render::AsciiStyle, width: Option<usize>) {
614        self.reset_image_view(format, style, width);
615        self.image = None;
616        self.animation = Some(crate::anim::AnimationState::new(anim.frames, anim.loop_count));
617    }
618
619    #[cfg(feature = "image")]
620    pub fn has_animation(&self) -> bool { self.animation.is_some() }
621
622    #[cfg(feature = "image")]
623    fn current_image(&self) -> Option<&image::RgbaImage> {
624        match &self.animation {
625            Some(a) => Some(a.current_frame()),
626            None => self.image.as_ref(),
627        }
628    }
629
630    #[cfg(feature = "image")]
631    pub fn tick(&mut self, dt: std::time::Duration) -> bool {
632        if let Some(a) = &mut self.animation {
633            if a.advance(dt) { self.image_scaled = None; return true; }
634        }
635        false
636    }
637
638    #[cfg(feature = "image")]
639    pub fn anim_deadline(&self) -> Option<std::time::Duration> {
640        self.animation.as_ref().and_then(|a| a.next_deadline())
641    }
642
643    #[cfg(feature = "image")]
644    pub fn anim_toggle_pause(&mut self) {
645        if let Some(a) = &mut self.animation { a.toggle_pause(); self.image_scaled = None; }
646    }
647
648    #[cfg(feature = "image")]
649    pub fn anim_step(&mut self, delta: i32) {
650        if let Some(a) = &mut self.animation { a.step(delta); self.image_scaled = None; }
651    }
652
653    #[cfg(feature = "image")]
654    pub fn anim_restart(&mut self) {
655        if let Some(a) = &mut self.animation { a.restart(); self.image_scaled = None; }
656    }
657
658    #[cfg(feature = "image")]
659    fn anim_badge(&self) -> String {
660        match &self.animation {
661            Some(a) => {
662                let (i, n) = (a.frame_index() + 1, a.frame_count());
663                if a.is_finished() { format!("  [done {n}/{n}]") }
664                else if a.is_playing() { format!("  [play {i}/{n}]") }
665                else { format!("  [pause {i}/{n}]") }
666            }
667            None => String::new(),
668        }
669    }
670
671    pub fn set_image_no_color(&mut self, on: bool) { self.image_no_color = on; }
672
673    #[cfg(feature = "image")]
674    pub fn set_image_protocol(&mut self, proto: ImageProtocol, cell_px: Option<(u16, u16)>) {
675        self.image_protocol = proto;
676        if let Some(c) = cell_px {
677            if c.0 > 0 && c.1 > 0 { self.cell_px = c; }
678        }
679        self.image_scaled = None;
680    }
681
682    #[cfg(feature = "image")]
683    pub fn image_protocol(&self) -> ImageProtocol { self.image_protocol }
684
685    pub fn image_mode(&self) -> bool { self.image_mode }
686
687    #[cfg(feature = "image")]
688    fn image_cols(&self) -> u16 {
689        self.image_width.map(|w| w.clamp(1, u16::MAX as usize) as u16).unwrap_or(self.cols.max(1))
690    }
691
692    #[cfg(feature = "image")]
693    pub fn image_total_rows(&self) -> usize {
694        match self.current_image() {
695            Some(img) => {
696                let (w, h) = img.dimensions();
697                if self.image_protocol != ImageProtocol::Ascii {
698                    protocol_occupied_rows(w, h, self.cols, self.cell_px, self.image_width)
699                } else {
700                    crate::image_render::output_rows(w, h, self.image_cols(), self.image_style)
701                }
702            }
703            None => 0,
704        }
705    }
706
707    #[cfg(feature = "image")]
708    pub fn is_at_bottom_image(&self) -> bool {
709        let body = self.body_rows() as usize;
710        self.top_line + body >= self.image_total_rows()
711    }
712
713    /// Set bytes-per-group for `--hex` rendering. Accepts 1, 2, 4, 8, or 16.
714    /// Invalid values are ignored.
715    pub fn set_hex_group_size(&mut self, bytes_per_group: usize) {
716        if matches!(bytes_per_group, 1 | 2 | 4 | 8 | 16) {
717            self.hex_group_size = bytes_per_group;
718        }
719    }
720
721    /// Current bytes-per-group for `--hex` rendering.
722    pub fn hex_group_size(&self) -> usize {
723        self.hex_group_size
724    }
725
726    pub fn set_prompt(&mut self, prompt: Option<crate::prompt::ParsedPrompt>) {
727        self.prompt = prompt;
728    }
729
730    pub fn set_preprocess_failure(&mut self, msg: Option<String>) {
731        self.preprocess_failure = msg;
732    }
733
734    pub fn set_file_index(&mut self, current: usize, total: usize) {
735        self.file_index = if total > 1 {
736            Some((current, total))
737        } else {
738            None
739        };
740    }
741
742    pub fn set_tag_active(&mut self, info: Option<(String, usize, usize)>) {
743        self.tag_active = info;
744    }
745
746    pub fn set_ansi_mode(&mut self, mode: crate::render::AnsiMode) {
747        self.ansi_mode = mode;
748    }
749
750    pub fn ansi_mode(&self) -> crate::render::AnsiMode {
751        self.ansi_mode
752    }
753
754    pub fn set_source_label(&mut self, label: String) {
755        self.source_label = label;
756    }
757
758    pub fn source_label_clone(&self) -> String {
759        self.source_label.clone()
760    }
761
762    /// Fetch a logical line's display bytes — rendered through the active
763    /// display template if one is set and the line parses against the format
764    /// regex, otherwise the raw bytes. Used everywhere the *visible* form of
765    /// the line matters: rendering, search, wrap-row counting.
766    fn line_display_bytes<'a>(&self, src: &'a dyn Source, idx: &LineIndex, line_n: usize) -> std::borrow::Cow<'a, [u8]> {
767        let range = idx.line_range(line_n, src);
768        let raw = src.bytes(range);
769        if let Some(r) = self.display.as_ref() {
770            if let Some(rendered) = r.render_line(&raw) {
771                return std::borrow::Cow::Owned(rendered.into_bytes());
772            }
773        }
774        raw
775    }
776
777    /// Compile and store a search pattern. Returns the parse error from the
778    /// regex crate if the pattern is invalid; the previous search (if any)
779    /// is preserved on error.
780    pub fn set_search(&mut self, raw: String, direction: SearchDirection) -> Result<(), String> {
781        let compiled = self.case_mode.apply_to_pattern(&raw);
782        let regex = Regex::new(&compiled).map_err(|e| e.to_string())?;
783        self.search = Some(SearchState { raw, regex, direction });
784        Ok(())
785    }
786
787    pub fn clear_search(&mut self) { self.search = None; }
788
789    pub fn search_active(&self) -> bool { self.search.is_some() }
790
791    pub fn search_direction(&self) -> SearchDirection {
792        self.search.as_ref().map(|s| s.direction).unwrap_or(SearchDirection::Forward)
793    }
794
795    /// Jump to the next match of the active search, in `direction` (or its
796    /// reverse if `reverse` is true). Wraps at the end of the source.
797    /// Returns true iff a match was found and the viewport moved.
798    pub fn search_repeat(&mut self, src: &dyn Source, idx: &mut LineIndex, reverse: bool) -> bool {
799        if idx.records_mode() {
800            self.search_repeat_records(src, idx, reverse)
801        } else {
802            self.search_repeat_lines(src, idx, reverse)
803        }
804    }
805
806    /// Line-mode search: unchanged original logic.
807    fn search_repeat_lines(&mut self, src: &dyn Source, idx: &mut LineIndex, reverse: bool) -> bool {
808        let Some(s) = self.search.as_ref() else { return false; };
809        let forward = matches!(
810            (s.direction, reverse),
811            (SearchDirection::Forward, false) | (SearchDirection::Backward, true)
812        );
813        idx.extend_to_end(src);
814        let pattern = s.regex.clone();
815        if self.hide_mode() {
816            self.extend_visible_lines(idx, src);
817            self.search_step_in_visible(&pattern, src, idx, forward)
818        } else {
819            self.search_step_in_logical(&pattern, src, idx, forward)
820        }
821    }
822
823    /// Records-mode search: iterate records, match against UTF-8-lossy decoded
824    /// record bytes (which may contain embedded `\n`s), and jump the viewport
825    /// to the first line of the matching record.
826    fn search_repeat_records(&mut self, src: &dyn Source, idx: &mut LineIndex, reverse: bool) -> bool {
827        let Some(s) = self.search.as_ref() else { return false; };
828        let forward = matches!(
829            (s.direction, reverse),
830            (SearchDirection::Forward, false) | (SearchDirection::Backward, true)
831        );
832        let pattern = s.regex.clone();
833        idx.extend_to_end(src);
834
835        let total = idx.record_count();
836        if total == 0 { return false; }
837
838        let cur_record = idx.line_to_record(self.top_line);
839
840        let range: Box<dyn Iterator<Item = usize>> = if forward {
841            Box::new(((cur_record + 1)..total).chain(0..=cur_record))
842        } else {
843            let earlier: Vec<usize> = (0..cur_record).rev().collect();
844            let later: Vec<usize> = (cur_record..total).rev().collect();
845            Box::new(earlier.into_iter().chain(later))
846        };
847
848        for r in range {
849            let bytes = idx.record_bytes_stripped(r, src);
850            let text = String::from_utf8_lossy(&bytes);
851            if pattern.is_match(&text) {
852                let line_range = idx.record_line_range(r);
853                self.top_line = line_range.start;
854                self.top_row = 0;
855                return true;
856            }
857        }
858        false
859    }
860
861    fn line_matches(&self, pattern: &Regex, src: &dyn Source, idx: &LineIndex, line_n: usize) -> bool {
862        // Search runs against the *displayed* bytes so what the user sees is
863        // what they can find. With a template active, that's the rendered form;
864        // otherwise the raw line. ANSI color sequences are stripped so that
865        // `/error` finds a red `error` regardless of escape codes.
866        let display = self.line_display_bytes(src, idx, line_n);
867        let bytes = crate::ansi::strip_sgr(&display);
868        match std::str::from_utf8(&bytes) {
869            Ok(s) => pattern.is_match(s),
870            Err(_) => false,
871        }
872    }
873
874    fn search_step_in_logical(&mut self, pattern: &Regex, src: &dyn Source, idx: &LineIndex, forward: bool) -> bool {
875        let total = idx.line_count();
876        if total == 0 { return false; }
877        let start = self.top_line;
878        // Walk every logical line once, starting from start+1 (or start-1)
879        // and wrapping at the end / beginning.
880        for offset in 1..=total {
881            let line_n = if forward {
882                (start + offset) % total
883            } else {
884                (start + total - offset) % total
885            };
886            if self.line_matches(pattern, src, idx, line_n) {
887                self.top_line = line_n;
888                self.top_row = 0;
889                return true;
890            }
891        }
892        false
893    }
894
895    fn search_step_in_visible(&mut self, pattern: &Regex, src: &dyn Source, idx: &LineIndex, forward: bool) -> bool {
896        let total = self.visible_lines.len();
897        if total == 0 { return false; }
898        // Find current visible position for top_line.
899        let cur = self.visible_lines.iter().position(|&l| l >= self.top_line).unwrap_or(0);
900        for offset in 1..=total {
901            let visible_idx = if forward {
902                (cur + offset) % total
903            } else {
904                (cur + total - offset) % total
905            };
906            let line_n = self.visible_lines[visible_idx];
907            if self.line_matches(pattern, src, idx, line_n) {
908                self.top_line = line_n;
909                self.top_row = 0;
910                return true;
911            }
912        }
913        false
914    }
915
916    pub fn set_filter(&mut self, filter: Option<CompiledFilter>) {
917        self.filter = filter;
918        self.visible_lines.clear();
919        self.visible_scanned = 0;
920        // Drop scroll state — line numbering may have changed under us.
921        self.top_line = 0;
922        self.top_row = 0;
923        self.left_col = 0;
924    }
925
926    pub fn set_grep(&mut self, grep: Option<GrepPredicate>) {
927        self.grep = grep;
928        self.visible_lines.clear();
929        self.visible_scanned = 0;
930        self.top_line = 0;
931        self.top_row = 0;
932        self.left_col = 0;
933    }
934
935    pub fn set_or_groups(&mut self, or_groups: OrGroups) {
936        self.or_groups = or_groups;
937        self.visible_lines.clear();
938        self.visible_scanned = 0;
939        self.top_line = 0;
940        self.top_row = 0;
941        self.left_col = 0;
942    }
943
944    pub fn or_active(&self) -> bool {
945        self.or_groups.is_active()
946    }
947
948    pub fn grep_active(&self) -> bool { self.grep.is_some() }
949
950    pub fn set_dim_mode(&mut self, on: bool) {
951        self.dim_mode = on;
952        // Hide mode is the only mode that needs visible_lines; clear when
953        // turning dim ON, and re-derive from scratch when turning dim OFF
954        // (next extend_visible_lines call rebuilds it).
955        self.visible_lines.clear();
956        self.visible_scanned = 0;
957    }
958
959    pub fn filter_active(&self) -> bool { self.filter.is_some() }
960
961    pub fn dim_mode(&self) -> bool { self.dim_mode }
962
963    fn hide_mode(&self) -> bool {
964        (self.filter.is_some() || self.grep.is_some() || self.or_groups.is_active())
965            && !self.dim_mode
966    }
967
968    /// Walk any newly indexed logical lines and append matching ones to
969    /// `visible_lines` if we're in hide mode. No-op otherwise. Cheap to call
970    /// every loop tick — keeps a `visible_scanned` cursor (line mode only;
971    /// records mode rebuilds from scratch each call).
972    pub fn extend_visible_lines(&mut self, idx: &LineIndex, src: &dyn Source) {
973        if !self.hide_mode() {
974            return;
975        }
976        if idx.records_mode() {
977            self.extend_visible_lines_records(idx, src);
978        } else {
979            self.extend_visible_lines_per_line(idx, src);
980        }
981    }
982
983    /// Line-mode: incrementally append newly indexed matching lines.
984    fn extend_visible_lines_per_line(&mut self, idx: &LineIndex, src: &dyn Source) {
985        let total = idx.line_count();
986        while self.visible_scanned < total {
987            let line_n = self.visible_scanned;
988            let bytes = idx.line_bytes_stripped(line_n, src);
989            if self.line_passes(&bytes) {
990                self.visible_lines.push(line_n);
991            }
992            self.visible_scanned += 1;
993        }
994    }
995
996    /// Records-mode: evaluate predicates once per record on the full record
997    /// bytes (which include embedded `\n`s). All physical lines of a matching
998    /// record are pushed to `visible_lines`; non-matching records are dropped
999    /// entirely (hide mode). Rebuilds from scratch on each call — O(records)
1000    /// per frame but acceptable for current workloads; avoids the complexity
1001    /// of tracking a records-scanned cursor alongside `visible_scanned`.
1002    fn extend_visible_lines_records(&mut self, idx: &LineIndex, src: &dyn Source) {
1003        self.visible_lines.clear();
1004        self.visible_scanned = 0; // not used by records path; reset for clarity
1005        let total_records = idx.record_count();
1006        for r in 0..total_records {
1007            if self.record_passes(idx, src, r) {
1008                for line_n in idx.record_line_range(r) {
1009                    self.visible_lines.push(line_n);
1010                }
1011            }
1012        }
1013    }
1014
1015    /// Combined predicate: bytes pass iff the (optional) filter matches AND
1016    /// the (optional) grep matches. Missing predicates vacuously pass.
1017    /// `bytes` is always a single logical line — records-mode callers go
1018    /// through `record_passes` instead because the two predicates have
1019    /// different granularity (filter = header line, grep = whole record).
1020    fn line_passes(&self, line: &[u8]) -> bool {
1021        let filter_ok = match self.filter.as_ref() {
1022            Some(f) => matches!(f.evaluate(line), FilterMatch::Matched),
1023            None => true,
1024        };
1025        let grep_ok = match self.grep.as_ref() {
1026            Some(g) => g.matches(line),
1027            None => true,
1028        };
1029        filter_ok && grep_ok && self.or_groups.matches_line(line)
1030    }
1031
1032    /// Records-mode predicate. Both filter and grep are evaluated against
1033    /// the full multi-line record bytes. Filter uses the format regex with
1034    /// dotall + multi-line semantics so greedy captures like
1035    /// `(?P<message>.*)$` span the whole record body — `--filter
1036    /// message~foo` matches when `foo` appears anywhere in the record, not
1037    /// only on the header. Grep matches anywhere in the record bytes too,
1038    /// so `(?s)foo.*bar` keeps working across continuation lines.
1039    fn record_passes(&self, idx: &LineIndex, src: &dyn Source, r: usize) -> bool {
1040        let need = self.filter.is_some() || self.grep.is_some() || self.or_groups.is_active();
1041        let bytes = if need {
1042            Some(idx.record_bytes_stripped(r, src))
1043        } else {
1044            None
1045        };
1046        let filter_ok = match self.filter.as_ref() {
1047            Some(f) => matches!(
1048                f.evaluate_record(bytes.as_deref().unwrap()),
1049                FilterMatch::Matched,
1050            ),
1051            None => true,
1052        };
1053        let grep_ok = match self.grep.as_ref() {
1054            Some(g) => g.matches(bytes.as_deref().unwrap()),
1055            None => true,
1056        };
1057        let or_ok = if self.or_groups.is_active() {
1058            self.or_groups.matches_record(bytes.as_deref().unwrap())
1059        } else {
1060            true
1061        };
1062        filter_ok && grep_ok && or_ok
1063    }
1064
1065    /// Return true iff line `line_n` should be rendered dim. In records mode,
1066    /// the match decision is made once per record and applied to all its
1067    /// physical lines. In line mode, the decision is made per line.
1068    fn should_dim_line(&self, line_n: usize, idx: &LineIndex, src: &dyn Source) -> bool {
1069        if !self.dim_mode {
1070            return false;
1071        }
1072        if idx.records_mode() {
1073            let r = idx.line_to_record(line_n);
1074            !self.record_passes(idx, src, r)
1075        } else {
1076            let bytes = idx.line_bytes_stripped(line_n, src);
1077            !self.line_passes(&bytes)
1078        }
1079    }
1080
1081    /// Logical line index of the *last* row drawn in the body, given the
1082    /// current `top_line` and `body_rows`. In line mode this is just
1083    /// `top_line + body_rows - 1` clamped to the indexed line count. In hide
1084    /// mode it's the logical line that sits at the bottom of the visible
1085    /// slice — i.e. `visible_lines[cur + body_rows - 1]`. Always returns a
1086    /// value `>= self.top_line`, so callers passing it to `line_to_record`
1087    /// never get a "bottom record < top record" inversion.
1088    fn bottom_visible_line(&self, idx: &LineIndex) -> usize {
1089        let body_rows = self.body_rows() as usize;
1090        if self.hide_mode() && !self.visible_lines.is_empty() {
1091            let cur = self
1092                .visible_lines
1093                .iter()
1094                .position(|&l| l >= self.top_line)
1095                .unwrap_or(self.visible_lines.len().saturating_sub(1));
1096            let last_pos = (cur + body_rows.saturating_sub(1)).min(self.visible_lines.len() - 1);
1097            return self.visible_lines[last_pos];
1098        }
1099        let total = idx.line_count();
1100        if total == 0 {
1101            return self.top_line;
1102        }
1103        (self.top_line + body_rows.saturating_sub(1)).min(total - 1)
1104    }
1105
1106    pub fn body_rows(&self) -> u16 { self.rows.saturating_sub(1).max(1) }
1107
1108    pub fn follow_mode(&self) -> bool { self.follow_mode }
1109
1110    /// Conditionally turn follow mode off. Used by motion handlers when
1111    /// `--follow-suspend-on-motion` is in effect — any motion (scroll,
1112    /// page, goto-line) suspends following until the user re-engages
1113    /// with Shift-F.
1114    pub fn suspend_follow_if(&mut self, flag: bool) {
1115        if flag {
1116            self.follow_mode = false;
1117        }
1118    }
1119
1120    pub fn set_follow_mode(&mut self, on: bool) { self.follow_mode = on; }
1121
1122    pub fn toggle_follow(&mut self) { self.follow_mode = !self.follow_mode; }
1123
1124    pub fn live_mode(&self) -> bool { self.live_mode }
1125
1126    pub fn set_live_mode(&mut self, on: bool) { self.live_mode = on; }
1127
1128    /// Status-line label for active pretty-print state, e.g. `"json"` or
1129    /// `"json:err"`. `None` means no indicator is shown.
1130    pub fn set_prettify_label(&mut self, label: Option<String>) {
1131        self.prettify_label = label;
1132    }
1133
1134    /// Active --format name shown in <format-tag>. Set from main when a named
1135    /// format is resolved; independent of whether --filter is also active.
1136    pub fn set_format_label(&mut self, label: Option<String>) {
1137        self.format_label = label;
1138    }
1139
1140    /// Drop the per-line filter-membership cache without disturbing the filter
1141    /// itself or scroll position. Used after a `--live` rebuild: line numbering
1142    /// may have changed, so cached `visible_lines` is stale, but we want to
1143    /// keep the same filter applied and let the user stay where they were.
1144    pub fn invalidate_filter_cache(&mut self) {
1145        self.visible_lines.clear();
1146        self.visible_scanned = 0;
1147    }
1148
1149    /// Clamp `top_line` so it doesn't fall past the new end of the source.
1150    /// Pairs with `invalidate_filter_cache` after a content rewrite.
1151    pub fn clamp_top_line(&mut self, line_count: usize) {
1152        if line_count == 0 {
1153            self.top_line = 0;
1154            self.top_row = 0;
1155        } else if self.top_line >= line_count {
1156            self.top_line = line_count - 1;
1157            self.top_row = 0;
1158        }
1159    }
1160
1161    /// True when the viewport's body window already covers the last line of
1162    /// the source. New content added past this point should auto-scroll if
1163    /// follow mode is on.
1164    pub fn is_at_bottom(&self, src: &dyn Source, idx: &LineIndex) -> bool {
1165        #[cfg(feature = "image")]
1166        if self.image_mode {
1167            return self.is_at_bottom_image();
1168        }
1169        if self.hide_mode() {
1170            // Wrap-aware: at the bottom once (top_line, top_row) is at/after
1171            // the visible-line anchor that puts the last match's tail on the
1172            // last body row.
1173            (self.top_line, self.top_row) >= self.hide_bottom_anchor(src, idx)
1174        } else {
1175            // Compare in display-row units against the wrap-aware bottom
1176            // anchor — `top_line + body >= line_count` would read true while
1177            // a wrapped tail is still off-screen below.
1178            (self.top_line, self.top_row) >= self.bottom_anchor(src, idx)
1179        }
1180    }
1181
1182    /// Width of the line-number gutter (digits + 1 space separator), 0 if disabled.
1183    fn gutter_width(&self, idx: &LineIndex) -> u16 {
1184        if !self.show_line_numbers { return 0; }
1185        let n = idx.line_count().max(1);
1186        let digits = (n as f64).log10().floor() as u16 + 1;
1187        digits + 1
1188    }
1189
1190    fn render_opts(&self, gutter: u16) -> RenderOpts {
1191        let mut o = self.opts.clone();
1192        // The status column (`-J`) reserves a fixed far-left cell outside the
1193        // scrolled content, so its width comes off the content budget too.
1194        o.cols = self.cols.saturating_sub(self.status_col_width() + gutter);
1195        o.mode = self.ansi_mode;
1196        o.left_col = self.left_col;   // horizontal scroll offset carried into the kernel
1197        o
1198    }
1199
1200    pub fn frame(&mut self, src: &dyn Source, idx: &mut LineIndex) -> Frame {
1201        #[cfg(feature = "image")]
1202        if self.image_mode {
1203            return self.frame_image();
1204        }
1205        if self.hex_mode {
1206            return self.frame_hex(src);
1207        }
1208        let body_rows = self.body_rows() as usize;
1209        idx.extend_to_line(self.top_line + body_rows + 1, src);
1210
1211        // Clamp horizontal scroll to the widest line currently visible, so we
1212        // never scroll into empty space. (Chop/text path only.)
1213        if self.left_col > 0 && self.hscroll_active() {
1214            let gutter_for_clamp = self.status_col_width() + self.gutter_width(idx);
1215            let avail = self.cols.saturating_sub(gutter_for_clamp) as usize;
1216            // Build opts with left_col=0 — display_width measures full line width
1217            // regardless of the current scroll offset.
1218            let mut width_opts = self.opts.clone();
1219            width_opts.cols = self.cols.saturating_sub(gutter_for_clamp);
1220            width_opts.mode = self.ansi_mode;
1221            width_opts.left_col = 0;
1222            let mut widest = 0usize;
1223            let total_lines_for_clamp = idx.line_count();
1224            if self.hide_mode() {
1225                let hide_pos = self.visible_lines.iter()
1226                    .position(|&l| l >= self.top_line)
1227                    .unwrap_or(self.visible_lines.len());
1228                let end_vi = (hide_pos + body_rows).min(self.visible_lines.len());
1229                for vi in hide_pos..end_vi {
1230                    let ln = self.visible_lines[vi];
1231                    let bytes = self.line_display_bytes(src, idx, ln);
1232                    widest = widest.max(crate::render::display_width(&bytes, &width_opts));
1233                }
1234            } else {
1235                let start = self.top_line.max(self.header_lines);
1236                let end = (start + body_rows).min(total_lines_for_clamp);
1237                for ln in start..end {
1238                    let bytes = self.line_display_bytes(src, idx, ln);
1239                    widest = widest.max(crate::render::display_width(&bytes, &width_opts));
1240                }
1241            }
1242            self.left_col = self.left_col.min(widest.saturating_sub(avail));
1243        }
1244
1245        let gutter = self.gutter_width(idx);
1246        let scol = self.status_col_width();
1247        let r_opts = self.render_opts(gutter);
1248
1249        // Reconstruct per-line SGR state for the start of the visible window so
1250        // that unclosed SGR sequences on lines above top_line carry through.
1251        // Only meaningful in Interpret mode; harmless (and cheap) to skip otherwise.
1252        let mut render_state = if self.ansi_mode == crate::render::AnsiMode::Interpret {
1253            reconstruct_render_state(src, idx, self.top_line)
1254        } else {
1255            crate::render::RenderState::default()
1256        };
1257        // Store in the struct field for future cache use; mark current top_line.
1258        self.render_state = render_state.clone();
1259        self.render_state_for = self.top_line;
1260
1261        let mut body: Vec<Vec<Cell>> = Vec::with_capacity(body_rows);
1262        let mut row_styles: Vec<RowStyle> = Vec::with_capacity(body_rows);
1263        let mut highlights: Vec<Vec<std::ops::Range<usize>>> = Vec::with_capacity(body_rows);
1264        let mut raw_rows: Vec<Option<Vec<u8>>> = Vec::with_capacity(body_rows);
1265        let raw_passthrough = self.ansi_mode == crate::render::AnsiMode::Raw;
1266        // In hide mode we walk visible_lines; otherwise we walk logical lines.
1267        let hide = self.hide_mode();
1268        let total_lines = idx.line_count();
1269
1270        // `--header=L`: pin the first L source lines as the top L rows of
1271        // the body. Renders only the first cell-row of each pinned line
1272        // (matches less semantics; long pinned lines truncate). Skipped in
1273        // hide mode where "first L lines" might not be visible. Skipped in
1274        // raw passthrough since the user's intent there is byte-faithful
1275        // emission, not pinned headers.
1276        let header_rows = if !hide && !raw_passthrough {
1277            self.header_lines.min(body_rows).min(total_lines)
1278        } else {
1279            0
1280        };
1281        if header_rows > 0 {
1282            for hl in 0..header_rows {
1283                let raw = src.bytes(idx.line_range(hl, src));
1284                let display_bytes = if let Some(r) = self.display.as_ref() {
1285                    match r.render_line(&raw) {
1286                        Some(s) => std::borrow::Cow::Owned(s.into_bytes()),
1287                        None => raw.clone(),
1288                    }
1289                } else {
1290                    raw.clone()
1291                };
1292                let rows = render_line(&display_bytes, &r_opts, None);
1293                let mut content_row = rows.into_iter().next().unwrap_or_else(|| {
1294                    let mut v = Vec::with_capacity(self.cols as usize);
1295                    while v.len() < self.cols as usize { v.push(Cell::Empty); }
1296                    v
1297                });
1298                let mut full: Vec<Cell> = Vec::with_capacity(self.cols as usize);
1299                if scol > 0 {
1300                    let matched = self.search.as_ref()
1301                        .is_some_and(|s| !find_row_highlights(&content_row, &s.regex).is_empty());
1302                    let glyph = self.status_glyph(hl, matched);
1303                    full.push(Self::status_cell(glyph));
1304                }
1305                if gutter > 0 {
1306                    let label = format!("{:>width$} ", hl + 1, width = (gutter as usize - 1));
1307                    for c in label.chars() {
1308                        full.push(Cell::Char {
1309                            ch: c,
1310                            width: 1,
1311                            style: crate::ansi::Style::default(),
1312                            hyperlink: None,
1313                        });
1314                    }
1315                }
1316                full.append(&mut content_row);
1317                body.push(full);
1318                row_styles.push(RowStyle::Normal);
1319                highlights.push(Vec::new());
1320                raw_rows.push(None);
1321            }
1322        }
1323
1324        // For hide mode, find where the viewport starts in visible_lines.
1325        let mut hide_pos = if hide {
1326            self.visible_lines
1327                .iter()
1328                .position(|&l| l >= self.top_line)
1329                .unwrap_or(self.visible_lines.len())
1330        } else {
1331            0
1332        };
1333        let mut line_n = if hide {
1334            self.visible_lines.get(hide_pos).copied().unwrap_or(total_lines)
1335        } else {
1336            // When header pinning is on, skip past the pinned region so the
1337            // scrolling window doesn't show those lines a second time.
1338            self.top_line.max(self.header_lines)
1339        };
1340        let mut skip = if header_rows > 0 { 0 } else { self.top_row };
1341
1342        while body.len() < body_rows {
1343            if line_n >= total_lines {
1344                let mut row = Vec::with_capacity(self.cols as usize);
1345                if scol > 0 {
1346                    for _ in 0..scol { row.push(Cell::Empty); }
1347                }
1348                if gutter > 0 {
1349                    for _ in 0..gutter { row.push(Cell::Empty); }
1350                }
1351                while row.len() < self.cols as usize { row.push(Cell::Empty); }
1352                body.push(row);
1353                row_styles.push(RowStyle::Normal);
1354                highlights.push(Vec::new());
1355                raw_rows.push(None);
1356                line_n += 1;
1357                continue;
1358            }
1359            // Filter evaluation runs on the raw line (it uses captures, not
1360            // text), but rendering goes through the template if one is set.
1361            let raw = src.bytes(idx.line_range(line_n, src));
1362            // `-s` / --squeeze-blank-lines: skip a blank line if its
1363            // immediate predecessor (in logical-line space) was also blank.
1364            // Real line numbers / counts in `idx` stay accurate — this is a
1365            // display-layer filter only.
1366            if self.squeeze_blanks && line_is_blank(&raw) {
1367                let prev_blank = line_n.checked_sub(1).is_some_and(|p| {
1368                    let prev = src.bytes(idx.line_range(p, src));
1369                    line_is_blank(&prev)
1370                });
1371                if prev_blank {
1372                    line_n += 1;
1373                    continue;
1374                }
1375            }
1376            let display_bytes = if let Some(r) = self.display.as_ref() {
1377                match r.render_line(&raw) {
1378                    Some(s) => std::borrow::Cow::Owned(s.into_bytes()),
1379                    None => raw.clone(),
1380                }
1381            } else {
1382                raw.clone()
1383            };
1384            let state_arg = if self.ansi_mode == crate::render::AnsiMode::Interpret {
1385                Some(&mut render_state)
1386            } else {
1387                None
1388            };
1389            let rows = render_line(&display_bytes, &r_opts, state_arg);
1390            let style = if self.filter.is_some() || self.grep.is_some() {
1391                if self.dim_mode {
1392                    if self.should_dim_line(line_n, idx, src) { RowStyle::Dim } else { RowStyle::Normal }
1393                } else {
1394                    // hide mode: only matching lines reach here
1395                    RowStyle::Normal
1396                }
1397            } else {
1398                RowStyle::Normal
1399            };
1400
1401            let mut first_emitted_for_this_line = true;
1402            // `-J` status column: remember the body index of this line's first
1403            // emitted display row, and whether ANY of its rows hold a search
1404            // match. We patch the glyph in after rendering the whole line so a
1405            // match on a wrapped continuation row still flags the first row.
1406            let mut status_first_row_idx: Option<usize> = None;
1407            let mut line_matched = false;
1408            for (i, mut content_row) in rows.into_iter().enumerate() {
1409                if i < skip { continue; }
1410                if body.len() >= body_rows { break; }
1411                // Track whether this line carries a search match for `-J`,
1412                // matching against the CONTENT only (before the status cell +
1413                // gutter are prepended) so gutter digits / padding can't
1414                // falsely flag a `*`. Independent of `hilite_search` (`-G`):
1415                // the status column reflects matches even when visual
1416                // highlighting is suppressed.
1417                if scol > 0 && !line_matched {
1418                    if let Some(s) = self.search.as_ref() {
1419                        if !find_row_highlights(&content_row, &s.regex).is_empty() {
1420                            line_matched = true;
1421                        }
1422                    }
1423                }
1424                let mut full: Vec<Cell> = Vec::with_capacity(self.cols as usize);
1425                if scol > 0 {
1426                    if status_first_row_idx.is_none() {
1427                        status_first_row_idx = Some(body.len());
1428                    }
1429                    // Placeholder blank; patched to the real glyph after the
1430                    // line's rows are all rendered (so wrapped-row matches count).
1431                    full.push(Self::status_cell(' '));
1432                }
1433                if gutter > 0 {
1434                    let label = if i == 0 { format!("{:>width$} ", line_n + 1, width = (gutter as usize - 1)) } else { " ".repeat(gutter as usize) };
1435                    for c in label.chars() {
1436                        full.push(Cell::Char { ch: c, width: 1, style: crate::ansi::Style::default(), hyperlink: None });
1437                    }
1438                }
1439                full.append(&mut content_row);
1440                // Paint the `<` left-edge marker when the view is scrolled right
1441                // in chop mode, so the user can see that content is off-screen left.
1442                // Mirrors the `>` rscroll marker style (dim). Placed at the first
1443                // content column (after the status column + gutter, if any).
1444                if self.left_col > 0 && !self.opts.wrap {
1445                    let marker_col = (scol + gutter) as usize;
1446                    if let Some(cell) = full.get_mut(marker_col) {
1447                        *cell = Cell::Char {
1448                            ch: '<',
1449                            width: 1,
1450                            style: crate::ansi::Style { dim: true, ..Default::default() },
1451                            hyperlink: None,
1452                        };
1453                    }
1454                }
1455                // Compute search highlights for this display row by running
1456                // the regex against the row's rendered text. Each match's
1457                // char range maps to a cell column range via `starts`.
1458                let row_highlights = if let (true, Some(s)) = (self.hilite_search, self.search.as_ref()) {
1459                    find_row_highlights(&full, &s.regex)
1460                } else {
1461                    Vec::new()
1462                };
1463                body.push(full);
1464                row_styles.push(style);
1465                highlights.push(row_highlights);
1466                if raw_passthrough {
1467                    if first_emitted_for_this_line {
1468                        // Emit the original line bytes verbatim once. Sub-rows
1469                        // (mid-line wrap continuations) are no-ops — the
1470                        // terminal will have already consumed enough columns
1471                        // from the line's full byte stream to fill them.
1472                        raw_rows.push(Some(raw.to_vec()));
1473                        first_emitted_for_this_line = false;
1474                    } else {
1475                        raw_rows.push(Some(Vec::new()));
1476                    }
1477                } else {
1478                    raw_rows.push(None);
1479                }
1480            }
1481            // `-J`: now that the whole line is rendered, set the first row's
1482            // status glyph (mark letter beats search-`*`). Continuation rows
1483            // keep their blank placeholder.
1484            if let Some(fi) = status_first_row_idx {
1485                let glyph = self.status_glyph(line_n, line_matched);
1486                if glyph != ' ' {
1487                    if let Some(cell) = body[fi].first_mut() {
1488                        *cell = Self::status_cell(glyph);
1489                    }
1490                }
1491            }
1492            skip = 0;
1493            // Advance to next line — visible-space if hiding, logical-space otherwise.
1494            if hide {
1495                hide_pos += 1;
1496                line_n = self.visible_lines.get(hide_pos).copied().unwrap_or(total_lines);
1497            } else {
1498                line_n += 1;
1499            }
1500        }
1501
1502        // After walking through the frame, render_state has been advanced past
1503        // top_line. Invalidate the cached sentinel so next frame re-reconstructs.
1504        self.render_state_for = usize::MAX;
1505
1506        let status = self.format_status(idx, src);
1507        Frame { body, row_styles, highlights, status, status_style: self.status_style, raw_rows, image_blob: None }
1508    }
1509
1510    fn format_status(&self, idx: &LineIndex, src: &dyn Source) -> String {
1511        if let Some(p) = self.prompt.as_ref() {
1512            let ctx = self.build_prompt_context(idx, src);
1513            return p.render(&ctx);
1514        }
1515        let body_rows = self.body_rows() as usize;
1516        let total = idx.line_count();
1517        // In hide mode, the line range and percentage refer to visible (matched)
1518        // lines, not the underlying logical line count.
1519        let (top, bottom, total_for_pct, total_str): (usize, usize, usize, String) = if self.hide_mode() {
1520            let visible_total = self.visible_lines.len();
1521            // top_line is a logical line; find its visible index.
1522            let cur = self
1523                .visible_lines
1524                .iter()
1525                .position(|&l| l >= self.top_line)
1526                .unwrap_or(visible_total);
1527            let top = cur + 1;
1528            let bottom = (cur + body_rows).min(visible_total.max(1));
1529            let total_str = if src.is_complete() {
1530                format!("{visible_total}/{total}")
1531            } else {
1532                format!("{visible_total}/{total}+")
1533            };
1534            (top, bottom, visible_total, total_str)
1535        } else {
1536            let top = self.top_line + 1;
1537            let bottom = (self.top_line + body_rows).min(total.max(1));
1538            let total_str = if src.is_complete() { format!("{total}") } else { format!("{total}+") };
1539            (top, bottom, total, total_str)
1540        };
1541        let pct = (bottom * 100).checked_div(total_for_pct).unwrap_or(0);
1542        // In records mode, prefix line numbers with 'L' and append an 'R' record block.
1543        // The R block always refers to logical lines on screen, which in hide
1544        // mode is *not* the same as `bottom` (which counts visible matches).
1545        let bottom_line = self.bottom_visible_line(idx);
1546        let (line_prefix, records_block) = if idx.records_mode() {
1547            let line_total = idx.line_count();
1548            let rec_total = idx.record_count();
1549            let rec_block = if line_total == 0 || rec_total == 0 {
1550                format!("R0-0/{}", rec_total)
1551            } else {
1552                let rec_top = idx.line_to_record(self.top_line) + 1;
1553                let rec_bottom = idx.line_to_record(bottom_line) + 1;
1554                let (rec_top, rec_bottom) = if rec_bottom < rec_top {
1555                    // Defensive: should be unreachable given `bottom_visible_line`
1556                    // is always `>= self.top_line`, but guard against future
1557                    // regressions producing nonsense like `R290-8/...`.
1558                    (rec_top, rec_top)
1559                } else {
1560                    (rec_top, rec_bottom)
1561                };
1562                format!("R{}-{}/{}", rec_top, rec_bottom, rec_total)
1563            };
1564            ("L", Some(rec_block))
1565        } else {
1566            ("", None)
1567        };
1568        let middle = match records_block {
1569            Some(ref rb) => format!("{}{}-{}/{}  {}  {}%", line_prefix, top, bottom, total_str, rb, pct),
1570            None         => format!("{}-{}/{}  {}%", top, bottom, total_str, pct),
1571        };
1572        let label_with_index = match self.file_index {
1573            Some((current, total)) => format!("{}  [{}/{}]", self.source_label, current + 1, total),
1574            None => self.source_label.clone(),
1575        };
1576        let mut s = format!("{}  {}", label_with_index, middle);
1577        // Wrap-row offset: when scrolled inside a long wrapping line, surface
1578        // the offset so the user knows scrolling is happening at sub-line
1579        // granularity. Without this the line range above stays static while
1580        // pressing `j` and the scroll is invisible on repeating content.
1581        if !self.hide_mode() && self.top_row > 0 {
1582            let line_rows = if total > 0 {
1583                let bytes = self.line_display_bytes(src, idx, self.top_line);
1584                count_rows(&bytes, &self.render_opts(self.gutter_width(idx)), None)
1585            } else { 1 };
1586            s.push_str(&format!("  +{}/{}", self.top_row, line_rows));
1587        }
1588        if self.left_col > 0 {
1589            s.push_str(&format!("  \u{00bb}{}", self.left_col));
1590        }
1591        if let Some(f) = self.filter.as_ref() {
1592            s.push_str(&format!("  [{}]", f.format_name));
1593        }
1594        if self.grep.is_some() {
1595            s.push_str("  [grep]");
1596        }
1597        if self.or_groups.is_active() {
1598            s.push_str("  [or]");
1599        }
1600        if self.filter.is_some() || self.grep.is_some() || self.or_groups.is_active() {
1601            s.push_str(if self.dim_mode { "  [dim]" } else { "  [hide]" });
1602        }
1603        if let Some(sr) = self.search.as_ref() {
1604            let prefix = if matches!(sr.direction, SearchDirection::Forward) { "/" } else { "?" };
1605            s.push_str(&format!("  [{}{}]", prefix, sr.raw));
1606        }
1607        if let Some(label) = self.prettify_label.as_ref() {
1608            s.push_str(&format!("  [pretty:{label}]"));
1609        }
1610        if self.live_mode { s.push_str("  (L)"); }
1611        if self.follow_mode {
1612            if let Some((msg, _)) = self.status_flash.as_ref() {
1613                s.push_str("  ");
1614                s.push_str(msg);
1615            } else if self.is_idle() {
1616                s.push_str("  (F idle)");
1617            } else {
1618                s.push_str("  (F)");
1619            }
1620        }
1621        if let Some(msg) = self.preprocess_failure.as_ref() {
1622            let first_line = msg.lines().next().unwrap_or("");
1623            s.push_str(&format!("  [preprocess-failed: {}]", first_line));
1624        }
1625        let tag_suffix = match &self.tag_active {
1626            Some((name, cur, total)) if *total > 1 => {
1627                format!("  [tag: {name} ({cur}/{total})]")
1628            }
1629            _ => String::new(),
1630        };
1631        s.push_str(&tag_suffix);
1632        // Right-aligned :help hint. If the existing status already overshoots
1633        // the width, no pad — the renderer will clip on draw.
1634        let used = s.chars().count();
1635        let hint = ":help";
1636        if (self.cols as usize) > used + 1 + hint.chars().count() {
1637            let pad = self.cols as usize - used - hint.chars().count();
1638            s.push_str(&" ".repeat(pad));
1639            s.push_str(hint);
1640        } else {
1641            s.push(' ');
1642            s.push_str(hint);
1643        }
1644        s
1645    }
1646
1647    fn build_prompt_context(&self, idx: &LineIndex, src: &dyn Source) -> crate::prompt::PromptContext {
1648        use crate::prompt::PromptContext;
1649
1650        let body_rows = self.body_rows() as usize;
1651        let total = idx.line_count();
1652        let top = self.top_line + 1;
1653        let bottom = (self.top_line + body_rows).min(total.max(1));
1654        let pct = (bottom * 100).checked_div(total).unwrap_or(0);
1655        let bottom_line = self.bottom_visible_line(idx);
1656
1657        let records_mode = idx.records_mode();
1658        let (rec_top, rec_bottom, rec_total) = if records_mode {
1659            let rt = idx.line_to_record(self.top_line) + 1;
1660            let rb_raw = idx.line_to_record(bottom_line) + 1;
1661            let rb = if rb_raw < rt { rt } else { rb_raw };
1662            (rt, rb, idx.record_count())
1663        } else {
1664            (0, 0, 0)
1665        };
1666
1667        let wrap_offset = if !self.hide_mode() && self.top_row > 0 {
1668            let line_rows = if total > 0 {
1669                let bytes = self.line_display_bytes(src, idx, self.top_line);
1670                count_rows(&bytes, &self.render_opts(self.gutter_width(idx)), None)
1671            } else { 1 };
1672            format!("+{}/{}", self.top_row, line_rows)
1673        } else {
1674            String::new()
1675        };
1676
1677        let col_offset = if self.left_col > 0 { format!("  \u{00bb}{}", self.left_col) } else { String::new() };
1678
1679        let format_tag = self.format_label.as_ref()
1680            .map(|n| format!("  [{}]", n))
1681            .unwrap_or_default();
1682        let filter_tag = self.filter.as_ref()
1683            .map(|f| format!("  [{}]", f.format_name))
1684            .unwrap_or_default();
1685        let grep_tag = if self.grep.is_some() { "  [grep]".to_string() } else { String::new() };
1686        let or_tag = if self.or_groups.is_active() { "  [or]".to_string() } else { String::new() };
1687        let hide_tag = if self.filter.is_some() || self.grep.is_some() || self.or_groups.is_active() {
1688            if self.dim_mode { "  [dim]".to_string() } else { "  [hide]".to_string() }
1689        } else {
1690            String::new()
1691        };
1692        let search_tag = self.search.as_ref()
1693            .map(|s| {
1694                let p = if matches!(s.direction, SearchDirection::Forward) { "/" } else { "?" };
1695                format!("  [{}{}]", p, s.raw)
1696            })
1697            .unwrap_or_default();
1698        let pretty_tag = self.prettify_label.as_ref()
1699            .map(|l| format!("  [pretty:{l}]"))
1700            .unwrap_or_default();
1701        let live_tag = if self.live_mode { "  (L)".to_string() } else { String::new() };
1702        let follow_tag = if self.follow_mode { "  (F)".to_string() } else { String::new() };
1703        let preprocess_failed_tag = self.preprocess_failure.as_ref()
1704            .map(|msg| {
1705                let first_line = msg.lines().next().unwrap_or("");
1706                format!("  [preprocess-failed: {}]", first_line)
1707            })
1708            .unwrap_or_default();
1709
1710        let file_index_tag = match self.file_index {
1711            Some((current, total)) => format!("  [{}/{}]", current + 1, total),
1712            None => String::new(),
1713        };
1714
1715        let tag_tag = match &self.tag_active {
1716            Some((name, cur, total)) if *total > 1 => {
1717                format!("  [tag: {name} ({cur}/{total})]")
1718            }
1719            _ => String::new(),
1720        };
1721
1722        PromptContext {
1723            label: self.source_label.clone(),
1724            top,
1725            bottom,
1726            total,
1727            pct: pct.min(100) as u8,
1728            rec_top,
1729            rec_bottom,
1730            rec_total,
1731            records_mode,
1732            wrap_offset,
1733            col_offset,
1734            format_tag,
1735            filter_tag,
1736            grep_tag,
1737            or_tag,
1738            hide_tag,
1739            search_tag,
1740            pretty_tag,
1741            live_tag,
1742            follow_tag,
1743            preprocess_failed_tag,
1744            file_index_tag,
1745            tag_tag,
1746        }
1747    }
1748
1749    fn frame_hex(&self, src: &dyn Source) -> Frame {
1750        use crate::hex::format_hex_row;
1751        use crate::render::{render_line, Cell, RenderOpts};
1752
1753        let body_rows = self.rows.saturating_sub(1) as usize;
1754        let total_bytes = src.len();
1755        let total_hex_rows = total_bytes.div_ceil(16);
1756
1757        let mut body: Vec<Vec<Cell>> = Vec::with_capacity(body_rows);
1758        let mut row_styles: Vec<RowStyle> = Vec::with_capacity(body_rows);
1759        let mut highlights: Vec<Vec<std::ops::Range<usize>>> = Vec::with_capacity(body_rows);
1760
1761        let opts = RenderOpts { cols: self.cols, wrap: false, tab_width: 1, mode: crate::render::AnsiMode::Strict, rscroll_char: None, word_wrap: false, left_col: 0, tab_stops: None };
1762
1763        for row_idx in 0..body_rows {
1764            let hex_row = self.top_line + row_idx;
1765            if hex_row >= total_hex_rows {
1766                body.push(vec![Cell::Empty; self.cols as usize]);
1767            } else {
1768                let offset = hex_row * 16;
1769                let end = (offset + 16).min(total_bytes);
1770                let bytes_cow = src.bytes(offset..end);
1771                let text = format_hex_row(offset, &bytes_cow, self.hex_group_size);
1772                let rows = render_line(text.as_bytes(), &opts, None);
1773                body.push(rows.into_iter().next().unwrap_or_else(|| {
1774                    vec![Cell::Empty; self.cols as usize]
1775                }));
1776            }
1777            row_styles.push(RowStyle::Normal);
1778            highlights.push(Vec::new());
1779        }
1780
1781        let status = self.format_status_hex(src);
1782        let raw_rows = vec![None; body.len()];
1783        Frame { body, row_styles, highlights, status, status_style: self.status_style, raw_rows, image_blob: None }
1784    }
1785
1786    fn format_status_hex(&self, src: &dyn Source) -> String {
1787        let total_bytes = src.len();
1788        let body_rows = self.rows.saturating_sub(1) as usize;
1789        // Byte offset of the first visible byte (start of the top hex row).
1790        let top_byte = self.top_line * 16;
1791        // Byte offset just past the last visible byte. Clamped to total_bytes
1792        // so we never show a value past EOF.
1793        let bottom_byte = ((self.top_line + body_rows) * 16).min(total_bytes);
1794        let pct = (bottom_byte * 100).checked_div(total_bytes).unwrap_or(0);
1795        let label_with_index = match self.file_index {
1796            Some((current, total)) => format!("{}  [{}/{}]", self.source_label, current + 1, total),
1797            None => self.source_label.clone(),
1798        };
1799        let tag_suffix = match &self.tag_active {
1800            Some((name, cur, total)) if *total > 1 => {
1801                format!("  [tag: {name} ({cur}/{total})]")
1802            }
1803            _ => String::new(),
1804        };
1805        format!(
1806            "{}  off {}-{}/{}  {}%  [hex]{}",
1807            label_with_index, top_byte, bottom_byte, total_bytes, pct, tag_suffix
1808        )
1809    }
1810
1811    #[cfg(feature = "image")]
1812    fn frame_image(&mut self) -> Frame {
1813        use crate::render::Cell;
1814        if self.image_protocol != ImageProtocol::Ascii {
1815            return self.frame_image_protocol();
1816        }
1817        let body_rows = self.body_rows() as usize;
1818        let cols = self.cols as usize;
1819        let img = match self.current_image() {
1820            Some(i) => i,
1821            None => {
1822                let body = vec![vec![Cell::Empty; cols]; body_rows];
1823                return Frame {
1824                    body,
1825                    row_styles: vec![RowStyle::Normal; body_rows],
1826                    highlights: vec![Vec::new(); body_rows],
1827                    status: self.image_format.clone(),
1828                    status_style: self.status_style,
1829                    raw_rows: vec![None; body_rows],
1830                    image_blob: None,
1831                };
1832            }
1833        };
1834        let color = !self.image_no_color;
1835        let grid = crate::image_render::render_image(img, self.image_cols(), self.image_style, color);
1836        let grid_w = grid.first().map(|r| r.len()).unwrap_or(0);
1837        let max_off = grid_w.saturating_sub(cols);
1838        if self.left_col > max_off { self.left_col = max_off; }
1839        let off = self.left_col;
1840        let mut body: Vec<Vec<Cell>> = Vec::with_capacity(body_rows);
1841        for r in 0..body_rows {
1842            let gi = self.top_line + r;
1843            if gi < grid.len() {
1844                let mut row: Vec<Cell> = grid[gi].iter().skip(off).take(cols).cloned().collect();
1845                while row.len() < cols { row.push(Cell::Empty); }
1846                body.push(row);
1847            } else {
1848                body.push(vec![Cell::Empty; cols]);
1849            }
1850        }
1851        let status = self.format_status_image(grid.len());
1852        Frame {
1853            body,
1854            row_styles: vec![RowStyle::Normal; body_rows],
1855            highlights: vec![Vec::new(); body_rows],
1856            status,
1857            status_style: self.status_style,
1858            raw_rows: vec![None; body_rows],
1859            image_blob: None,
1860        }
1861    }
1862
1863    #[cfg(feature = "image")]
1864    fn format_status_image(&self, total_rows: usize) -> String {
1865        let body = self.body_rows() as usize;
1866        let top = self.top_line + 1;
1867        let bottom = (self.top_line + body).min(total_rows.max(1));
1868        let dims = self.current_image().map(|i| { let (w, h) = i.dimensions(); format!("{w}×{h}") }).unwrap_or_default();
1869        let mut s = format!("{}  {}  {}  rows {}-{}/{}", self.source_label, dims, self.image_format, top, bottom, total_rows);
1870        if self.left_col > 0 {
1871            s.push_str(&format!("  \u{00bb}{}", self.left_col));
1872        }
1873        s.push_str(&self.anim_badge());
1874        s
1875    }
1876
1877    #[cfg(feature = "image")]
1878    fn frame_image_protocol(&mut self) -> Frame {
1879        use crate::render::Cell;
1880        let body_rows = self.body_rows() as usize;
1881        let cols = self.cols as usize;
1882        let status_style = self.status_style;
1883        let blank = |status: String, blob: Option<Vec<u8>>| Frame {
1884            body: vec![vec![Cell::Empty; cols]; body_rows],
1885            row_styles: vec![RowStyle::Normal; body_rows],
1886            highlights: vec![Vec::new(); body_rows],
1887            status,
1888            status_style,
1889            raw_rows: vec![None; body_rows],
1890            image_blob: blob,
1891        };
1892        let (iw, ih) = match self.current_image() {
1893            Some(i) => i.dimensions(),
1894            None => return blank(self.image_format.clone(), None),
1895        };
1896        let ch = self.cell_px.1.max(1) as u32;
1897        let (scaled_w, scaled_h) = protocol_scaled_dims(iw, ih, self.cols, self.cell_px, self.image_width);
1898
1899        // Build / reuse the width-scaled image.
1900        let need = self.image_scaled.as_ref().map(|(c, _)| *c != scaled_w as u16).unwrap_or(true);
1901        if need {
1902            let scaled = {
1903                let src = self.current_image().unwrap();
1904                image::imageops::resize(src, scaled_w, scaled_h, image::imageops::FilterType::Triangle)
1905            };
1906            self.image_scaled = Some((scaled_w as u16, scaled));
1907        }
1908
1909        let total_rows = protocol_occupied_rows(iw, ih, self.cols, self.cell_px, self.image_width);
1910        let max_top = total_rows.saturating_sub(body_rows);
1911        if self.top_line > max_top { self.top_line = max_top; }
1912        self.left_col = 0; // horizontal scroll is a no-op in protocol mode
1913
1914        let y0 = (self.top_line as u32 * ch).min(scaled_h);
1915        let band_h = ((body_rows as u32) * ch).min(scaled_h - y0).max(1);
1916        let scaled = &self.image_scaled.as_ref().unwrap().1;
1917        let band = image::imageops::crop_imm(scaled, 0, y0, scaled_w, band_h).to_image();
1918        let blob = match self.image_protocol {
1919            ImageProtocol::Kitty => crate::image_protocol::encode_kitty(&band),
1920            ImageProtocol::Sixel => crate::image_protocol::encode_sixel(&band),
1921            ImageProtocol::Ascii => unreachable!("frame_image_protocol only entered for non-Ascii"),
1922        };
1923        let status = self.format_status_image_protocol(total_rows);
1924        blank(status, Some(blob))
1925    }
1926
1927    #[cfg(feature = "image")]
1928    fn format_status_image_protocol(&self, total_rows: usize) -> String {
1929        let body = self.body_rows() as usize;
1930        let top = self.top_line + 1;
1931        let bottom = (self.top_line + body).min(total_rows.max(1));
1932        let proto = match self.image_protocol {
1933            ImageProtocol::Kitty => "kitty",
1934            ImageProtocol::Sixel => "sixel",
1935            ImageProtocol::Ascii => "ascii",
1936        };
1937        let dims = self.current_image().map(|i| { let (w, h) = i.dimensions(); format!("{w}×{h}") }).unwrap_or_default();
1938        format!("{}  {}  {}  [{}]  rows {}-{}/{}{}", self.source_label, dims, self.image_format, proto, top, bottom, total_rows, self.anim_badge())
1939    }
1940
1941    /// Jump by whole logical lines, regardless of wrap rows. `top_row` is
1942    /// reset to 0 so the start of the destination line is at the top of
1943    /// the viewport. In hide mode this is equivalent to `scroll_lines`
1944    /// (which already moves by visible/logical lines).
1945    pub fn scroll_logical_lines(&mut self, delta: i64, src: &dyn Source, idx: &mut LineIndex) {
1946        if delta == 0 { return; }
1947        #[cfg(feature = "image")]
1948        if self.image_mode {
1949            self.scroll_lines(delta, src, idx);
1950            return;
1951        }
1952        if self.hide_mode() {
1953            // J/K move by whole visible lines (ignoring wrap rows), with K
1954            // first snapping to the start of the current line — the visible-
1955            // line analogue of the non-hide branch below.
1956            self.extend_visible_lines(idx, src);
1957            let n = self.visible_lines.len();
1958            if n == 0 {
1959                self.top_line = 0;
1960                self.top_row = 0;
1961                return;
1962            }
1963            let vi = self
1964                .visible_lines
1965                .iter()
1966                .position(|&l| l >= self.top_line)
1967                .unwrap_or(n - 1);
1968            if delta > 0 {
1969                let target = (vi + delta as usize).min(n - 1);
1970                self.top_line = self.visible_lines[target];
1971                self.top_row = 0;
1972            } else {
1973                let back = (-delta) as usize;
1974                let consumed_for_snap = if self.top_row > 0 { 1 } else { 0 };
1975                let extra_back = back.saturating_sub(consumed_for_snap);
1976                self.top_line = self.visible_lines[vi.saturating_sub(extra_back)];
1977                self.top_row = 0;
1978            }
1979            return;
1980        }
1981        if delta > 0 {
1982            idx.extend_to_line(self.top_line + delta as usize + 1, src);
1983            let total = idx.line_count();
1984            if total == 0 { return; }
1985            let target = (self.top_line as i64 + delta).min(total as i64 - 1) as usize;
1986            self.top_line = target;
1987            self.top_row = 0;
1988        } else {
1989            let back = (-delta) as usize;
1990            // If we're inside a wrapped line (top_row > 0), `K` first snaps to
1991            // the start of the current line; only the remaining count goes to
1992            // previous lines. This matches the user's mental model of "jump
1993            // to the start of the previous line".
1994            let consumed_for_snap = if self.top_row > 0 { 1 } else { 0 };
1995            let extra_back = back.saturating_sub(consumed_for_snap);
1996            self.top_line = self.top_line.saturating_sub(extra_back);
1997            self.top_row = 0;
1998        }
1999    }
2000
2001    pub fn scroll_lines(&mut self, delta: i64, src: &dyn Source, idx: &mut LineIndex) {
2002        if delta == 0 { return; }
2003        #[cfg(feature = "image")]
2004        if self.image_mode {
2005            let total = self.image_total_rows();
2006            let body = self.body_rows() as usize;
2007            let max_top = total.saturating_sub(body);
2008            let next = (self.top_line as i64 + delta).clamp(0, max_top as i64);
2009            self.top_line = next as usize;
2010            self.top_row = 0;
2011            return;
2012        }
2013        if self.hide_mode() {
2014            // Scroll by display rows over the visible (matching) lines, honoring
2015            // wrap rows via top_row — the same model as the non-hide branch
2016            // below, but walking visible_lines instead of every line.
2017            self.extend_visible_lines(idx, src);
2018            let n = self.visible_lines.len();
2019            if n == 0 {
2020                self.top_line = 0;
2021                self.top_row = 0;
2022                return;
2023            }
2024            let mut vi = self
2025                .visible_lines
2026                .iter()
2027                .position(|&l| l >= self.top_line)
2028                .unwrap_or(n - 1);
2029            // Keep top anchored on a real visible line; a top_row only means
2030            // something relative to one.
2031            if self.visible_lines[vi] != self.top_line {
2032                self.top_row = 0;
2033            }
2034            self.top_line = self.visible_lines[vi];
2035            let r_opts = self.render_opts(self.gutter_width(idx));
2036            if delta > 0 {
2037                let mut remaining = delta as usize;
2038                while remaining > 0 {
2039                    let line = self.visible_lines[vi];
2040                    let rows = count_rows(&self.line_display_bytes(src, idx, line), &r_opts, None).max(1);
2041                    if self.top_row + 1 < rows {
2042                        self.top_row += 1;
2043                    } else if vi + 1 < n {
2044                        self.top_row = 0;
2045                        vi += 1;
2046                        self.top_line = self.visible_lines[vi];
2047                    } else {
2048                        break;
2049                    }
2050                    remaining -= 1;
2051                }
2052                let anchor = self.hide_bottom_anchor(src, idx);
2053                if (self.top_line, self.top_row) > anchor {
2054                    self.top_line = anchor.0;
2055                    self.top_row = anchor.1;
2056                }
2057            } else {
2058                let mut remaining = (-delta) as usize;
2059                while remaining > 0 {
2060                    if self.top_row > 0 {
2061                        self.top_row -= 1;
2062                    } else if vi > 0 {
2063                        vi -= 1;
2064                        self.top_line = self.visible_lines[vi];
2065                        let line = self.visible_lines[vi];
2066                        let rows = count_rows(&self.line_display_bytes(src, idx, line), &r_opts, None).max(1);
2067                        self.top_row = rows.saturating_sub(1);
2068                    } else {
2069                        break;
2070                    }
2071                    remaining -= 1;
2072                }
2073            }
2074            return;
2075        }
2076        if delta > 0 {
2077            let mut remaining = delta as usize;
2078            while remaining > 0 {
2079                idx.extend_to_line(self.top_line + 1, src);
2080                let total = idx.line_count();
2081                if total == 0 { break; }
2082                let bytes = self.line_display_bytes(src, idx, self.top_line);
2083                let line_rows = count_rows(&bytes, &self.render_opts(self.gutter_width(idx)), None);
2084                if self.top_row + 1 < line_rows {
2085                    self.top_row += 1;
2086                } else if self.top_line + 1 < total {
2087                    self.top_row = 0;
2088                    self.top_line += 1;
2089                } else {
2090                    break;
2091                }
2092                remaining -= 1;
2093            }
2094            // Don't scroll past the natural bottom (the last line resting on
2095            // the last body row). Only clamp once the source is fully
2096            // scanned — mid-scan the true end is unknown, and clamping to a
2097            // partial index would strand the viewport short of unread content.
2098            if idx.scanned_through() >= src.len() {
2099                let anchor = self.bottom_anchor(src, idx);
2100                if (self.top_line, self.top_row) > anchor {
2101                    self.top_line = anchor.0;
2102                    self.top_row = anchor.1;
2103                }
2104            }
2105        } else {
2106            let mut remaining = (-delta) as usize;
2107            while remaining > 0 {
2108                if self.top_row > 0 {
2109                    self.top_row -= 1;
2110                } else if self.top_line > 0 {
2111                    self.top_line -= 1;
2112                    let bytes = self.line_display_bytes(src, idx, self.top_line);
2113                    let line_rows = count_rows(&bytes, &self.render_opts(self.gutter_width(idx)), None);
2114                    self.top_row = line_rows.saturating_sub(1);
2115                } else {
2116                    break;
2117                }
2118                remaining -= 1;
2119            }
2120        }
2121    }
2122
2123    pub fn page_down(&mut self, src: &dyn Source, idx: &mut LineIndex) {
2124        let n = self.page_size
2125            .map(|p| p as i64)
2126            .unwrap_or_else(|| self.body_rows() as i64);
2127        self.scroll_lines(n, src, idx);
2128    }
2129
2130    pub fn page_up(&mut self, src: &dyn Source, idx: &mut LineIndex) {
2131        let n = self.page_size
2132            .map(|p| p as i64)
2133            .unwrap_or_else(|| self.body_rows() as i64);
2134        self.scroll_lines(-n, src, idx);
2135    }
2136
2137    pub fn half_page_down(&mut self, src: &dyn Source, idx: &mut LineIndex) {
2138        let n = (self.body_rows() / 2).max(1) as i64;
2139        self.scroll_lines(n, src, idx);
2140    }
2141
2142    pub fn half_page_up(&mut self, src: &dyn Source, idx: &mut LineIndex) {
2143        let n = (self.body_rows() / 2).max(1) as i64;
2144        self.scroll_lines(-n, src, idx);
2145    }
2146
2147    pub fn goto_top(&mut self) {
2148        self.top_line = 0;
2149        self.top_row = 0;
2150    }
2151
2152    /// The top `(line, row)` position such that the source's final display row
2153    /// lands on the last body row. Computed in display-row units by walking
2154    /// backward from the end, so it stays correct when lines wrap (the
2155    /// default): the last `body` logical lines can occupy more than `body`
2156    /// rows. Returns `(0, 0)` when the whole document fits within the body.
2157    /// Non-hide-mode only — hide mode scrolls by whole visible lines.
2158    fn bottom_anchor(&self, src: &dyn Source, idx: &LineIndex) -> (usize, usize) {
2159        let body = self.body_rows() as usize;
2160        let total = idx.line_count();
2161        if total == 0 || body == 0 {
2162            return (0, 0);
2163        }
2164        let r_opts = self.render_opts(self.gutter_width(idx));
2165        let mut remaining = body;
2166        let mut line = total - 1;
2167        loop {
2168            let bytes = self.line_display_bytes(src, idx, line);
2169            let line_rows = count_rows(&bytes, &r_opts, None).max(1);
2170            if line_rows >= remaining {
2171                return (line, line_rows - remaining);
2172            }
2173            remaining -= line_rows;
2174            if line == 0 {
2175                return (0, 0);
2176            }
2177            line -= 1;
2178        }
2179    }
2180
2181    /// Hide-mode bottom anchor, in display-row units over the *visible*
2182    /// (matching) lines: the top `(line, row)` such that the last visible
2183    /// line's final wrap row lands on the last body row. Mirrors
2184    /// `bottom_anchor`, but walks `visible_lines` instead of every line.
2185    fn hide_bottom_anchor(&self, src: &dyn Source, idx: &LineIndex) -> (usize, usize) {
2186        let body = self.body_rows() as usize;
2187        let n = self.visible_lines.len();
2188        if n == 0 || body == 0 {
2189            return (0, 0);
2190        }
2191        let r_opts = self.render_opts(self.gutter_width(idx));
2192        let mut remaining = body;
2193        let mut vi = n - 1;
2194        loop {
2195            let line = self.visible_lines[vi];
2196            let rows = count_rows(&self.line_display_bytes(src, idx, line), &r_opts, None).max(1);
2197            if rows >= remaining {
2198                return (line, rows - remaining);
2199            }
2200            remaining -= rows;
2201            if vi == 0 {
2202                return (self.visible_lines[0], 0);
2203            }
2204            vi -= 1;
2205        }
2206    }
2207
2208    pub fn goto_bottom(&mut self, src: &dyn Source, idx: &mut LineIndex) {
2209        #[cfg(feature = "image")]
2210        if self.image_mode {
2211            let body = self.body_rows() as usize;
2212            self.top_line = self.image_total_rows().saturating_sub(body);
2213            self.top_row = 0;
2214            return;
2215        }
2216        idx.extend_to_end(src);
2217        if self.hide_mode() {
2218            self.extend_visible_lines(idx, src);
2219            let (line, row) = self.hide_bottom_anchor(src, idx);
2220            self.top_line = line;
2221            self.top_row = row;
2222        } else {
2223            let (line, row) = self.bottom_anchor(src, idx);
2224            self.top_line = line;
2225            self.top_row = row;
2226        }
2227    }
2228
2229    /// Position the viewport so line `n` (0-indexed) is the top visible line.
2230    pub fn goto_line(&mut self, n: usize, src: &dyn Source, idx: &mut LineIndex) {
2231        idx.extend_to_line(n, src);
2232        let target = n.min(idx.line_count().saturating_sub(1));
2233        self.top_line = target;
2234        self.top_row = 0;
2235    }
2236
2237    /// Position the viewport at the start of record `n` (0-indexed).
2238    pub fn goto_record(&mut self, n: usize, src: &dyn Source, idx: &mut LineIndex) {
2239        // Ensure the record exists by extending the index. Records can only
2240        // appear after their constituent lines are scanned; extend repeatedly
2241        // until the record exists or we hit EOF.
2242        while idx.record_count() <= n && idx.scanned_through() < src.len() {
2243            idx.extend_to_end(src);
2244        }
2245        if idx.record_count() == 0 {
2246            return;
2247        }
2248        let target = n.min(idx.record_count().saturating_sub(1));
2249        let line_range = idx.record_line_range(target);
2250        self.top_line = line_range.start;
2251        self.top_row = 0;
2252    }
2253
2254    /// Position the viewport at `p` percent through the file by bytes.
2255    /// `p` is clamped to 0..=100. p=100 lands at the last line.
2256    pub fn goto_percent(&mut self, p: u8, src: &dyn Source, idx: &mut LineIndex) {
2257        let p = p.min(100) as usize;
2258        let target_byte = src.len().saturating_mul(p) / 100;
2259        idx.extend_to_byte_for_query(src, target_byte);
2260        let line_n = idx.line_at_byte(target_byte)
2261            .or_else(|| {
2262                // target_byte at or past EOF: fall through to the last line.
2263                let lc = idx.line_count();
2264                if lc > 0 { Some(lc - 1) } else { None }
2265            })
2266            .unwrap_or(0);
2267        self.top_line = line_n;
2268        self.top_row = 0;
2269    }
2270
2271    /// Get the currently top-displayed physical line index.
2272    pub fn top_line(&self) -> usize {
2273        self.top_line
2274    }
2275
2276    pub fn resize(&mut self, cols: u16, rows: u16) {
2277        self.cols = cols.max(1);
2278        self.rows = rows.max(2);
2279        self.opts.cols = self.cols;
2280    }
2281
2282    pub fn toggle_line_numbers(&mut self) {
2283        self.show_line_numbers = !self.show_line_numbers;
2284    }
2285
2286    pub fn toggle_chop(&mut self) {
2287        self.opts.wrap = !self.opts.wrap;
2288        if self.opts.wrap {
2289            self.left_col = 0;
2290        }
2291    }
2292
2293    const HSCROLL_STEP: usize = 8;
2294
2295    /// Horizontal scroll is available only for non-wrapping content: image
2296    /// mode, or chop-mode text. Wrap mode has no horizontal axis; hex (fixed
2297    /// xxd layout) and raw (`AnsiMode::Raw`) are excluded.
2298    pub fn hscroll_active(&self) -> bool {
2299        #[cfg(feature = "image")]
2300        if self.current_image().is_some() {
2301            return true;
2302        }
2303        !self.opts.wrap
2304            && !self.hex_mode
2305            && self.ansi_mode != crate::render::AnsiMode::Raw
2306    }
2307
2308    fn hscroll_by(&mut self, delta: isize) {
2309        if !self.hscroll_active() {
2310            return;
2311        }
2312        self.left_col = (self.left_col as isize + delta).max(0) as usize;
2313        // Upper bound is enforced per-frame by the clamp in Task 3/4.
2314    }
2315
2316    pub fn hscroll_left_half(&mut self)  { let h = (self.cols as usize / 2).max(1) as isize; self.hscroll_by(-h); }
2317    pub fn hscroll_right_half(&mut self) { let h = (self.cols as usize / 2).max(1) as isize; self.hscroll_by(h); }
2318    pub fn hscroll_left_step(&mut self)  { self.hscroll_by(-(Self::HSCROLL_STEP as isize)); }
2319    pub fn hscroll_right_step(&mut self) { self.hscroll_by(Self::HSCROLL_STEP as isize); }
2320
2321    /// Scroll left by an explicit column count (used by `--shift N`).
2322    pub fn hscroll_left_cols(&mut self, n: u16)  { self.hscroll_by(-(n as isize)); }
2323    /// Scroll right by an explicit column count (used by `--shift N`).
2324    pub fn hscroll_right_cols(&mut self, n: u16) { self.hscroll_by(n as isize); }
2325
2326    pub fn left_col(&self) -> usize { self.left_col }
2327
2328    /// Drop the horizontal scroll offset. Called when a new file is opened
2329    /// (a fresh document has no relationship to the prior horizontal position).
2330    pub fn reset_hscroll(&mut self) { self.left_col = 0; }
2331
2332    /// Return the current set of visible (matched) line indices. Non-empty only
2333    /// in hide mode (filter or grep active without --dim). Stable public accessor
2334    /// so integration tests and external tooling can inspect filter results.
2335    pub fn visible_lines(&self) -> &[usize] { &self.visible_lines }
2336}
2337
2338/// Fit-width scaled pixel dimensions of an image: width = target_cols cells
2339/// wide (in pixels), height preserving aspect. `width_cols` overrides the
2340/// terminal `cols` when `Some` (from `--image-width`). Pure; the single source
2341/// of the protocol fit-width scaling formula.
2342#[cfg(feature = "image")]
2343pub fn protocol_scaled_dims(img_w: u32, img_h: u32, cols: u16,
2344                            cell_px: (u16, u16), width_cols: Option<usize>) -> (u32, u32) {
2345    let target_cols = width_cols.unwrap_or(cols as usize).max(1) as u32;
2346    let scaled_w = (target_cols * cell_px.0.max(1) as u32).max(1);
2347    let img_w = img_w.max(1);
2348    let scaled_h = (img_h as u64 * scaled_w as u64 / img_w as u64).max(1) as u32;
2349    (scaled_w, scaled_h)
2350}
2351
2352/// Text rows a fit-width protocol image occupies. `width_cols` overrides the
2353/// terminal `cols` when `Some` (from `--image-width`). Pure; used for scroll math.
2354#[cfg(feature = "image")]
2355pub fn protocol_occupied_rows(img_w: u32, img_h: u32, cols: u16,
2356                              cell_px: (u16, u16), width_cols: Option<usize>) -> usize {
2357    let (_, scaled_h) = protocol_scaled_dims(img_w, img_h, cols, cell_px, width_cols);
2358    (scaled_h as usize).div_ceil(cell_px.1.max(1) as usize).max(1)
2359}
2360
2361#[cfg(test)]
2362mod tests {
2363    use super::*;
2364    use crate::source::MockSource;
2365
2366    fn setup(content: &[u8]) -> (MockSource, LineIndex) {
2367        let m = MockSource::new();
2368        m.append(content);
2369        m.finish();
2370        let idx = LineIndex::new();
2371        (m, idx)
2372    }
2373
2374    /// Read the `ch` of the first cell of a body row (the status column when
2375    /// `-J` is on). Panics if the cell isn't a `Cell::Char`.
2376    fn first_cell_char(row: &[Cell]) -> char {
2377        match row.first() {
2378            Some(Cell::Char { ch, .. }) => *ch,
2379            other => panic!("expected Char in first cell, got {:?}", other),
2380        }
2381    }
2382
2383    #[test]
2384    fn status_column_shows_mark_then_search_glyphs() {
2385        // 3 lines, chop mode (no wrap). Mark line 1 ('a'), search matches
2386        // "bb" on line 2. Line 0 → blank, line 1 → 'a' (mark), line 2 → '*'.
2387        let (m, mut idx) = setup(b"aa\nbb\ncc\n");
2388        let mut v = Viewport::new(20, 5, "f".into()); // body = 4
2389        v.opts.wrap = false;
2390        v.set_status_column(true);
2391        let mut marks = std::collections::HashMap::new();
2392        marks.insert(1usize, 'a');
2393        v.set_status_marks(marks);
2394        v.set_search("cc".into(), SearchDirection::Forward).unwrap();
2395
2396        let frame = v.frame(&m, &mut idx);
2397        assert_eq!(first_cell_char(&frame.body[0]), ' ', "line 0: no mark, no match");
2398        assert_eq!(first_cell_char(&frame.body[1]), 'a', "line 1: mark letter");
2399        assert_eq!(first_cell_char(&frame.body[2]), '*', "line 2: search match");
2400    }
2401
2402    #[test]
2403    fn status_column_mark_beats_search_match() {
2404        // Line 1 is BOTH marked ('z') AND matched by the search — the mark
2405        // letter takes precedence over the search `*`.
2406        let (m, mut idx) = setup(b"aa\nbb\ncc\n");
2407        let mut v = Viewport::new(20, 5, "f".into());
2408        v.opts.wrap = false;
2409        v.set_status_column(true);
2410        let mut marks = std::collections::HashMap::new();
2411        marks.insert(1usize, 'z');
2412        v.set_status_marks(marks);
2413        v.set_search("bb".into(), SearchDirection::Forward).unwrap();
2414
2415        let frame = v.frame(&m, &mut idx);
2416        assert_eq!(first_cell_char(&frame.body[1]), 'z', "mark beats search-match");
2417    }
2418
2419    #[test]
2420    fn status_column_matches_content_not_gutter_digits() {
2421        // Regression: with line numbers (`-N`) on, searching for a digit that
2422        // appears in a visible LINE NUMBER must not falsely flag `*` in the
2423        // status column. The `*` must reflect matches in the CONTENT only,
2424        // never the gutter digits / status cell / padding.
2425        // 12 lines, all letters, no digits in content. Line number 5 contains
2426        // the digit '5' in its gutter; searching "5" must NOT mark that line.
2427        let (m, mut idx) = setup(b"aa\nbb\ncc\ndd\nee\nff\ngg\nhh\nii\njj\nkk\nll\n");
2428        let mut v = Viewport::new(40, 14, "f".into()); // body = 13
2429        v.opts.wrap = false;
2430        v.show_line_numbers = true;
2431        v.set_status_column(true);
2432        v.set_search("5".into(), SearchDirection::Forward).unwrap();
2433
2434        let frame = v.frame(&m, &mut idx);
2435        // body[4] is line 5 (1-based) — its gutter shows "5" but content "ee"
2436        // has no '5'. Status column must be blank, not '*'. (Only the 12 real
2437        // content rows; row 12 is post-EOF filler with an Empty status cell.)
2438        for i in 0..12 {
2439            assert_eq!(
2440                first_cell_char(&frame.body[i]), ' ',
2441                "body row {i}: no content match for '5' but status column flagged it"
2442            );
2443        }
2444
2445        // Positive case: a search matching actual CONTENT still flags `*`.
2446        let (m2, mut idx2) = setup(b"aa\nbb\ncc\ndd\nee\nff\ngg\nhh\nii\njj\nkk\nll\n");
2447        let mut v2 = Viewport::new(40, 14, "f".into());
2448        v2.opts.wrap = false;
2449        v2.show_line_numbers = true;
2450        v2.set_status_column(true);
2451        v2.set_search("ee".into(), SearchDirection::Forward).unwrap();
2452        let frame2 = v2.frame(&m2, &mut idx2);
2453        assert_eq!(first_cell_char(&frame2.body[4]), '*', "line 5 content 'ee' matches search");
2454    }
2455
2456    #[test]
2457    fn status_column_off_leaves_first_cell_as_content() {
2458        // With the feature off, no extra column is prepended — the first cell
2459        // of each body row is the line's content (no line numbers here).
2460        let (m, mut idx) = setup(b"aa\nbb\ncc\n");
2461        let mut v = Viewport::new(20, 5, "f".into());
2462        v.opts.wrap = false;
2463        // status_column defaults off; set a mark + search anyway to prove gating.
2464        let mut marks = std::collections::HashMap::new();
2465        marks.insert(1usize, 'a');
2466        v.set_status_marks(marks);
2467        v.set_search("bb".into(), SearchDirection::Forward).unwrap();
2468
2469        let frame = v.frame(&m, &mut idx);
2470        assert_eq!(first_cell_char(&frame.body[0]), 'a', "line 0 content unchanged");
2471        assert_eq!(first_cell_char(&frame.body[1]), 'b', "line 1 content unchanged");
2472        assert_eq!(first_cell_char(&frame.body[2]), 'c', "line 2 content unchanged");
2473    }
2474
2475    #[test]
2476    fn frame_renders_body_height_rows() {
2477        let (m, mut idx) = setup(b"a\nb\nc\nd\ne\n");
2478        let mut v = Viewport::new(10, 5, "test".into());  // body = 4
2479        let frame = v.frame(&m, &mut idx);
2480        assert_eq!(frame.body.len(), 4);
2481        assert_eq!(frame.body[0][0], Cell::Char { ch: 'a', width: 1, style: crate::ansi::Style::default(), hyperlink: None });
2482        assert_eq!(frame.body[3][0], Cell::Char { ch: 'd', width: 1, style: crate::ansi::Style::default(), hyperlink: None });
2483    }
2484
2485    #[test]
2486    fn scroll_down_advances_top_line() {
2487        // 8 lines, body=4 → there are 4 lines below the first screen to scroll
2488        // into, so scrolling down by 2 lands cleanly above the bottom anchor.
2489        let (m, mut idx) = setup(b"a\nb\nc\nd\ne\nf\ng\nh\n");
2490        let mut v = Viewport::new(10, 5, "test".into());
2491        v.scroll_lines(2, &m, &mut idx);
2492        assert_eq!(v.top_line, 2);
2493        assert_eq!(v.top_row, 0);
2494    }
2495
2496    #[test]
2497    fn scroll_up_clamps_at_zero() {
2498        let (m, mut idx) = setup(b"a\nb\nc\n");
2499        let mut v = Viewport::new(10, 5, "test".into());
2500        v.scroll_lines(-5, &m, &mut idx);
2501        assert_eq!(v.top_line, 0);
2502        assert_eq!(v.top_row, 0);
2503    }
2504
2505    #[test]
2506    fn scroll_down_clamps_at_last_line() {
2507        // 8 single-row lines, body=4. Scrolling far past the end clamps at the
2508        // bottom anchor: the last line resting on the last body row, i.e.
2509        // top_line = 8 - 4 = 4. (Not line 7 at the top — that would strand the
2510        // tail off-screen, the bug this guards against.)
2511        let (m, mut idx) = setup(b"a\nb\nc\nd\ne\nf\ng\nh\n");
2512        let mut v = Viewport::new(10, 5, "test".into());
2513        v.scroll_lines(50, &m, &mut idx);
2514        assert_eq!((v.top_line, v.top_row), (4, 0));
2515        assert!(v.is_at_bottom(&m, &idx));
2516    }
2517
2518    #[test]
2519    fn scroll_logical_lines_skips_wrap_rows() {
2520        // Line 0 has 50 wraps in a 10-col viewport. J should jump straight to line 1.
2521        let mut content = vec![b'X'; 500];
2522        content.push(b'\n');
2523        content.extend_from_slice(b"second\n");
2524        content.extend_from_slice(b"third\n");
2525        let (m, mut idx) = setup(&content);
2526        let mut v = Viewport::new(10, 8, "f".into());
2527        v.scroll_logical_lines(1, &m, &mut idx);
2528        assert_eq!((v.top_line, v.top_row), (1, 0));
2529        v.scroll_logical_lines(1, &m, &mut idx);
2530        assert_eq!((v.top_line, v.top_row), (2, 0));
2531    }
2532
2533    #[test]
2534    fn scroll_logical_lines_back_snaps_to_line_start() {
2535        // Mid-wrap K should snap to start of current line first, then go back.
2536        // Three 50-char lines (5 wrap rows each) with body=8 put the bottom
2537        // anchor at (1, 2), so scrolling down 7 rows lands inside line 1's
2538        // wraps without being clamped.
2539        let mut content = vec![b'A'; 50];
2540        content.push(b'\n');
2541        content.extend_from_slice(&[b'B'; 50]);
2542        content.push(b'\n');
2543        content.extend_from_slice(&[b'C'; 50]);
2544        content.push(b'\n');
2545        let (m, mut idx) = setup(&content);
2546        let mut v = Viewport::new(10, 8, "f".into());
2547        v.scroll_lines(7, &m, &mut idx);
2548        assert_eq!(v.top_line, 1, "should be on line 1");
2549        assert!(v.top_row > 0, "should be inside line 1's wraps");
2550        v.scroll_logical_lines(-1, &m, &mut idx);
2551        assert_eq!((v.top_line, v.top_row), (1, 0), "K snaps to start of current line");
2552        v.scroll_logical_lines(-1, &m, &mut idx);
2553        assert_eq!((v.top_line, v.top_row), (0, 0), "K then goes to previous line");
2554    }
2555
2556    #[test]
2557    fn scroll_down_walks_wraps_of_last_line() {
2558        // Last line is 60 chars in a 10-col viewport → 6 wrap rows. With body=4
2559        // the bottom anchor sits at (1, 2), so walking into the last line's
2560        // wraps up to row 2 is legitimate (it doesn't strand the tail).
2561        let mut content = b"first\n".to_vec();
2562        content.extend_from_slice(&[b'X'; 60]);
2563        content.push(b'\n');
2564        let (m, mut idx) = setup(&content);
2565        let mut v = Viewport::new(10, 5, "f".into());
2566        v.scroll_lines(1, &m, &mut idx);
2567        assert_eq!((v.top_line, v.top_row), (1, 0));
2568        v.scroll_lines(1, &m, &mut idx);
2569        assert_eq!((v.top_line, v.top_row), (1, 1), "should advance into wraps of last line");
2570        v.scroll_lines(1, &m, &mut idx);
2571        assert_eq!((v.top_line, v.top_row), (1, 2), "should reach the bottom anchor row");
2572        // Already at the anchor — scrolling further must not strand the tail.
2573        v.scroll_lines(5, &m, &mut idx);
2574        assert_eq!((v.top_line, v.top_row), (1, 2), "clamped at the bottom anchor");
2575    }
2576
2577    #[test]
2578    fn scroll_down_walks_wrap_rows_within_long_line() {
2579        // Line 0 is 30 chars in a 10-col viewport → 3 wrap rows. Body = 4.
2580        // Six short lines follow so the bottom anchor sits well below line 0,
2581        // leaving room to walk through line 0's wrap rows and into line 1.
2582        let mut content = vec![b'X'; 30];
2583        content.push(b'\n');
2584        content.extend_from_slice(b"a\nb\nc\nd\ne\nf\n");
2585        let (m, mut idx) = setup(&content);
2586        let mut v = Viewport::new(10, 5, "f".into());
2587        v.scroll_lines(1, &m, &mut idx);
2588        assert_eq!((v.top_line, v.top_row), (0, 1), "first j → wrap row 1");
2589        v.scroll_lines(1, &m, &mut idx);
2590        assert_eq!((v.top_line, v.top_row), (0, 2), "second j → wrap row 2");
2591        v.scroll_lines(1, &m, &mut idx);
2592        assert_eq!((v.top_line, v.top_row), (1, 0), "third j → next logical line");
2593    }
2594
2595    #[test]
2596    fn status_line_shows_range_and_pct() {
2597        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
2598        let mut v = Viewport::new(20, 5, "f".into());  // body = 4
2599        let frame = v.frame(&m, &mut idx);
2600        assert!(frame.status.starts_with("f  1-4/10"));
2601    }
2602
2603    #[test]
2604    fn page_down_advances_by_body_rows() {
2605        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n");
2606        let mut v = Viewport::new(10, 5, "f".into());  // body = 4
2607        v.page_down(&m, &mut idx);
2608        assert_eq!(v.top_line, 4);
2609    }
2610
2611    #[test]
2612    fn page_up_then_page_down_returns_to_start_when_no_resize() {
2613        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n");
2614        let mut v = Viewport::new(10, 5, "f".into());
2615        v.page_down(&m, &mut idx);
2616        v.page_up(&m, &mut idx);
2617        assert_eq!(v.top_line, 0);
2618        assert_eq!(v.top_row, 0);
2619    }
2620
2621    #[test]
2622    fn half_page_down_advances_by_half_body() {
2623        // 12 lines, body=6 → bottom anchor at line 6, so a half-page (3) lands
2624        // cleanly above it.
2625        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n");
2626        let mut v = Viewport::new(10, 7, "f".into());  // body = 6, half = 3
2627        v.half_page_down(&m, &mut idx);
2628        assert_eq!(v.top_line, 3);
2629    }
2630
2631    #[test]
2632    fn goto_top_resets_position() {
2633        let (m, mut idx) = setup(b"1\n2\n3\n4\n");
2634        let mut v = Viewport::new(10, 5, "f".into());
2635        v.scroll_lines(2, &m, &mut idx);
2636        v.goto_top();
2637        assert_eq!(v.top_line, 0);
2638        assert_eq!(v.top_row, 0);
2639    }
2640
2641    #[test]
2642    fn goto_bottom_scrolls_to_last_page() {
2643        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
2644        let mut v = Viewport::new(10, 5, "f".into());  // body = 4
2645        v.goto_bottom(&m, &mut idx);
2646        // Last page should show lines 7..=10 → top_line = 6.
2647        assert_eq!(v.top_line, 6);
2648    }
2649
2650    #[test]
2651    #[cfg(feature = "image")]
2652    fn protocol_image_occupied_rows_fit_width() {
2653        // 100x200 image, cols=50, cell_px=(8,16): scaled_w=400, scaled_h=800, rows=50.
2654        assert_eq!(crate::viewport::protocol_occupied_rows(100, 200, 50, (8, 16), None), 50);
2655        // --image-width 25 cols: scaled_w=200, scaled_h=400, rows=25.
2656        assert_eq!(crate::viewport::protocol_occupied_rows(100, 200, 50, (8, 16), Some(25)), 25);
2657    }
2658
2659    #[cfg(feature = "image")]
2660    #[test]
2661    fn frame_image_protocol_sets_image_blob() {
2662        use image::{Rgba, RgbaImage};
2663        let mut vp = Viewport::new(40, 10, "cat.png".into());
2664        let mut idx = LineIndex::new();
2665        let m = MockSource::new();
2666        vp.set_image(RgbaImage::from_pixel(20, 40, Rgba([10, 20, 30, 255])), "png", crate::image_render::AsciiStyle::Ramp, None);
2667        vp.set_image_protocol(crate::viewport::ImageProtocol::Kitty, Some((8, 16)));
2668        let frame = vp.frame(&m, &mut idx);
2669        assert!(frame.image_blob.is_some(), "Kitty protocol frame carries an image blob");
2670        // ASCII protocol → no blob
2671        vp.set_image_protocol(crate::viewport::ImageProtocol::Ascii, None);
2672        let frame2 = vp.frame(&m, &mut idx);
2673        assert!(frame2.image_blob.is_none(), "ASCII protocol frame has no blob");
2674    }
2675
2676    #[cfg(feature = "image")]
2677    #[test]
2678    fn protocol_image_clamps_vertical_scroll() {
2679        use image::{Rgba, RgbaImage};
2680        let mut vp = Viewport::new(40, 10, "cat.png".into()); // body = 9
2681        let mut idx = LineIndex::new();
2682        let m = MockSource::new();
2683        // Tall image so it occupies many rows.
2684        vp.set_image(RgbaImage::from_pixel(20, 2000, Rgba([10, 20, 30, 255])), "png", crate::image_render::AsciiStyle::Ramp, None);
2685        vp.set_image_protocol(crate::viewport::ImageProtocol::Kitty, Some((8, 16)));
2686        // Scroll way past the end; frame() applies the clamp.
2687        for _ in 0..10_000 {
2688            vp.scroll_lines(1, &m, &mut idx);
2689        }
2690        let _ = vp.frame(&m, &mut idx);
2691        // After scrolling past the end, top_line clamps to (protocol_total - body_rows).
2692        let total = crate::viewport::protocol_occupied_rows(20, 2000, 40, (8, 16), None);
2693        let body = vp.body_rows() as usize;
2694        assert_eq!(
2695            vp.top_line(),
2696            total.saturating_sub(body),
2697            "scroll reaches exactly the protocol image bottom"
2698        );
2699    }
2700
2701    #[cfg(feature = "image")]
2702    #[test]
2703    fn image_mode_frame_renders_and_scrolls() {
2704        use image::{Rgba, RgbaImage};
2705        let img = RgbaImage::from_pixel(20, 200, Rgba([255, 255, 255, 255]));
2706        let mut v = Viewport::new(20, 6, "cat.png".into()); // body = 5
2707        v.set_image(img, "png", crate::image_render::AsciiStyle::Ramp, Some(20));
2708        assert!(v.image_mode());
2709        let total = v.image_total_rows();
2710        assert!(total > 5, "tall image should exceed the body");
2711        assert!(!v.is_at_bottom_image(), "starts at top");
2712        let mut idx = LineIndex::new();
2713        let m = MockSource::new();
2714        let frame = v.frame(&m, &mut idx);
2715        assert_eq!(frame.body.len(), 5);
2716        v.goto_bottom(&m, &mut idx);
2717        assert!(v.is_at_bottom_image());
2718    }
2719
2720    #[cfg(feature = "image")]
2721    #[test]
2722    fn frame_image_slices_at_left_col() {
2723        use crate::render::Cell;
2724        use image::{Rgba, RgbaImage};
2725
2726        // Image pixel width = 40, terminal cols = 10.
2727        // set_image with width=Some(40) → image_cols() = 40 → grid rows are 40 cells wide.
2728        // Terminal is only 10 cols wide, so the grid is wider than the viewport.
2729        // Column-varying image so each grid column renders a DISTINCT cell —
2730        // otherwise the slice-advance assertions below would be tautological.
2731        let img = RgbaImage::from_fn(40, 20, |x, _y| Rgba([(x as u8).saturating_mul(6), 0, 0, 255]));
2732        let mut v = Viewport::new(10, 4, "wide.png".into()); // body = 3, cols = 10
2733        v.set_image(img, "png", crate::image_render::AsciiStyle::Ramp, Some(40));
2734        assert!(v.hscroll_active(), "image mode should make hscroll active");
2735
2736        let mut idx = LineIndex::new();
2737        let m = MockSource::new();
2738
2739        // --- pass 1: left_col == 0, frame_image returns grid columns 0..10 ---
2740        assert_eq!(v.left_col(), 0);
2741        let frame0 = v.frame(&m, &mut idx);
2742        assert_eq!(frame0.body.len(), 3, "body should have body_rows rows");
2743        // The grid row at offset 0 (no scroll) should give us exactly cols=10 cells.
2744        assert_eq!(frame0.body[0].len(), 10);
2745        // No '<' or '>' marker cells (images never show scroll markers).
2746        assert!(
2747            !frame0.body[0].iter().any(|c| matches!(c, Cell::Char { ch: '<', .. } | Cell::Char { ch: '>', .. })),
2748            "no scroll marker expected on image frame at left_col=0"
2749        );
2750        // Record the first cell at offset 0 for comparison after scrolling.
2751        let cell_at_col0 = frame0.body[0][0].clone();
2752        let cell_at_col8 = frame0.body[0][8].clone();
2753
2754        // --- pass 2: scroll right (left_col = HSCROLL_STEP = 8) ---
2755        v.hscroll_right_step();
2756        assert_eq!(v.left_col(), 8);
2757        let frame1 = v.frame(&m, &mut idx);
2758        assert_eq!(frame1.body[0].len(), 10);
2759        // No '<' or '>' marker cells on images.
2760        assert!(
2761            !frame1.body[0].iter().any(|c| matches!(c, Cell::Char { ch: '<', .. } | Cell::Char { ch: '>', .. })),
2762            "no scroll marker expected on image frame after hscroll_right_step"
2763        );
2764        // The slice DID advance: frame1.body[0][0] is what was at grid column 8.
2765        assert_eq!(
2766            frame1.body[0][0], cell_at_col8,
2767            "after hscroll_right_step the first visible cell should be grid col 8"
2768        );
2769        // And — because the image varies by column — it must differ from the
2770        // pre-scroll first cell, proving the offset was actually applied (not a
2771        // no-op that the uniform-image version couldn't have detected).
2772        assert_ne!(
2773            frame1.body[0][0], cell_at_col0,
2774            "the scrolled first cell must differ from the unscrolled one"
2775        );
2776    }
2777
2778    #[cfg(feature = "image")]
2779    #[test]
2780    fn animation_renders_current_frame_and_advances() {
2781        use image::{Rgba, RgbaImage};
2782        use std::time::Duration;
2783        let m = MockSource::new();
2784        let mut idx = LineIndex::new();
2785        let mut vp = Viewport::new(40, 10, "x.gif".into());
2786        let frames = vec![
2787            (RgbaImage::from_pixel(4, 4, Rgba([0, 0, 0, 255])), Duration::from_millis(100)),
2788            (RgbaImage::from_pixel(4, 4, Rgba([255, 255, 255, 255])), Duration::from_millis(100)),
2789        ];
2790        vp.set_animation(crate::image_render::Animation { frames, loop_count: None }, "gif",
2791                         crate::image_render::AsciiStyle::Ramp, None);
2792        let f0 = vp.frame(&m, &mut idx);
2793        let changed = vp.tick(Duration::from_millis(120));
2794        assert!(changed, "tick past the frame delay advances");
2795        let f1 = vp.frame(&m, &mut idx);
2796        assert_ne!(format!("{:?}", f0.body), format!("{:?}", f1.body), "frame content changed");
2797        assert!(vp.has_animation());
2798        assert!(vp.anim_deadline().is_some());
2799    }
2800
2801    #[cfg(feature = "image")]
2802    #[test]
2803    fn animation_status_badge_reflects_play_pause() {
2804        use image::{Rgba, RgbaImage};
2805        use std::time::Duration;
2806        let m = MockSource::new();
2807        let mut idx = LineIndex::new();
2808        let mut vp = Viewport::new(40, 10, "x.gif".into());
2809        let frames = vec![
2810            (RgbaImage::from_pixel(4, 4, Rgba([0, 0, 0, 255])), Duration::from_millis(100)),
2811            (RgbaImage::from_pixel(4, 4, Rgba([255, 255, 255, 255])), Duration::from_millis(100)),
2812        ];
2813        vp.set_animation(crate::image_render::Animation { frames, loop_count: None }, "gif",
2814                         crate::image_render::AsciiStyle::Ramp, None);
2815        let playing = vp.frame(&m, &mut idx);
2816        assert!(playing.status.contains("[play 1/2]"), "status: {:?}", playing.status);
2817        vp.anim_toggle_pause();
2818        let paused = vp.frame(&m, &mut idx);
2819        assert!(paused.status.contains("[pause 1/2]"), "status: {:?}", paused.status);
2820    }
2821
2822    #[cfg(feature = "image")]
2823    #[test]
2824    fn animation_pause_stops_advance() {
2825        use image::{Rgba, RgbaImage};
2826        use std::time::Duration;
2827        let mut vp = Viewport::new(40, 10, "x.gif".into());
2828        let frames = vec![
2829            (RgbaImage::from_pixel(4, 4, Rgba([0, 0, 0, 255])), Duration::from_millis(100)),
2830            (RgbaImage::from_pixel(4, 4, Rgba([255, 255, 255, 255])), Duration::from_millis(100)),
2831        ];
2832        vp.set_animation(crate::image_render::Animation { frames, loop_count: None }, "gif",
2833                         crate::image_render::AsciiStyle::Ramp, None);
2834        vp.anim_toggle_pause();
2835        assert!(!vp.tick(Duration::from_millis(500)), "paused tick does not advance");
2836        assert_eq!(vp.anim_deadline(), None);
2837    }
2838
2839    #[test]
2840    fn goto_line_positions_top_line() {
2841        let m = MockSource::new();
2842        m.append(b"a\nb\nc\nd\ne\n");
2843        let mut idx = LineIndex::new();
2844        idx.extend_to_end(&m);
2845        let mut v = Viewport::new(20, 5, "f".into());
2846        v.goto_line(3, &m, &mut idx);
2847        assert_eq!(v.top_line(), 3);
2848    }
2849
2850    #[test]
2851    fn goto_line_clamps_to_last_line() {
2852        let m = MockSource::new();
2853        m.append(b"a\nb\n");
2854        let mut idx = LineIndex::new();
2855        idx.extend_to_end(&m);
2856        let mut v = Viewport::new(20, 5, "f".into());
2857        v.goto_line(999, &m, &mut idx);
2858        assert_eq!(v.top_line(), 1);
2859    }
2860
2861    #[test]
2862    fn goto_record_positions_at_record_start_line() {
2863        let m = MockSource::new();
2864        m.append(b"[1] a\n  cont\n[2] b\n[3] c\n");
2865        let mut idx = LineIndex::new();
2866        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
2867        idx.extend_to_end(&m);
2868        let mut v = Viewport::new(20, 5, "f".into());
2869        v.goto_record(1, &m, &mut idx);  // record 1 starts at line 2 ("[2] b")
2870        assert_eq!(v.top_line(), 2);
2871    }
2872
2873    #[test]
2874    fn goto_record_in_line_per_record_mode_equals_goto_line() {
2875        let m = MockSource::new();
2876        m.append(b"a\nb\nc\n");
2877        let mut idx = LineIndex::new();
2878        idx.extend_to_end(&m);
2879        let mut v = Viewport::new(20, 5, "f".into());
2880        v.goto_record(2, &m, &mut idx);
2881        assert_eq!(v.top_line(), 2);
2882    }
2883
2884    #[test]
2885    fn goto_percent_50_lands_in_middle() {
2886        let m = MockSource::new();
2887        m.append(b"a\nb\nc\nd\ne\n");  // 10 bytes
2888        let mut idx = LineIndex::new();
2889        idx.extend_to_end(&m);
2890        let mut v = Viewport::new(20, 5, "f".into());
2891        v.goto_percent(50, &m, &mut idx);
2892        assert_eq!(v.top_line(), 2);  // byte 5 → line 2
2893    }
2894
2895    #[test]
2896    fn goto_percent_100_lands_at_last_line() {
2897        let m = MockSource::new();
2898        m.append(b"a\nb\nc\n");  // 6 bytes, 3 lines
2899        let mut idx = LineIndex::new();
2900        idx.extend_to_end(&m);
2901        let mut v = Viewport::new(20, 5, "f".into());
2902        v.goto_percent(100, &m, &mut idx);
2903        assert_eq!(v.top_line(), 2);
2904    }
2905
2906    #[test]
2907    fn goto_percent_0_lands_at_first_line() {
2908        let m = MockSource::new();
2909        m.append(b"a\nb\nc\n");
2910        let mut idx = LineIndex::new();
2911        idx.extend_to_end(&m);
2912        let mut v = Viewport::new(20, 5, "f".into());
2913        v.goto_record(2, &m, &mut idx);  // first jump elsewhere
2914        assert_eq!(v.top_line(), 2);
2915        v.goto_percent(0, &m, &mut idx);
2916        assert_eq!(v.top_line(), 0);
2917    }
2918
2919    #[test]
2920    fn resize_updates_dimensions_and_render_opts() {
2921        let (m, mut idx) = setup(b"1\n2\n");
2922        let mut v = Viewport::new(10, 5, "f".into());
2923        v.resize(40, 12);
2924        assert_eq!(v.cols, 40);
2925        assert_eq!(v.rows, 12);
2926        assert_eq!(v.opts.cols, 40);
2927        let _ = v.frame(&m, &mut idx);
2928    }
2929
2930    #[test]
2931    fn toggle_line_numbers_changes_gutter() {
2932        let (m, mut idx) = setup(b"a\nb\nc\n");
2933        let mut v = Viewport::new(10, 5, "f".into());
2934        let frame_off = v.frame(&m, &mut idx);
2935        v.toggle_line_numbers();
2936        let frame_on = v.frame(&m, &mut idx);
2937        // With gutter, first cell is a digit or space, not 'a'.
2938        assert_eq!(frame_off.body[0][0], Cell::Char { ch: 'a', width: 1, style: crate::ansi::Style::default(), hyperlink: None });
2939        assert_ne!(frame_on.body[0][0], Cell::Char { ch: 'a', width: 1, style: crate::ansi::Style::default(), hyperlink: None });
2940    }
2941
2942    #[test]
2943    fn toggle_chop_changes_wrap_mode() {
2944        let (m, mut idx) = setup(b"abcdefghij\n");
2945        let mut v = Viewport::new(4, 5, "f".into());
2946        v.toggle_chop();
2947        let frame = v.frame(&m, &mut idx);
2948        // After toggle_chop, the line is one row, not wrapped.
2949        // Body row 0 is "abcd"; rows 1..3 are blank fill.
2950        assert_eq!(frame.body[0][..4],
2951            [Cell::Char { ch: 'a', width: 1, style: crate::ansi::Style::default(), hyperlink: None },
2952             Cell::Char { ch: 'b', width: 1, style: crate::ansi::Style::default(), hyperlink: None },
2953             Cell::Char { ch: 'c', width: 1, style: crate::ansi::Style::default(), hyperlink: None },
2954             Cell::Char { ch: 'd', width: 1, style: crate::ansi::Style::default(), hyperlink: None }]);
2955        // Row 1 should be all-empty (no wrap continuation).
2956        assert!(frame.body[1].iter().all(|c| matches!(c, Cell::Empty)));
2957    }
2958
2959    // ----- Follow mode -----
2960
2961    #[test]
2962    fn is_at_bottom_initially_only_when_source_fits() {
2963        let (m, mut idx) = setup(b"a\nb\n");  // 2 lines
2964        let v = Viewport::new(10, 5, "f".into());  // body = 4 ≥ 2
2965        idx.extend_to_end(&m);
2966        assert!(v.is_at_bottom(&m, &idx), "small file fits in body, top is at bottom");
2967    }
2968
2969    #[test]
2970    fn is_at_bottom_false_when_top_and_more_lines_below() {
2971        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n");  // 8 lines
2972        let v = Viewport::new(10, 5, "f".into());  // body = 4
2973        idx.extend_to_end(&m);
2974        assert!(!v.is_at_bottom(&m, &idx), "top of 8-line file with body=4 is not at bottom");
2975    }
2976
2977    #[test]
2978    fn is_at_bottom_true_after_goto_bottom() {
2979        let (m, mut idx) = setup(b"1\n2\n3\n4\n5\n6\n7\n8\n");
2980        let mut v = Viewport::new(10, 5, "f".into());
2981        v.goto_bottom(&m, &mut idx);
2982        assert!(v.is_at_bottom(&m, &idx));
2983    }
2984
2985    #[test]
2986    fn status_shows_follow_suffix_when_follow_mode_on() {
2987        let (m, mut idx) = setup(b"a\nb\n");
2988        let mut v = Viewport::new(20, 5, "f".into());
2989        let frame_off = v.frame(&m, &mut idx);
2990        assert!(!frame_off.status.contains("(F)"));
2991        v.set_follow_mode(true);
2992        let frame_on = v.frame(&m, &mut idx);
2993        assert!(frame_on.status.contains("(F)"), "expected (F) in status, got: {}", frame_on.status);
2994    }
2995
2996    #[test]
2997    fn toggle_follow_flips_state() {
2998        let mut v = Viewport::new(10, 5, "f".into());
2999        assert!(!v.follow_mode());
3000        v.toggle_follow();
3001        assert!(v.follow_mode());
3002        v.toggle_follow();
3003        assert!(!v.follow_mode());
3004    }
3005
3006    #[test]
3007    fn idle_indicator_kicks_in_at_threshold() {
3008        let (m, mut idx) = setup(b"a\nb\n");
3009        let mut v = Viewport::new(20, 5, "f".into());
3010        v.set_follow_mode(true);
3011        // 19 idle ticks → still (F).
3012        for _ in 0..19 { v.tick_idle(); }
3013        let f1 = v.frame(&m, &mut idx);
3014        assert!(f1.status.contains("(F)"));
3015        assert!(!f1.status.contains("idle"));
3016        // 20th tick crosses the threshold.
3017        v.tick_idle();
3018        let f2 = v.frame(&m, &mut idx);
3019        assert!(f2.status.contains("(F idle)"), "{}", f2.status);
3020    }
3021
3022    #[test]
3023    fn note_growth_resets_idle() {
3024        let (m, mut idx) = setup(b"a\nb\n");
3025        let mut v = Viewport::new(20, 5, "f".into());
3026        v.set_follow_mode(true);
3027        for _ in 0..25 { v.tick_idle(); }
3028        assert!(v.is_idle());
3029        v.note_growth();
3030        assert!(!v.is_idle());
3031        let f = v.frame(&m, &mut idx);
3032        assert!(!f.status.contains("idle"));
3033    }
3034
3035    #[test]
3036    fn qae_off_never_quits_even_at_bottom() {
3037        let (m, mut idx) = setup(b"a\n");
3038        let mut v = Viewport::new(20, 5, "f".into());
3039        v.set_quit_at_eof(QuitAtEof::Off);
3040        v.goto_bottom(&m, &mut idx);
3041        assert!(!v.note_motion_for_eof(true, &m, &idx));
3042    }
3043
3044    #[test]
3045    fn qae_first_quits_immediately_at_bottom() {
3046        let (m, mut idx) = setup(b"a\n");
3047        let mut v = Viewport::new(20, 5, "f".into());
3048        v.set_quit_at_eof(QuitAtEof::First);
3049        v.goto_bottom(&m, &mut idx);
3050        assert!(v.note_motion_for_eof(true, &m, &idx));
3051    }
3052
3053    #[test]
3054    fn qae_first_only_quits_at_eof_not_mid_file() {
3055        let mut content = Vec::new();
3056        for _ in 0..50 { content.extend_from_slice(b"x\n"); }
3057        let (m, mut idx) = setup(&content);
3058        idx.extend_to_end(&m);  // populate so is_at_bottom can see the 50 lines
3059        let mut v = Viewport::new(20, 5, "f".into());
3060        v.set_quit_at_eof(QuitAtEof::First);
3061        // top_line is 0; with 50 lines and a 5-row body, we're not at bottom.
3062        assert!(!v.is_at_bottom(&m, &idx));
3063        assert!(!v.note_motion_for_eof(true, &m, &idx));
3064    }
3065
3066    #[test]
3067    fn qae_second_quits_on_second_hit() {
3068        let (m, mut idx) = setup(b"a\n");
3069        let mut v = Viewport::new(20, 5, "f".into());
3070        v.set_quit_at_eof(QuitAtEof::Second);
3071        v.goto_bottom(&m, &mut idx);
3072        // 1st forward at EOF: count, don't quit.
3073        assert!(!v.note_motion_for_eof(true, &m, &idx));
3074        // 2nd forward at EOF: quit.
3075        assert!(v.note_motion_for_eof(true, &m, &idx));
3076    }
3077
3078    #[test]
3079    fn squeeze_collapses_consecutive_blanks() {
3080        // Source: a, blank, blank, blank, b.
3081        let (m, mut idx) = setup(b"a\n\n\n\nb\n");
3082        let mut v = Viewport::new(10, 8, "f".into());
3083        v.set_squeeze_blanks(true);
3084        let f = v.frame(&m, &mut idx);
3085        // First non-empty body row chars (trimmed).
3086        let stringify = |row: &Vec<Cell>| -> String {
3087            row.iter().filter_map(|c| match c {
3088                Cell::Char { ch, .. } => Some(*ch),
3089                _ => None,
3090            }).collect::<String>().trim().to_string()
3091        };
3092        let rows: Vec<String> = f.body.iter().map(stringify).collect();
3093        // With squeeze: a, blank, b. Then padding.
3094        assert_eq!(&rows[0], "a");
3095        assert_eq!(&rows[1], "");
3096        assert_eq!(&rows[2], "b");
3097    }
3098
3099    #[test]
3100    fn header_pins_top_rows_when_scrolling() {
3101        // 12 lines, 6-row terminal → body_rows = 5. header=2 pins lines 0,1.
3102        let mut content = Vec::new();
3103        for n in 0..12 { content.extend_from_slice(format!("line{n}\n").as_bytes()); }
3104        let (m, mut idx) = setup(&content);
3105        let mut v = Viewport::new(20, 6, "f".into());
3106        v.set_header(2, 0);
3107        // set_header floors top_line at header_lines, so we start showing
3108        // line2 in the scroll window. Scrolling down 5 advances by 5
3109        // logical lines from there.
3110        v.scroll_lines(5, &m, &mut idx);
3111        let f = v.frame(&m, &mut idx);
3112        let chs = |row: &Vec<Cell>| -> String {
3113            row.iter().filter_map(|c| match c {
3114                Cell::Char { ch, .. } => Some(*ch),
3115                _ => None,
3116            }).collect::<String>().trim().to_string()
3117        };
3118        // Rows 0 and 1 are the pinned header (line0, line1) regardless of scroll.
3119        assert_eq!(&chs(&f.body[0]), "line0");
3120        assert_eq!(&chs(&f.body[1]), "line1");
3121        // top_line is now 2 + 5 = 7; row 2 shows line7.
3122        assert_eq!(&chs(&f.body[2]), "line7");
3123    }
3124
3125    #[test]
3126    fn page_size_when_set_overrides_body_rows() {
3127        let mut content = Vec::new();
3128        for n in 0..100 { content.extend_from_slice(format!("L{n}\n").as_bytes()); }
3129        let (m, mut idx) = setup(&content);
3130        let mut v = Viewport::new(20, 10, "f".into());
3131        v.set_page_size(Some(3));
3132        let before = v.top_line();
3133        v.page_down(&m, &mut idx);
3134        assert_eq!(v.top_line(), before + 3);
3135        v.page_up(&m, &mut idx);
3136        assert_eq!(v.top_line(), before);
3137    }
3138
3139    #[test]
3140    fn page_size_unset_uses_body_rows() {
3141        let mut content = Vec::new();
3142        for n in 0..100 { content.extend_from_slice(format!("L{n}\n").as_bytes()); }
3143        let (m, mut idx) = setup(&content);
3144        let mut v = Viewport::new(20, 10, "f".into());
3145        // body_rows = rows - 1 = 9.
3146        v.page_down(&m, &mut idx);
3147        assert_eq!(v.top_line(), 9);
3148    }
3149
3150    #[test]
3151    fn header_zero_lines_renders_like_no_header() {
3152        let mut content = Vec::new();
3153        for n in 0..10 { content.extend_from_slice(format!("line{n}\n").as_bytes()); }
3154        let (m, mut idx) = setup(&content);
3155        let mut v = Viewport::new(20, 6, "f".into());
3156        v.set_header(0, 0);
3157        let f = v.frame(&m, &mut idx);
3158        let chs = |row: &Vec<Cell>| -> String {
3159            row.iter().filter_map(|c| match c {
3160                Cell::Char { ch, .. } => Some(*ch),
3161                _ => None,
3162            }).collect::<String>().trim().to_string()
3163        };
3164        assert_eq!(&chs(&f.body[0]), "line0");
3165        assert_eq!(&chs(&f.body[1]), "line1");
3166    }
3167
3168    #[test]
3169    fn squeeze_off_preserves_blanks() {
3170        let (m, mut idx) = setup(b"a\n\n\n\nb\n");
3171        let mut v = Viewport::new(10, 8, "f".into());
3172        // Default is off.
3173        let f = v.frame(&m, &mut idx);
3174        let stringify = |row: &Vec<Cell>| -> String {
3175            row.iter().filter_map(|c| match c {
3176                Cell::Char { ch, .. } => Some(*ch),
3177                _ => None,
3178            }).collect::<String>().trim().to_string()
3179        };
3180        let rows: Vec<String> = f.body.iter().map(stringify).collect();
3181        // Without squeeze: a, blank, blank, blank, b.
3182        assert_eq!(&rows[0], "a");
3183        assert_eq!(&rows[1], "");
3184        assert_eq!(&rows[2], "");
3185        assert_eq!(&rows[3], "");
3186        assert_eq!(&rows[4], "b");
3187    }
3188
3189    #[test]
3190    fn qae_second_resets_on_backward_motion() {
3191        let (m, mut idx) = setup(b"a\n");
3192        let mut v = Viewport::new(20, 5, "f".into());
3193        v.set_quit_at_eof(QuitAtEof::Second);
3194        v.goto_bottom(&m, &mut idx);
3195        assert!(!v.note_motion_for_eof(true, &m, &idx));
3196        // Backward motion clears the counter.
3197        v.note_motion_for_eof(false, &m, &idx);
3198        // Next forward starts fresh: counts, doesn't quit.
3199        assert!(!v.note_motion_for_eof(true, &m, &idx));
3200        // Now the second consecutive forward triggers quit.
3201        assert!(v.note_motion_for_eof(true, &m, &idx));
3202    }
3203
3204    #[test]
3205    fn flash_message_overrides_follow_suffix() {
3206        let (m, mut idx) = setup(b"a\nb\n");
3207        let mut v = Viewport::new(40, 5, "f".into());
3208        v.set_follow_mode(true);
3209        v.flash("(F reopened)", 3);
3210        let f = v.frame(&m, &mut idx);
3211        assert!(f.status.contains("(F reopened)"), "{}", f.status);
3212        assert!(!f.status.contains("(F idle)"));
3213    }
3214
3215    #[test]
3216    fn flash_countdown_clears() {
3217        let mut v = Viewport::new(10, 5, "f".into());
3218        v.flash("hello", 2);
3219        v.tick_flash();
3220        assert!(v.status_flash.is_some());
3221        v.tick_flash();
3222        assert!(v.status_flash.is_none());
3223    }
3224
3225    #[test]
3226    fn suspend_follow_if_off_is_noop() {
3227        let mut v = Viewport::new(10, 5, "f".into());
3228        v.set_follow_mode(true);
3229        v.suspend_follow_if(false);
3230        assert!(v.follow_mode());
3231    }
3232
3233    #[test]
3234    fn suspend_follow_if_on_flips_off() {
3235        let mut v = Viewport::new(10, 5, "f".into());
3236        v.set_follow_mode(true);
3237        v.suspend_follow_if(true);
3238        assert!(!v.follow_mode());
3239    }
3240
3241    #[test]
3242    fn case_mode_sensitive_returns_pattern_unchanged() {
3243        assert_eq!(CaseMode::Sensitive.apply_to_pattern("foo"), "foo");
3244        assert_eq!(CaseMode::Sensitive.apply_to_pattern("FOO"), "FOO");
3245    }
3246
3247    #[test]
3248    fn case_mode_insensitive_prepends_i_flag() {
3249        assert_eq!(CaseMode::Insensitive.apply_to_pattern("foo"), "(?i)foo");
3250        assert_eq!(CaseMode::Insensitive.apply_to_pattern("FOO"), "(?i)FOO");
3251    }
3252
3253    #[test]
3254    fn case_mode_smart_lowercase_is_insensitive() {
3255        assert_eq!(CaseMode::Smart.apply_to_pattern("foo"), "(?i)foo");
3256    }
3257
3258    #[test]
3259    fn case_mode_smart_with_uppercase_is_sensitive() {
3260        assert_eq!(CaseMode::Smart.apply_to_pattern("Foo"), "Foo");
3261        assert_eq!(CaseMode::Smart.apply_to_pattern("FOO"), "FOO");
3262    }
3263
3264    #[test]
3265    fn set_case_mode_recompiles_active_search() {
3266        let (m, mut idx) = setup(b"hello WORLD\n");
3267        let mut v = Viewport::new(40, 5, "f".into());
3268        v.set_search("world".into(), SearchDirection::Forward).unwrap();
3269        // Sensitive: no match for lowercase against WORLD.
3270        assert!(!v.search_repeat(&m, &mut idx, false));
3271        // Switch to insensitive — should re-compile and now match.
3272        v.set_case_mode(CaseMode::Insensitive);
3273        assert!(v.search_repeat(&m, &mut idx, false));
3274    }
3275
3276    #[test]
3277    fn status_shows_prettify_label_when_set() {
3278        let (m, mut idx) = setup(b"a\n");
3279        let mut v = Viewport::new(40, 5, "f".into());
3280        let frame_off = v.frame(&m, &mut idx);
3281        assert!(!frame_off.status.contains("[pretty"));
3282        v.set_prettify_label(Some("json".into()));
3283        let frame_on = v.frame(&m, &mut idx);
3284        assert!(frame_on.status.contains("[pretty:json]"),
3285            "expected [pretty:json] in status, got: {}", frame_on.status);
3286        v.set_prettify_label(Some("json:err".into()));
3287        let frame_err = v.frame(&m, &mut idx);
3288        assert!(frame_err.status.contains("[pretty:json:err]"),
3289            "expected [pretty:json:err] in status, got: {}", frame_err.status);
3290    }
3291
3292    #[test]
3293    fn status_shows_l_suffix_when_live_mode_on() {
3294        let (m, mut idx) = setup(b"a\nb\n");
3295        let mut v = Viewport::new(20, 5, "f".into());
3296        let frame_off = v.frame(&m, &mut idx);
3297        assert!(!frame_off.status.contains("(L)"));
3298        v.set_live_mode(true);
3299        let frame_on = v.frame(&m, &mut idx);
3300        assert!(frame_on.status.contains("(L)"), "expected (L) in status, got: {}", frame_on.status);
3301    }
3302
3303    #[test]
3304    fn clamp_top_line_pulls_back_when_total_shrinks() {
3305        let mut v = Viewport::new(20, 5, "f".into());
3306        // Pretend we were on line 100, then a rewrite leaves only 10 lines.
3307        v.scroll_lines(0, &MockSource::new(), &mut LineIndex::new()); // no-op, just to satisfy
3308        // Force top_line via a sequence; easiest: just call clamp directly.
3309        // We can't poke private state, but clamp works regardless of how we got there.
3310        v.clamp_top_line(100);  // total bigger than top_line=0, no change
3311        v.clamp_top_line(0);    // empty source: must reset
3312        // After clamp(0), line 0 is the floor.
3313        // (No public getter for top_line; we verify indirectly by going to top.)
3314        v.goto_top();
3315        // Just confirm no panic and no overflow on subsequent frame composition.
3316        let (m, mut idx) = setup(b"only\n");
3317        let _ = v.frame(&m, &mut idx);
3318    }
3319
3320    /// Simulates the app::run timeout-branch logic to verify auto-scroll engages
3321    /// when follow mode is on and the viewport is at the bottom.
3322    fn simulate_growth_tick(
3323        v: &mut Viewport,
3324        src: &MockSource,
3325        idx: &mut LineIndex,
3326    ) {
3327        if !v.follow_mode() { return; }
3328        let was_at_bottom = v.is_at_bottom(src, idx);
3329        let lines_before = idx.line_count();
3330        idx.notice_new_bytes(src);
3331        if idx.line_count() != lines_before && was_at_bottom {
3332            v.goto_bottom(src, idx);
3333        }
3334    }
3335
3336    #[test]
3337    fn auto_scroll_engages_when_at_bottom() {
3338        let m = MockSource::new();
3339        m.append(b"1\n2\n3\n4\n");  // 4 lines, body=4 fits
3340        let mut idx = LineIndex::new();
3341        let mut v = Viewport::new(10, 5, "f".into());
3342        v.set_follow_mode(true);
3343        idx.extend_to_end(&m);
3344        assert!(v.is_at_bottom(&m, &idx));
3345        let top_before = {
3346            let f = v.frame(&m, &mut idx);
3347            f.status.clone()  // unused, just exercise frame
3348        };
3349        let _ = top_before;
3350        // Simulate growth: source gains 4 more lines.
3351        m.append(b"5\n6\n7\n8\n");
3352        simulate_growth_tick(&mut v, &m, &mut idx);
3353        // After auto-scroll, top_line should have advanced so the new last line is in view.
3354        assert!(v.is_at_bottom(&m, &idx), "after auto-scroll, viewport should still be at bottom");
3355        let frame = v.frame(&m, &mut idx);
3356        // The bottom-most body row should now contain the last logical line ('8').
3357        // Find which row has '8'.
3358        let last_row = &frame.body[frame.body.len() - 1];
3359        assert_eq!(last_row[0], Cell::Char { ch: '8', width: 1, style: crate::ansi::Style::default(), hyperlink: None });
3360    }
3361
3362    #[test]
3363    fn auto_scroll_suppressed_when_scrolled_up() {
3364        let m = MockSource::new();
3365        m.append(b"1\n2\n3\n4\n5\n6\n7\n8\n");  // 8 lines
3366        let mut idx = LineIndex::new();
3367        let mut v = Viewport::new(10, 5, "f".into());  // body=4
3368        v.set_follow_mode(true);
3369        idx.extend_to_end(&m);
3370        v.goto_bottom(&m, &mut idx);
3371        // Now scroll up off the bottom.
3372        v.scroll_lines(-2, &m, &mut idx);
3373        assert!(!v.is_at_bottom(&m, &idx));
3374        let frame_before = v.frame(&m, &mut idx);
3375        let top_first_cell_before = frame_before.body[0][0].clone();
3376        // Simulate growth.
3377        m.append(b"9\n10\n");
3378        simulate_growth_tick(&mut v, &m, &mut idx);
3379        // Viewport should NOT have moved (auto-scroll suppressed).
3380        let frame_after = v.frame(&m, &mut idx);
3381        assert_eq!(frame_after.body[0][0], top_first_cell_before, "viewport moved despite being scrolled up");
3382    }
3383
3384    // ----- Search -----
3385
3386    #[test]
3387    fn set_search_compiles_regex() {
3388        let mut v = Viewport::new(10, 5, "f".into());
3389        assert!(v.set_search("foo".into(), SearchDirection::Forward).is_ok());
3390        assert!(v.search_active());
3391    }
3392
3393    #[test]
3394    fn set_search_rejects_bad_regex() {
3395        let mut v = Viewport::new(10, 5, "f".into());
3396        let err = v.set_search("[".into(), SearchDirection::Forward).unwrap_err();
3397        assert!(!err.is_empty());
3398        assert!(!v.search_active(), "no search should be set on error");
3399    }
3400
3401    #[test]
3402    fn search_step_forward_finds_match_after_top() {
3403        let (m, mut idx) = setup(b"alpha\nbeta\ngamma\ndelta\nepsilon\n");
3404        let mut v = Viewport::new(20, 5, "f".into());
3405        v.set_search("gamma".into(), SearchDirection::Forward).unwrap();
3406        let found = v.search_repeat(&m, &mut idx, false);
3407        assert!(found);
3408        // gamma is line 2 (0-indexed)
3409        assert_eq!(v.top_line, 2);
3410    }
3411
3412    #[test]
3413    fn search_step_backward_finds_match_before_top() {
3414        let (m, mut idx) = setup(b"alpha\nbeta\ngamma\ndelta\nepsilon\n");
3415        let mut v = Viewport::new(20, 5, "f".into());
3416        v.scroll_lines(4, &m, &mut idx); // top_line = 4
3417        v.set_search("alpha".into(), SearchDirection::Backward).unwrap();
3418        let found = v.search_repeat(&m, &mut idx, false);
3419        assert!(found);
3420        assert_eq!(v.top_line, 0);
3421    }
3422
3423    #[test]
3424    fn search_wraps_at_end() {
3425        let (m, mut idx) = setup(b"alpha\nbeta\ngamma\n");
3426        let mut v = Viewport::new(20, 5, "f".into());
3427        v.scroll_lines(2, &m, &mut idx); // top_line = 2 (last line)
3428        v.set_search("alpha".into(), SearchDirection::Forward).unwrap();
3429        let found = v.search_repeat(&m, &mut idx, false);
3430        assert!(found, "search should wrap forward past EOF");
3431        assert_eq!(v.top_line, 0);
3432    }
3433
3434    #[test]
3435    fn search_no_match_returns_false_and_does_not_move() {
3436        let (m, mut idx) = setup(b"alpha\nbeta\ngamma\n");
3437        let mut v = Viewport::new(20, 5, "f".into());
3438        v.set_search("nowhere".into(), SearchDirection::Forward).unwrap();
3439        let found = v.search_repeat(&m, &mut idx, false);
3440        assert!(!found);
3441        assert_eq!(v.top_line, 0);
3442    }
3443
3444    #[test]
3445    fn frame_records_highlight_ranges_for_matches() {
3446        let (m, mut idx) = setup(b"alpha\nbeta\ngamma\ndelta\n");
3447        let mut v = Viewport::new(20, 5, "f".into());
3448        v.set_search("gamma".into(), SearchDirection::Forward).unwrap();
3449        let frame = v.frame(&m, &mut idx);
3450        // Body has 4 rows; row 2 is "gamma" (5 chars at columns 0..5).
3451        assert_eq!(frame.row_styles[0], RowStyle::Normal);
3452        assert!(frame.highlights[0].is_empty());
3453        assert!(frame.highlights[1].is_empty());
3454        assert_eq!(frame.highlights[2], vec![0..5]);
3455        assert!(frame.highlights[3].is_empty());
3456    }
3457
3458    #[test]
3459    fn frame_highlights_substring_inside_a_row() {
3460        let (m, mut idx) = setup(b"the alpha and the beta\nfoo\n");
3461        let mut v = Viewport::new(40, 5, "f".into());
3462        v.set_search("beta".into(), SearchDirection::Forward).unwrap();
3463        let frame = v.frame(&m, &mut idx);
3464        // "beta" starts at column 18 in the first row.
3465        assert_eq!(frame.highlights[0], vec![18..22]);
3466        assert!(frame.highlights[1].is_empty());
3467    }
3468
3469    #[test]
3470    fn search_highlight_with_filter_dim_keeps_row_dim() {
3471        // alpha matches filter → Normal. beta doesn't → Dim. Search for
3472        // "beta" should leave row style Dim and mark the substring 0..4.
3473        let (m, mut idx) = setup(b"alpha\nbeta\n");
3474        let mut v = Viewport::new(20, 5, "f".into());
3475        let fmt = crate::format::LogFormat::compile(
3476            "simple",
3477            r"^(?P<line>.+)$",
3478        )
3479        .unwrap();
3480        let f = crate::filter::CompiledFilter::compile(
3481            &fmt,
3482            vec![crate::filter::FilterSpec::parse("line=alpha").unwrap()],
3483            CaseMode::Sensitive,
3484        )
3485        .unwrap();
3486        v.set_filter(Some(f));
3487        v.set_dim_mode(true);
3488        v.set_search("beta".into(), SearchDirection::Forward).unwrap();
3489        let frame = v.frame(&m, &mut idx);
3490        assert_eq!(frame.row_styles[0], RowStyle::Normal);
3491        assert_eq!(frame.row_styles[1], RowStyle::Dim);
3492        assert_eq!(frame.highlights[1], vec![0..4]);
3493    }
3494
3495    #[test]
3496    fn grep_only_hides_non_matching_lines() {
3497        use crate::grep::GrepPredicate;
3498        let src = crate::source::MockSource::new();
3499        src.append(b"keep this error\n");
3500        src.append(b"drop this one\n");
3501        src.append(b"another error line\n");
3502        src.finish();
3503        let mut idx = crate::line_index::LineIndex::new();
3504        idx.extend_to_end(&src);
3505
3506        let mut v = Viewport::new(40, 5, "test".into());
3507        v.set_grep(Some(GrepPredicate::compile(&["error".to_string()], crate::viewport::CaseMode::Sensitive).unwrap()));
3508        v.extend_visible_lines(&idx, &src);
3509
3510        // Only the two "error" lines should be visible.
3511        let frame = v.frame(&src, &mut idx);
3512        let body_text: Vec<String> = frame.body.iter()
3513            .map(|row| row.iter().filter_map(|c| match c {
3514                crate::render::Cell::Char { ch, .. } => Some(*ch),
3515                _ => None,
3516            }).collect())
3517            .collect();
3518        assert!(body_text[0].contains("keep this error"));
3519        assert!(body_text[1].contains("another error line"));
3520        assert!(frame.status.contains("[grep]"));
3521    }
3522
3523    #[test]
3524    fn incsearch_preview_anchors_from_origin_not_previous_match() {
3525        // The valuable invariant: every preview restarts its scan from
3526        // `origin`, never from where the previous preview landed. We prove
3527        // this by previewing a far-below match, then previewing a second
3528        // pattern that has matches BOTH above and below the first match.
3529        // Anchoring from origin must pick the earlier (above) match; a scan
3530        // that continued forward from the previous match would pick the
3531        // later (below) one instead.
3532        let src = crate::source::MockSource::new();
3533        src.append(b"zero\n");    // line 0
3534        src.append(b"one\n");     // line 1
3535        src.append(b"origin\n");  // line 2 — the search origin (non-(0,0))
3536        src.append(b"three\n");   // line 3
3537        src.append(b"mark\n");    // line 4 — second pattern, ABOVE first match
3538        src.append(b"five\n");    // line 5
3539        src.append(b"six\n");     // line 6
3540        src.append(b"seven\n");   // line 7
3541        src.append(b"target\n");  // line 8 — first pattern's only match (below)
3542        src.append(b"mark\n");    // line 9 — second pattern, BELOW first match
3543        src.finish();
3544        let mut idx = crate::line_index::LineIndex::new();
3545
3546        let origin = (2usize, 0usize);
3547        let mut vp = Viewport::new(20, 4, "test".into()); // body = 3
3548        vp.set_top(origin.0, origin.1);
3549        assert_eq!(vp.top_line(), 2);
3550
3551        // Step 1: preview a match well below origin — jumps onto it.
3552        vp.incsearch_preview(&src, &mut idx, "target", SearchDirection::Forward, origin);
3553        assert_eq!(vp.top_line(), 8, "should land on the far-below match");
3554        assert_eq!(vp.top_row(), 0);
3555
3556        // Step 2: from the SAME origin, preview a pattern whose matches are at
3557        // line 4 (above the first match) and line 9 (below it). Because the
3558        // preview re-anchors at origin (line 2) before scanning forward, it
3559        // must land on line 4 — not line 9, which is what a scan continuing
3560        // forward from the previous match (line 8) would have found first.
3561        vp.incsearch_preview(&src, &mut idx, "mark", SearchDirection::Forward, origin);
3562        assert_eq!(
3563            vp.top_line(), 4,
3564            "preview must reset to origin before scanning, landing on the match \
3565             after origin rather than continuing forward from the previous match"
3566        );
3567        assert_eq!(vp.top_row(), 0);
3568    }
3569
3570    #[test]
3571    fn incsearch_preview_empty_or_invalid_is_noop() {
3572        let (src, mut idx) = setup(b"alpha\nbeta\n[unbalanced\n");
3573        let mut vp = Viewport::new(20, 4, "test".into());
3574        vp.set_top(1, 0);
3575        // Empty pattern: no-op, position unchanged.
3576        vp.incsearch_preview(&src, &mut idx, "", SearchDirection::Forward, (0, 0));
3577        assert_eq!(vp.top_line(), 1);
3578        // Invalid regex: silent, position reset to origin but no jump/panic.
3579        vp.incsearch_preview(&src, &mut idx, "(", SearchDirection::Forward, (0, 0));
3580        assert_eq!(vp.top_line(), 0);
3581    }
3582
3583    #[test]
3584    fn filter_and_grep_combine_with_and() {
3585        use crate::grep::GrepPredicate;
3586        let fmt = crate::format::LogFormat::compile(
3587            "simple",
3588            r"^(?P<level>\w+) (?P<msg>.+)$",
3589        ).unwrap();
3590        let f = crate::filter::CompiledFilter::compile(
3591            &fmt,
3592            vec![crate::filter::FilterSpec::parse("level=ERROR").unwrap()],
3593            CaseMode::Sensitive,
3594        ).unwrap();
3595        let g = GrepPredicate::compile(&["timeout".to_string()], crate::viewport::CaseMode::Sensitive).unwrap();
3596
3597        let src = crate::source::MockSource::new();
3598        src.append(b"ERROR timeout connecting\n");      // matches both → keep
3599        src.append(b"ERROR file not found\n");          // matches filter only → drop
3600        src.append(b"WARN timeout retrying\n");         // matches grep only → drop
3601        src.append(b"INFO all good\n");                 // matches neither → drop
3602        src.finish();
3603        let mut idx = crate::line_index::LineIndex::new();
3604        idx.extend_to_end(&src);
3605
3606        let mut v = Viewport::new(80, 5, "test".into());
3607        v.set_filter(Some(f));
3608        v.set_grep(Some(g));
3609        v.extend_visible_lines(&idx, &src);
3610        assert_eq!(v.visible_lines(), &[0usize]);
3611    }
3612
3613    #[test]
3614    fn search_status_shows_pattern() {
3615        let (m, mut idx) = setup(b"x\n");
3616        let mut v = Viewport::new(20, 5, "f".into());
3617        v.set_search("foo".into(), SearchDirection::Forward).unwrap();
3618        let frame = v.frame(&m, &mut idx);
3619        assert!(frame.status.contains("[/foo]"), "status: {}", frame.status);
3620    }
3621
3622    #[test]
3623    fn repeat_search_after_first_match_advances() {
3624        let (m, mut idx) = setup(b"alpha\nfoo one\nbeta\nfoo two\ngamma\nfoo three\n");
3625        let mut v = Viewport::new(40, 5, "f".into());
3626        v.set_search("foo".into(), SearchDirection::Forward).unwrap();
3627        assert!(v.search_repeat(&m, &mut idx, false));
3628        assert_eq!(v.top_line, 1, "first foo");
3629        v.set_search("foo".into(), SearchDirection::Forward).unwrap();
3630        assert!(v.search_repeat(&m, &mut idx, false), "second search should still match");
3631        assert_eq!(v.top_line, 3, "should advance to next foo");
3632    }
3633
3634    #[test]
3635    fn auto_scroll_paused_when_follow_off() {
3636        let m = MockSource::new();
3637        m.append(b"1\n2\n3\n4\n");
3638        let mut idx = LineIndex::new();
3639        let mut v = Viewport::new(10, 5, "f".into());
3640        // Follow is off; viewport at top.
3641        idx.extend_to_end(&m);
3642        let frame_before = v.frame(&m, &mut idx);
3643        let top_first_cell = frame_before.body[0][0].clone();
3644        m.append(b"5\n6\n7\n8\n");
3645        simulate_growth_tick(&mut v, &m, &mut idx);
3646        let frame_after = v.frame(&m, &mut idx);
3647        assert_eq!(frame_after.body[0][0], top_first_cell, "auto-scroll fired despite follow off");
3648    }
3649
3650    // ----- Records-mode search -----
3651
3652    #[test]
3653    fn search_jumps_to_next_matching_record() {
3654        let m = MockSource::new();
3655        m.append(b"[1] alpha\n  cont\n[2] bravo\n[3] charlie\n  cont\n[4] delta\n");
3656        let mut idx = LineIndex::new();
3657        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3658        idx.extend_to_end(&m);
3659        let mut v = Viewport::new(40, 10, "f".into());
3660        v.set_search("charlie".into(), SearchDirection::Forward).unwrap();
3661        let hit = v.search_repeat(&m, &mut idx, false);
3662        assert!(hit, "should find 'charlie' in record 2");
3663        assert_eq!(v.top_line(), 3);  // record 2 starts at line 3 ("[3] charlie")
3664    }
3665
3666    #[test]
3667    fn search_finds_cross_line_match_in_record_with_s_flag() {
3668        let m = MockSource::new();
3669        m.append(b"[1] head\n  Renderer.php(214)\n[2] other line\n");
3670        let mut idx = LineIndex::new();
3671        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3672        idx.extend_to_end(&m);
3673        let mut v = Viewport::new(40, 10, "f".into());
3674        v.set_search(r"(?s)head.*Renderer".into(), SearchDirection::Forward).unwrap();
3675        let hit = v.search_repeat(&m, &mut idx, false);
3676        assert!(hit, "should match across \\n inside record 0 with (?s)");
3677        assert_eq!(v.top_line(), 0);
3678    }
3679
3680    #[test]
3681    fn search_repeat_with_no_match_returns_false() {
3682        let m = MockSource::new();
3683        m.append(b"[1] alpha\n[2] bravo\n");
3684        let mut idx = LineIndex::new();
3685        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3686        idx.extend_to_end(&m);
3687        let mut v = Viewport::new(40, 10, "f".into());
3688        v.set_search("nonexistent".into(), SearchDirection::Forward).unwrap();
3689        let hit = v.search_repeat(&m, &mut idx, false);
3690        assert!(!hit);
3691    }
3692
3693    // ----- Records-mode filter/grep -----
3694
3695    #[test]
3696    fn filter_hide_mode_drops_all_lines_of_nonmatching_record() {
3697        // Record 0: "[1] head\n  cont a" — grep matches "cont a" → visible.
3698        // Record 1: "[2] head\n  cont b" — grep does NOT match → hidden.
3699        let m = MockSource::new();
3700        m.append(b"[1] head\n  cont a\n[2] head\n  cont b\n");
3701        let mut idx = LineIndex::new();
3702        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3703        idx.extend_to_end(&m);
3704        let grep = GrepPredicate::compile(&["cont a".to_string()], crate::viewport::CaseMode::Sensitive).unwrap();
3705        let mut v = Viewport::new(40, 10, "f".into());
3706        v.set_grep(Some(grep));
3707        v.extend_visible_lines(&idx, &m);
3708        // Record 0 ([1] head + cont a) matches; lines 0 and 1 visible.
3709        // Record 1 ([2] head + cont b) does not match; lines 2 and 3 hidden.
3710        assert_eq!(v.visible_lines(), &[0usize, 1]);
3711    }
3712
3713    #[test]
3714    fn filter_in_records_mode_keeps_whole_record_when_header_matches() {
3715        // The format regex is designed for the header line (it ends with `$`).
3716        // Applied to the full multi-line record bytes it would never match
3717        // because `$` doesn't match before a non-final `\n`. Records-mode
3718        // filter must evaluate against the first line of the record, then
3719        // include all of the record's lines when it matches.
3720        let m = MockSource::new();
3721        m.append(
3722            b"[1] kind=category\n  body a\n  body a2\n[2] kind=rule\n  body b\n",
3723        );
3724        let mut idx = LineIndex::new();
3725        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3726        idx.extend_to_end(&m);
3727        let fmt = crate::format::LogFormat::compile(
3728            "rec",
3729            r"^\[(?P<id>\d+)\] kind=(?P<kind>.+)$",
3730        )
3731        .unwrap();
3732        let f = crate::filter::CompiledFilter::compile(
3733            &fmt,
3734            vec![crate::filter::FilterSpec::parse("kind~category").unwrap()],
3735            CaseMode::Sensitive,
3736        )
3737        .unwrap();
3738        let mut v = Viewport::new(40, 10, "f".into());
3739        v.set_filter(Some(f));
3740        v.extend_visible_lines(&idx, &m);
3741        // Record 0 (lines 0, 1, 2) matches; record 1 (lines 3, 4) does not.
3742        assert_eq!(v.visible_lines(), &[0usize, 1, 2]);
3743    }
3744
3745    #[test]
3746    fn grep_matches_across_record_newlines_in_records_mode() {
3747        // Pattern spans the record-header and a continuation line (needs (?s) for .).
3748        let m = MockSource::new();
3749        m.append(b"[1] head\n  Renderer.php\n[2] other\n  body\n");
3750        let mut idx = LineIndex::new();
3751        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3752        idx.extend_to_end(&m);
3753        let grep = GrepPredicate::compile(&[r"(?s)head.*Renderer".to_string()], crate::viewport::CaseMode::Sensitive).unwrap();
3754        let mut v = Viewport::new(40, 10, "f".into());
3755        v.set_grep(Some(grep));
3756        v.extend_visible_lines(&idx, &m);
3757        // Record 0 matches (cross-line); record 1 does not.
3758        assert_eq!(v.visible_lines(), &[0usize, 1]);
3759    }
3760
3761    #[test]
3762    fn dim_mode_keeps_all_lines_visible_dims_nonmatching_records() {
3763        // All 4 lines stay in visible_lines (dim mode = no hiding).
3764        // Record 0 matches grep → Normal; record 1 does not → Dim.
3765        let m = MockSource::new();
3766        m.append(b"[1] head\n  cont\n[2] other\n  cont\n");
3767        let mut idx = LineIndex::new();
3768        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3769        idx.extend_to_end(&m);
3770        let grep = GrepPredicate::compile(&[r"\[1\]".to_string()], CaseMode::Sensitive).unwrap();
3771        let mut v = Viewport::new(40, 10, "f".into());
3772        v.set_grep(Some(grep));
3773        v.set_dim_mode(true);
3774        v.extend_visible_lines(&idx, &m);
3775        // Dim mode: visible_lines stays empty (hide_mode() is false).
3776        assert_eq!(v.visible_lines(), &[] as &[usize]);
3777        // Dim decision is per record: lines 0 and 1 belong to matching record → Normal.
3778        assert!(!v.should_dim_line(0, &idx, &m));
3779        assert!(!v.should_dim_line(1, &idx, &m));
3780        // Lines 2 and 3 belong to non-matching record → Dim.
3781        assert!(v.should_dim_line(2, &idx, &m));
3782        assert!(v.should_dim_line(3, &idx, &m));
3783    }
3784
3785    #[test]
3786    fn status_unchanged_when_records_inactive() {
3787        let (m, mut idx) = setup(b"a\nb\nc\n");
3788        let mut v = Viewport::new(20, 5, "f".into());
3789        let frame = v.frame(&m, &mut idx);
3790        let status = &frame.status;
3791        // Default format: <label>  <top>-<bot>/<total>  <pct>%
3792        assert!(status.contains("1-3/3"), "got: {status}");
3793        assert!(!status.contains("L1"), "no L block in line-mode: {status}");
3794        assert!(!status.contains("R1"), "no R block in line-mode: {status}");
3795    }
3796
3797    #[test]
3798    fn status_r_block_uses_real_lines_in_hide_mode() {
3799        // Regression: in hide mode `bottom` is a position in visible_lines
3800        // (i.e. a count of *visible* matches), not a logical line index.
3801        // The R-block was passing that position into `line_to_record`, which
3802        // resolved to whatever record contained logical line `bottom-1` —
3803        // typically a very early record, producing nonsense like `R290-8`
3804        // where the bottom record is *before* the top record on screen.
3805        // Build a scenario: many records, only the last few match the filter,
3806        // and the viewport is scrolled to the matching tail.
3807        let m = MockSource::new();
3808        // 10 records, two physical lines each. Record N's header has `kind=A`
3809        // for N < 8 and `kind=B` for N >= 8 (so only records 8 and 9 match).
3810        let mut buf = Vec::new();
3811        for n in 0..10 {
3812            let kind = if n >= 8 { "B" } else { "A" };
3813            buf.extend_from_slice(format!("[{}] kind={}\n  body {}\n", n, kind, n).as_bytes());
3814        }
3815        m.append(&buf);
3816        m.finish();
3817
3818        let mut idx = LineIndex::new();
3819        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3820        idx.extend_to_end(&m);
3821
3822        let fmt = crate::format::LogFormat::compile(
3823            "rec",
3824            r"^\[(?P<id>\d+)\] kind=(?P<kind>.+)$",
3825        )
3826        .unwrap();
3827        let f = crate::filter::CompiledFilter::compile(
3828            &fmt,
3829            vec![crate::filter::FilterSpec::parse("kind=B").unwrap()],
3830            CaseMode::Sensitive,
3831        )
3832        .unwrap();
3833
3834        // 5-row terminal: 4 body rows + 1 status row. With 4 visible-matches
3835        // rows of body and 4 visible lines, the whole filtered set fits.
3836        let mut v = Viewport::new(80, 5, "f".into());
3837        v.set_filter(Some(f));
3838        v.extend_visible_lines(&idx, &m);
3839
3840        // Jump to the first matching record (record 8, 0-indexed).
3841        v.goto_record(8, &m, &mut idx);
3842
3843        let frame = v.frame(&m, &mut idx);
3844        // Records 8 (rec_top=9) and 9 (rec_bottom=10) are on screen.
3845        assert!(
3846            frame.status.contains("R9-10/10"),
3847            "expected R9-10/10 in status, got: {}",
3848            frame.status,
3849        );
3850    }
3851
3852    #[test]
3853    fn status_dual_readout_when_records_active() {
3854        let m = MockSource::new();
3855        m.append(b"[1] a\n  cont\n[2] b\n");
3856        m.finish();
3857        let mut idx = LineIndex::new();
3858        idx.set_record_start(regex::bytes::Regex::new(r"^\[").unwrap());
3859        idx.extend_to_end(&m);
3860        let mut v = Viewport::new(20, 5, "f".into());
3861        let frame = v.frame(&m, &mut idx);
3862        let status = &frame.status;
3863        assert!(status.contains("L1-3/3"), "lines block missing or wrong: {status}");
3864        assert!(status.contains("R1-2/2"), "records block missing or wrong: {status}");
3865    }
3866
3867    #[test]
3868    fn format_status_uses_custom_template_when_set() {
3869        let m = MockSource::new();
3870        m.append(b"a\nb\nc\n");
3871        m.finish();
3872        let mut idx = LineIndex::new();
3873        idx.extend_to_end(&m);
3874        let mut v = Viewport::new(20, 5, "f".into());
3875        let prompt = crate::prompt::ParsedPrompt::parse("<label> <pct>%").unwrap();
3876        v.set_prompt(Some(prompt));
3877        let frame = v.frame(&m, &mut idx);
3878        assert_eq!(frame.status, "f 100%");
3879    }
3880
3881    #[test]
3882    fn status_shows_preprocess_failed_tag_when_set() {
3883        let m = MockSource::new();
3884        m.append(b"a\n");
3885        let mut idx = LineIndex::new();
3886        idx.extend_to_end(&m);
3887        let mut v = Viewport::new(40, 5, "f".into());
3888        v.set_preprocess_failure(Some("pdftotext: not found".to_string()));
3889        let frame = v.frame(&m, &mut idx);
3890        assert!(frame.status.contains("[preprocess-failed: pdftotext: not found]"),
3891                "got: {}", frame.status);
3892    }
3893
3894    #[test]
3895    fn default_status_includes_help_hint() {
3896        let (m, mut idx) = setup(b"a\nb\nc\n");
3897        let mut v = Viewport::new(80, 5, "f".into());
3898        let frame = v.frame(&m, &mut idx);
3899        assert!(frame.status.ends_with(":help"), "got: {:?}", frame.status);
3900    }
3901
3902    #[test]
3903    fn custom_prompt_does_not_get_help_hint() {
3904        let (m, mut idx) = setup(b"a\nb\nc\n");
3905        let mut v = Viewport::new(80, 5, "f".into());
3906        v.set_prompt(Some(crate::prompt::ParsedPrompt::parse("<label>").unwrap()));
3907        let frame = v.frame(&m, &mut idx);
3908        assert!(!frame.status.contains(":help"), "got: {:?}", frame.status);
3909    }
3910
3911    #[test]
3912    fn status_shows_file_index_when_multifile() {
3913        let m = MockSource::new();
3914        m.append(b"a\n");
3915        let mut idx = LineIndex::new();
3916        idx.extend_to_end(&m);
3917        let mut v = Viewport::new(60, 5, "f.log".into());
3918        v.set_file_index(0, 3);
3919        let frame = v.frame(&m, &mut idx);
3920        assert!(frame.status.contains("f.log  [1/3]"), "got: {}", frame.status);
3921    }
3922
3923    #[test]
3924    fn status_omits_file_index_when_single_file() {
3925        let m = MockSource::new();
3926        m.append(b"a\n");
3927        let mut idx = LineIndex::new();
3928        idx.extend_to_end(&m);
3929        let mut v = Viewport::new(60, 5, "f.log".into());
3930        v.set_file_index(0, 1);
3931        let frame = v.frame(&m, &mut idx);
3932        assert!(!frame.status.contains('['), "should not show [1/1] for single-file: {}", frame.status);
3933    }
3934
3935    #[test]
3936    fn status_shows_tag_active_when_multimatch() {
3937        let m = MockSource::new();
3938        m.append(b"a\n");
3939        let mut idx = LineIndex::new();
3940        idx.extend_to_end(&m);
3941        let mut v = Viewport::new(80, 5, "f.log".into());
3942        v.set_tag_active(Some(("foo".into(), 2, 3)));
3943        let frame = v.frame(&m, &mut idx);
3944        assert!(
3945            frame.status.contains("[tag: foo (2/3)]"),
3946            "got: {}",
3947            frame.status
3948        );
3949    }
3950
3951    #[test]
3952    fn status_omits_tag_active_when_single_match() {
3953        let m = MockSource::new();
3954        m.append(b"a\n");
3955        let mut idx = LineIndex::new();
3956        idx.extend_to_end(&m);
3957        let mut v = Viewport::new(80, 5, "f.log".into());
3958        v.set_tag_active(Some(("foo".into(), 1, 1)));
3959        let frame = v.frame(&m, &mut idx);
3960        assert!(
3961            !frame.status.contains("[tag:"),
3962            "should not show indicator for single match: {}",
3963            frame.status
3964        );
3965    }
3966
3967    #[test]
3968    fn hscroll_noop_when_wrapping() {
3969        let mut v = Viewport::new(80, 24, "t".into());
3970        // default is wrap ON → hscroll inactive
3971        v.hscroll_right_step();
3972        assert_eq!(v.left_col(), 0);
3973    }
3974
3975    #[test]
3976    fn hscroll_active_in_chop_and_clamps_at_zero() {
3977        let mut v = Viewport::new(80, 24, "t".into());
3978        v.toggle_chop(); // turn chop ON (wrap off)
3979        assert!(v.hscroll_active());
3980        v.hscroll_right_step();
3981        assert_eq!(v.left_col(), 8);
3982        v.hscroll_right_half();
3983        assert_eq!(v.left_col(), 8 + 40); // cols/2 = 40
3984        v.hscroll_left_half();
3985        assert_eq!(v.left_col(), 8);
3986        v.hscroll_left_half();
3987        assert_eq!(v.left_col(), 0); // clamps at 0
3988    }
3989
3990    #[test]
3991    fn hscroll_by_explicit_cols_moves_left_col() {
3992        // `--shift N` path: scroll right/left by an explicit column count.
3993        let mut v = Viewport::new(80, 24, "t".into());
3994        v.toggle_chop(); // chop on so hscroll is active
3995        v.hscroll_right_cols(12);
3996        assert_eq!(v.left_col(), 12);
3997        v.hscroll_right_cols(12);
3998        assert_eq!(v.left_col(), 24);
3999        v.hscroll_left_cols(12);
4000        assert_eq!(v.left_col(), 12);
4001        v.hscroll_left_cols(99);
4002        assert_eq!(v.left_col(), 0); // clamps at 0
4003    }
4004
4005    #[test]
4006    fn hscroll_resets_to_zero_when_wrap_turned_on() {
4007        let mut v = Viewport::new(80, 24, "t".into());
4008        v.toggle_chop();             // chop on
4009        v.hscroll_right_step();
4010        assert_eq!(v.left_col(), 8);
4011        v.toggle_chop();             // wrap back on → reset
4012        assert_eq!(v.left_col(), 0);
4013    }
4014
4015    #[test]
4016    fn reset_hscroll_zeroes_left_col() {
4017        // The new-file path (app::switch_file) calls this after goto_top.
4018        let mut v = Viewport::new(80, 24, "t".into());
4019        v.toggle_chop();
4020        v.hscroll_right_step();
4021        assert_eq!(v.left_col(), 8);
4022        v.reset_hscroll();
4023        assert_eq!(v.left_col(), 0);
4024    }
4025
4026    // ----- SGR state reconstruction tests -----
4027
4028    #[test]
4029    fn reconstruct_picks_up_state_from_prior_lines() {
4030        let m = MockSource::new();
4031        m.append(b"\x1b[31mline 1\n");
4032        m.append(b"line 2 (still red, no reset)\n");
4033        m.append(b"line 3\n");
4034        let mut idx = LineIndex::new();
4035        idx.extend_to_end(&m);
4036        let state = reconstruct_render_state(&m, &idx, 2);
4037        assert_eq!(
4038            state.style.fg,
4039            Some(crate::ansi::Color::Ansi(1)),
4040            "red SGR from line 0 should persist to line 2"
4041        );
4042    }
4043
4044    #[test]
4045    fn reconstruct_respects_reset_between_lines() {
4046        let m = MockSource::new();
4047        m.append(b"\x1b[31mline 1\x1b[0m\n");
4048        m.append(b"line 2 (default)\n");
4049        let mut idx = LineIndex::new();
4050        idx.extend_to_end(&m);
4051        let state = reconstruct_render_state(&m, &idx, 1);
4052        assert_eq!(state.style.fg, None);
4053    }
4054
4055    #[test]
4056    fn reconstruct_caps_walkback_at_max_lines() {
4057        let m = MockSource::new();
4058        m.append(b"\x1b[31mvery early\n");
4059        for _ in 0..300 {
4060            m.append(b"line\n");
4061        }
4062        let mut idx = LineIndex::new();
4063        idx.extend_to_end(&m);
4064        // Line 290 is 290 lines past the red SGR. We cap at 256, so the
4065        // anchor we'd pick is line 34 (290 - 256), which is past the red.
4066        let state = reconstruct_render_state(&m, &idx, 290);
4067        assert_eq!(state.style.fg, None);
4068    }
4069
4070    #[test]
4071    fn or_groups_narrow_within_required_line_mode() {
4072        let mut raw = crate::or::OrSpecRaw::new();
4073        raw.add_grep(crate::or::DEFAULT_GROUP, "failed".into());
4074        raw.add_grep(crate::or::DEFAULT_GROUP, "denied".into());
4075        let og = crate::or::OrGroups::compile(&raw, None, crate::viewport::CaseMode::Sensitive).unwrap();
4076        let mut v = Viewport::new(80, 24, "t".into());
4077        v.set_or_groups(og);
4078        assert!(v.or_active());
4079        assert!(v.line_passes(b"login failed"));
4080        assert!(v.line_passes(b"access denied"));
4081        assert!(!v.line_passes(b"login ok"));
4082    }
4083
4084    #[test]
4085    fn status_shows_or_indicator_when_active() {
4086        let mut raw = crate::or::OrSpecRaw::new();
4087        raw.add_grep(crate::or::DEFAULT_GROUP, "x".into());
4088        let og = crate::or::OrGroups::compile(&raw, None, crate::viewport::CaseMode::Sensitive).unwrap();
4089        let (m, mut idx) = setup(b"x\ny\nx\n");
4090        idx.extend_to_end(&m);
4091        let mut v = Viewport::new(80, 5, "f".into());
4092        v.set_or_groups(og);
4093        v.extend_visible_lines(&idx, &m);
4094        let status = v.format_status(&idx, &m);
4095        assert!(status.contains("[or]"), "expected [or] in status: {status}");
4096        assert!(status.contains("[hide]"), "expected [hide] in status: {status}");
4097    }
4098
4099    #[test]
4100    fn status_shows_col_offset_when_scrolled() {
4101        // Long single line wider than the viewport; chop mode + scroll right.
4102        let content = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz\n";
4103        let (m, mut idx) = setup(content);
4104        let mut v = Viewport::new(10, 3, "t".into());
4105        v.toggle_chop();          // chop mode (wrap off) — hscroll active
4106        v.hscroll_right_step();   // left_col = HSCROLL_STEP (8)
4107        let f = v.frame(&m, &mut idx);
4108        assert!(
4109            f.status.contains('\u{00bb}'),
4110            "expected » in status after hscroll_right_step, got: {}",
4111            f.status
4112        );
4113    }
4114
4115    #[test]
4116    fn frame_text_horizontal_scroll_shifts_and_marks_left_edge() {
4117        // A single long line "ABCDEFGHIJKLMNOPQRSTUVWXYZ..." wider than cols,
4118        // in chop mode with a small viewport. Tests:
4119        //   1) At left_col==0: first body cell is 'A', no '<' marker.
4120        //   2) After hscroll_right_step(): first text-region cell is '<' (left
4121        //      marker), and a subsequent cell shows a char that was off-screen
4122        //      at offset 0 (proves the content shifted).
4123        // HSCROLL_STEP = 8, so after one step left_col == 8. Cell 0 becomes '<'
4124        // and cell 1 should be 'J' (0-indexed display column 9: A=0 … I=8, J=9).
4125        let content = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n";
4126        let (m, mut idx) = setup(content);
4127
4128        // 10 cols wide, 3 rows (body = 2). Default is wrap=true, so toggle chop.
4129        let mut v = Viewport::new(10, 3, "t".into());
4130        v.toggle_chop(); // chop mode = !wrap
4131
4132        // ---- pass 1: no horizontal scroll ----
4133        let frame0 = v.frame(&m, &mut idx);
4134        assert_eq!(
4135            frame0.body[0][0],
4136            Cell::Char { ch: 'A', width: 1, style: crate::ansi::Style::default(), hyperlink: None },
4137            "at left_col=0 first cell should be 'A'"
4138        );
4139        // No '<' marker at all
4140        assert!(
4141            !frame0.body[0].iter().any(|c| matches!(c, Cell::Char { ch: '<', .. })),
4142            "no left marker expected at left_col=0"
4143        );
4144
4145        // ---- pass 2: scroll right by one step (HSCROLL_STEP=8 columns) ----
4146        v.hscroll_right_step();
4147        assert_eq!(v.left_col(), 8, "left_col should be 8 after one right step");
4148
4149        let frame1 = v.frame(&m, &mut idx);
4150        // First content cell (col 0, no gutter) should be the '<' marker.
4151        assert_eq!(
4152            frame1.body[0][0],
4153            Cell::Char { ch: '<', width: 1, style: crate::ansi::Style { dim: true, ..Default::default() }, hyperlink: None },
4154            "after scrolling right, first cell should be the '<' left marker"
4155        );
4156        // The cell at index 1 (display col 1 of the content region) was at
4157        // absolute column 9 = left_col(8) + 1. In "ABCDEFGHIJKL..." the char
4158        // at display column 9 (0-indexed) is 'J'. Verify content shifted.
4159        assert_eq!(
4160            frame1.body[0][1],
4161            Cell::Char { ch: 'J', width: 1, style: crate::ansi::Style::default(), hyperlink: None },
4162            "second cell should be 'J' (display column left_col+1 = 9)"
4163        );
4164    }
4165}