From bf5cd89b967509682d445b4e021e30f5554a85ea Mon Sep 17 00:00:00 2001 From: Michael Ramos Date: Sun, 15 Nov 2026 10:58:54 -0500 Subject: [PATCH 1/3] feat: let hosts hide chrome, mark gutter lines, and reveal or select source - Options.chrome = false draws no label row, footer or help overlay. The diff and sidebar fill the whole rectangle, Command::Help is ignored, and the placeholders name no keys. The host reads status() for the footer's state. - Engine::set_markers takes Marker { range: SourceRange, style } values and draws a styled gutter glyph on visible rows whose source line on that side is in range. Markers are flattened per file and side when the set is replaced, so each drawn row costs one lookup. - Engine::reveal(file, side, line) puts the cursor on a displayed source line, and Engine::select_range(range) makes the same fixed selection a user would. Both return an error when the line is not in the diff. --- src/app.rs | 5 + src/app/controls.rs | 1 + src/app/sidebar.rs | 39 ++-- src/app/source.rs | 13 ++ src/engine.rs | 28 ++- src/engine/markers.rs | 227 +++++++++++++++++++++++ src/engine/selection.rs | 66 +++++++ src/render.rs | 98 +++++++--- src/storage.rs | 9 + src/view.rs | 11 ++ tests/engine.rs | 395 +++++++++++++++++++++++++++++++++++++++- 11 files changed, 849 insertions(+), 43 deletions(-) create mode 100644 src/engine/markers.rs diff --git a/src/app.rs b/src/app.rs index e2a57f0..19e5273 100644 --- a/src/app.rs +++ b/src/app.rs @@ -54,6 +54,8 @@ pub struct App { pub horizontal: usize, pub wrap: bool, pub numbers: bool, + /// Draws the label row, footer and help overlay. Hosts that own those rows turn it off. + pub chrome: bool, pub help: bool, pub cache: Cache, pub geometry: crate::geometry::Cache, @@ -65,6 +67,7 @@ pub struct App { pub worker: Worker, pub(crate) search_match: Option, pub(crate) selection: Option, + pub(crate) markers: crate::engine::markers::Markers, reading_anchor: Option, } @@ -92,12 +95,14 @@ impl App { worker, search_match: None, selection: None, + markers: Default::default(), top: Position::default(), cursor: Position::default(), old_side: false, horizontal: 0, wrap: false, numbers: true, + chrome: true, help: false, cache: Cache::default(), geometry: crate::geometry::Cache::default(), diff --git a/src/app/controls.rs b/src/app/controls.rs index 04de687..72da60a 100644 --- a/src/app/controls.rs +++ b/src/app/controls.rs @@ -138,6 +138,7 @@ impl App { self.sidebar.notice = None; let mut highlight = true; match command { + Command::Help if !self.chrome => return Ok(InputEffect::Ignored), Command::Help => { self.help = !self.help; highlight = false; diff --git a/src/app/sidebar.rs b/src/app/sidebar.rs index 723b2dd..ce69465 100644 --- a/src/app/sidebar.rs +++ b/src/app/sidebar.rs @@ -117,6 +117,16 @@ impl App { self.sidebar.preference = preference; } + /// The pane height that view movement reasons about. It always counts the label and + /// footer rows, so without chrome the diff rows fill the whole supplied rectangle. + pub(crate) fn layout_height(&self, screen: u16) -> u16 { + if self.chrome { + screen + } else { + screen.saturating_add(2) + } + } + pub(crate) fn prepare_view(&mut self, screen: Rect) -> anyhow::Result<()> { for file in &self.doc.files[self.sidebar.tree.len()..] { self.sidebar.tree.append(file)?; @@ -140,24 +150,21 @@ impl App { SidebarPreference::Show => true, SidebarPreference::Hide => false, }; - let shown = wants_tree && screen.width >= MIN_WIDTH && screen.height >= 10; + let height = self.layout_height(screen.height); + let top = screen.y + u16::from(self.chrome); + let shown = wants_tree && screen.width >= MIN_WIDTH && height >= 10; if shown { self.sidebar.notice = None; } self.sidebar.screen = screen; self.sidebar.area = if shown { - Rect::new(screen.x, screen.y + 1, TREE_WIDTH, screen.height - 2) + Rect::new(screen.x, top, TREE_WIDTH, height - 2) } else { Rect::default() }; let inset = if shown { TREE_WIDTH + 1 } else { 0 }; - self.sidebar.diff = if screen.width >= 40 && screen.height >= 10 { - Rect::new( - screen.x + inset, - screen.y + 1, - screen.width - inset, - screen.height - 2, - ) + self.sidebar.diff = if screen.width >= 40 && height >= 10 { + Rect::new(screen.x + inset, top, screen.width - inset, height - 2) } else { Rect::default() }; @@ -169,20 +176,28 @@ impl App { self.visible.clear(); return Ok(()); } - let changed = self.width != self.sidebar.diff.width || self.height != screen.height; + let changed = self.width != self.sidebar.diff.width || self.height != height; if changed { - self.reflow(self.layout, self.sidebar.diff.width, screen.height)?; + self.reflow(self.layout, self.sidebar.diff.width, height)?; } Ok(()) } + /// Gives the diff keyboard focus and closes help, as a host-requested move expects. + pub(crate) fn focus_diff(&mut self) { + self.sidebar.focus = Focus::Diff; + self.help = false; + } + pub(crate) fn files_command(&mut self) -> anyhow::Result<()> { if self.sidebar.focused() { self.sidebar.preference = SidebarPreference::Hide; self.sidebar.focus = Focus::Diff; } else { self.sidebar.preference = SidebarPreference::Show; - if self.sidebar.screen.width < MIN_WIDTH || self.sidebar.screen.height < 10 { + if self.sidebar.screen.width < MIN_WIDTH + || self.layout_height(self.sidebar.screen.height) < 10 + { self.sidebar.notice = Some("Files need at least 97 columns × 10 rows"); } else { self.sidebar.focus = Focus::Files; diff --git a/src/app/source.rs b/src/app/source.rs index 7b6e6bb..f622895 100644 --- a/src/app/source.rs +++ b/src/app/source.rs @@ -42,6 +42,19 @@ impl App { Ok(Some((id, old))) } + /// The displayed record of an original line on one side: a patch record, or revealed + /// checked context. Lines in unrevealed gaps, or outside the file, have none. + pub(crate) fn displayed_source(&self, file: usize, old: bool, line: u32) -> Option { + let index = self.doc.files.get(file)?; + let id = index + .source_for_line(old, line) + .map(LineId::Patch) + .or_else(|| self.view.context_line(file, old, line))?; + self.view + .source_row(&self.doc, id, self.split()) + .map(|_| id) + } + pub(crate) fn line_file(&self, id: LineId) -> &Arc { match id { LineId::Patch(id) => self.doc.file_for_source(id), diff --git a/src/engine.rs b/src/engine.rs index 621660e..e2d2330 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,5 +1,6 @@ //! A review owns its workers and view state, but never the host terminal. +pub(crate) mod markers; mod reading; mod selection; @@ -28,6 +29,7 @@ use crate::{ text::{safe_label, safe_text}, theme::Appearance, }; +pub use markers::Marker; pub enum Input { Patch(PathBuf), @@ -46,6 +48,9 @@ pub struct Options { pub sidebar: SidebarPreference, pub wrap: bool, pub line_numbers: bool, + /// Draw the label row, the footer and the help overlay. With `false` the diff fills the + /// whole draw rectangle, `Command::Help` is ignored, and the host shows `status()` itself. + pub chrome: bool, } impl Default for Options { @@ -56,6 +61,7 @@ impl Default for Options { sidebar: SidebarPreference::Auto, wrap: false, line_numbers: true, + chrome: true, } } } @@ -115,6 +121,8 @@ pub struct Engine { last_draw: Instant, draw_pending: bool, area: Rect, + /// Markers set before the first file arrived; the viewer state takes them when created. + markers: markers::Markers, } impl Engine { @@ -157,6 +165,7 @@ impl Engine { last_draw: Instant::now(), draw_pending: false, area: Rect::default(), + markers: markers::Markers::default(), }) } @@ -171,6 +180,7 @@ impl Engine { sidebar: app.sidebar.preference, wrap: app.wrap, line_numbers: app.numbers, + chrome: app.chrome, }) } @@ -305,6 +315,8 @@ impl Engine { app.set_sidebar_preference(self.options.sidebar); app.wrap = self.options.wrap; app.numbers = self.options.line_numbers; + app.chrome = self.options.chrome; + app.markers = std::mem::take(&mut self.markers); if self.full_sources { app.sources = Some(Vec::new()); } @@ -425,13 +437,24 @@ impl Engine { render::draw_with_controls(frame, app, area, self.error.as_deref(), controls)?; self.request |= previous_size != (app.width, app.height); } else { + // Without chrome the host owns the keys, so these placeholders name none. + let keys = self.options.chrome; let text = self .error .as_ref() - .map(|error| format!("Cannot open patch\n\n{error}\n\nq quit")) + .map(|error| { + format!( + "Cannot open patch\n\n{error}{}", + if keys { "\n\nq quit" } else { "" } + ) + }) .unwrap_or_else(|| { if self.input.is_some() { - "Loading patch…\n\nq quit · Ctrl-C cancel".into() + if keys { + "Loading patch…\n\nq quit · Ctrl-C cancel".into() + } else { + "Loading patch…".into() + } } else { "Review closed".into() } @@ -588,6 +611,7 @@ impl Engine { .transpose(); let colors = self.app.as_mut().map(|app| app.worker.close()).transpose(); self.app = None; + self.markers = markers::Markers::default(); self.loading = false; self.error = None; self.source_failed = false; diff --git a/src/engine/markers.rs b/src/engine/markers.rs new file mode 100644 index 0000000..70bd49b --- /dev/null +++ b/src/engine/markers.rs @@ -0,0 +1,227 @@ +use std::collections::{BinaryHeap, HashMap}; + +use anyhow::{Result, ensure}; +use ratatui::style::Style; + +use super::{Engine, InputEffect, ReviewId, Side}; +use crate::reading::SourceRange; + +/// A host-owned gutter mark over original source lines `range.first..=range.last` on one side. +/// +/// The engine draws `▌` in the first gutter cell of each visible row whose source line on +/// `range.side` falls in the range, patched with `style`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Marker { + pub range: SourceRange, + pub style: Style, +} + +/// The gutter glyph of a marked row. +pub(crate) const SYMBOL: &str = "▌"; + +/// Non-overlapping, ordered line spans for one side of one file. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Span { + first: u32, + last: u32, + style: Style, +} + +/// Markers indexed by file and side. Overlaps are resolved when the set is replaced, so a +/// drawn row costs one map lookup and one binary search, whatever the number of markers. +#[derive(Default)] +pub(crate) struct Markers { + files: HashMap; 2]>, +} + +impl Markers { + /// Later markers in `markers` win where ranges overlap. + fn new(markers: &[Marker]) -> Self { + let mut grouped: HashMap<(usize, usize), Vec> = HashMap::new(); + for (index, marker) in markers.iter().enumerate() { + grouped + .entry((marker.range.file, side_index(marker.range.side))) + .or_default() + .push(index); + } + let mut files: HashMap; 2]> = HashMap::new(); + for ((file, side), indices) in grouped { + files.entry(file).or_default()[side] = flatten(markers, indices); + } + Self { files } + } + + pub(crate) fn style(&self, file: usize, side: Side, line: Option) -> Option