diff: Add commit meta rendering
750d7fb790b5fa93384f8e05d68f63af141714f0
1 parent
18202992
crates/diff/benches/render.rs
+3 -3
| 359 | 359 | Err(error) => { |
|
| 360 | 360 | eprintln!("failed to decode generated benchmark diff: {error}"); |
|
| 361 | 361 | std::process::exit(1); |
|
| 362 | 362 | } |
|
| 363 | 363 | }; |
|
| 364 | - | let rows = DiffViewer::new(document.clone(), DiffOptions::default()).content_rows(AREA.cols); |
|
| 364 | + | let rows = DiffViewer::new(document.clone(), DiffOptions::default()).row_count(); |
|
| 365 | 365 | let mut rates = Vec::with_capacity(args.runs); |
|
| 366 | 366 | let mut checksum = rows; |
|
| 367 | 367 | ||
| 368 | 368 | for run in 0..args.runs { |
|
| 369 | 369 | let (run_elapsed, run_checksum) = run_once(workload, document.clone(), frames); |
| 505 | 505 | } |
|
| 506 | 506 | ||
| 507 | 507 | /// Renders while applying synthetic highlight results for visible rows. |
|
| 508 | 508 | fn run_highlight(document: DiffDocument, frames: usize) -> (Duration, usize) { |
|
| 509 | 509 | run_scripted(document, frames, |viewer, frame| { |
|
| 510 | - | let requests = viewer.visible_highlight_requests(AREA); |
|
| 510 | + | let requests = viewer.highlight_requests(AREA); |
|
| 511 | 511 | viewer.apply_highlights(requests.into_iter().take(AREA.rows).map(highlight_result)); |
|
| 512 | 512 | viewer.apply( |
|
| 513 | 513 | DiffAction::Scroll(Scroll::Lines((frame % 5 + 1) as isize)), |
|
| 514 | 514 | AREA, |
|
| 515 | 515 | ); |
| 522 | 522 | let mut viewer = DiffViewer::new(document, DiffOptions::default()); |
|
| 523 | 523 | let start = Instant::now(); |
|
| 524 | 524 | let mut checksum = 0usize; |
|
| 525 | 525 | ||
| 526 | 526 | for frame in 0..frames { |
|
| 527 | - | let requests = viewer.visible_highlight_requests(AREA); |
|
| 527 | + | let requests = viewer.highlight_requests(AREA); |
|
| 528 | 528 | checksum ^= sample_requests(&requests, frame); |
|
| 529 | 529 | viewer.apply( |
|
| 530 | 530 | DiffAction::Scroll(Scroll::Lines((frame % 5 + 1) as isize)), |
|
| 531 | 531 | AREA, |
|
| 532 | 532 | ); |
src/file_{file:04}.rs\n"
+0 -0
crates/diff/src/bin/rustik-diff.rs
+2 -2
| 150 | 150 | ..DiffOptions::default() |
|
| 151 | 151 | }; |
|
| 152 | 152 | ||
| 153 | 153 | if !io::stdout().is_terminal() { |
|
| 154 | 154 | let mut viewer = DiffViewer::new(document, options); |
|
| 155 | - | let rows = viewer.content_rows(STATIC_COLS).min(STATIC_MAX_ROWS); |
|
| 155 | + | let rows = viewer.row_count().min(STATIC_MAX_ROWS); |
|
| 156 | 156 | let constraints = tui::Constraint::max(tui::Size::new(STATIC_COLS, rows)); |
|
| 157 | 157 | ||
| 158 | 158 | tui_termion::term::print_constrained(&mut viewer, constraints)?; |
|
| 159 | 159 | return Ok(()); |
|
| 160 | 160 | } |
| 243 | 243 | } |
|
| 244 | 244 | ||
| 245 | 245 | /// Requests highlight work for currently visible rows. |
|
| 246 | 246 | fn request_visible_highlights(&mut self) { |
|
| 247 | 247 | let area = viewer_area(self.terminal.area()); |
|
| 248 | - | let requests = self.viewer.visible_highlight_requests(area); |
|
| 248 | + | let requests = self.viewer.highlight_requests(area); |
|
| 249 | 249 | if !requests.is_empty() { |
|
| 250 | 250 | let _ = self.highlights_tx.send(requests); |
|
| 251 | 251 | } |
|
| 252 | 252 | } |
|
| 253 | 253 |
+0 -0
+0 -0
a.rs
+0 -0
a.rs\x1b[m
+0 -0
crates/diff/src/commit.rs
added
+207 -0
| 1 | + | //! Commit preamble parsed from `git log`-style diff input. |
|
| 2 | + | ||
| 3 | + | use std::fmt; |
|
| 4 | + | use std::str::FromStr; |
|
| 5 | + | ||
| 6 | + | use crate::{Oid, OidError}; |
|
| 7 | + | ||
| 8 | + | /// Parsed commit preamble. |
|
| 9 | + | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 10 | + | pub struct Commit { |
|
| 11 | + | /// Object identifier from the `commit <oid>` line. |
|
| 12 | + | pub hash: Oid, |
|
| 13 | + | /// Ref decoration following the hash (e.g. `(HEAD -> main, origin/main)`). |
|
| 14 | + | pub decoration: Option<String>, |
|
| 15 | + | /// Headers in input order with original key casing (e.g. `Author`, `Date`). |
|
| 16 | + | pub headers: Vec<Header>, |
|
| 17 | + | /// Commit message, verbatim and including any source indentation. |
|
| 18 | + | pub message: String, |
|
| 19 | + | } |
|
| 20 | + | ||
| 21 | + | /// One `Key: value` header line. |
|
| 22 | + | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 23 | + | pub struct Header { |
|
| 24 | + | /// Header name (e.g. `Author`). |
|
| 25 | + | pub key: String, |
|
| 26 | + | /// Header value, with leading whitespace after the colon trimmed. |
|
| 27 | + | pub value: String, |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Error returned when no `commit <hash>` line is found in the input. |
|
| 31 | + | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 32 | + | pub struct ParseCommitError; |
|
| 33 | + | ||
| 34 | + | impl fmt::Display for ParseCommitError { |
|
| 35 | + | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 36 | + | f.write_str("missing `commit <hash>` line") |
|
| 37 | + | } |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | impl std::error::Error for ParseCommitError {} |
|
| 41 | + | ||
| 42 | + | impl From<OidError> for ParseCommitError { |
|
| 43 | + | fn from(_: OidError) -> Self { |
|
| 44 | + | Self |
|
| 45 | + | } |
|
| 46 | + | } |
|
| 47 | + | ||
| 48 | + | impl Commit { |
|
| 49 | + | /// Returns the first header value matching `name` (case-insensitive). |
|
| 50 | + | pub fn header(&self, name: &str) -> Option<&str> { |
|
| 51 | + | self.headers |
|
| 52 | + | .iter() |
|
| 53 | + | .find(|header| header.key.eq_ignore_ascii_case(name)) |
|
| 54 | + | .map(|header| header.value.as_str()) |
|
| 55 | + | } |
|
| 56 | + | } |
|
| 57 | + | ||
| 58 | + | impl FromStr for Commit { |
|
| 59 | + | type Err = ParseCommitError; |
|
| 60 | + | ||
| 61 | + | fn from_str(input: &str) -> Result<Self, Self::Err> { |
|
| 62 | + | let mut hash = None; |
|
| 63 | + | let mut decoration = None; |
|
| 64 | + | let mut headers = Vec::new(); |
|
| 65 | + | let mut lines = input.split('\n').peekable(); |
|
| 66 | + | ||
| 67 | + | while let Some(&line) = lines.peek() { |
|
| 68 | + | if line.is_empty() { |
|
| 69 | + | lines.next(); |
|
| 70 | + | break; |
|
| 71 | + | } |
|
| 72 | + | if let Some(value) = line.strip_prefix("commit ") { |
|
| 73 | + | let (oid, rest) = match value.trim().split_once(char::is_whitespace) { |
|
| 74 | + | Some((oid, rest)) => (oid, Some(rest.trim().to_owned())), |
|
| 75 | + | None => (value.trim(), None), |
|
| 76 | + | }; |
|
| 77 | + | hash = Some(oid.parse()?); |
|
| 78 | + | decoration = rest; |
|
| 79 | + | lines.next(); |
|
| 80 | + | continue; |
|
| 81 | + | } |
|
| 82 | + | if let Some(header) = Header::parse(line) { |
|
| 83 | + | headers.push(header); |
|
| 84 | + | lines.next(); |
|
| 85 | + | continue; |
|
| 86 | + | } |
|
| 87 | + | break; |
|
| 88 | + | } |
|
| 89 | + | ||
| 90 | + | Ok(Self { |
|
| 91 | + | hash: hash.ok_or(ParseCommitError)?, |
|
| 92 | + | decoration, |
|
| 93 | + | headers, |
|
| 94 | + | message: lines.collect::<Vec<_>>().join("\n"), |
|
| 95 | + | }) |
|
| 96 | + | } |
|
| 97 | + | } |
|
| 98 | + | ||
| 99 | + | impl fmt::Display for Commit { |
|
| 100 | + | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 101 | + | write!(f, "commit {}", self.hash)?; |
|
| 102 | + | if let Some(decoration) = &self.decoration { |
|
| 103 | + | write!(f, " {decoration}")?; |
|
| 104 | + | } |
|
| 105 | + | writeln!(f)?; |
|
| 106 | + | for header in &self.headers { |
|
| 107 | + | writeln!(f, "{}: {}", header.key, header.value)?; |
|
| 108 | + | } |
|
| 109 | + | if !self.message.is_empty() { |
|
| 110 | + | writeln!(f)?; |
|
| 111 | + | f.write_str(&self.message)?; |
|
| 112 | + | if !self.message.ends_with('\n') { |
|
| 113 | + | writeln!(f)?; |
|
| 114 | + | } |
|
| 115 | + | } |
|
| 116 | + | writeln!(f) |
|
| 117 | + | } |
|
| 118 | + | } |
|
| 119 | + | ||
| 120 | + | impl Header { |
|
| 121 | + | /// Parses one `Key: value` line, returning `None` if the shape is invalid. |
|
| 122 | + | fn parse(line: &str) -> Option<Self> { |
|
| 123 | + | let (key, value) = line.split_once(':')?; |
|
| 124 | + | let key = key.trim(); |
|
| 125 | + | if key.is_empty() || key.chars().any(char::is_whitespace) { |
|
| 126 | + | return None; |
|
| 127 | + | } |
|
| 128 | + | Some(Self { |
|
| 129 | + | key: key.to_owned(), |
|
| 130 | + | value: value.trim_start().to_owned(), |
|
| 131 | + | }) |
|
| 132 | + | } |
|
| 133 | + | } |
|
| 134 | + | ||
| 135 | + | #[cfg(test)] |
|
| 136 | + | mod tests { |
|
| 137 | + | use super::*; |
|
| 138 | + | ||
| 139 | + | #[test] |
|
| 140 | + | fn parses_headers_only() { |
|
| 141 | + | let commit = Commit::from_str("commit abc\nAuthor: A\n\n").expect("parses"); |
|
| 142 | + | ||
| 143 | + | assert_eq!(commit.hash.as_str(), "abc"); |
|
| 144 | + | assert_eq!(commit.decoration, None); |
|
| 145 | + | assert_eq!(commit.header("author"), Some("A")); |
|
| 146 | + | assert!(commit.message.is_empty()); |
|
| 147 | + | } |
|
| 148 | + | ||
| 149 | + | #[test] |
|
| 150 | + | fn parses_sha256_hash() { |
|
| 151 | + | let oid = "0".repeat(64); |
|
| 152 | + | let input = format!("commit {oid}\n\n"); |
|
| 153 | + | let commit = Commit::from_str(&input).expect("parses"); |
|
| 154 | + | ||
| 155 | + | assert_eq!(commit.hash.as_str(), oid); |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | #[test] |
|
| 159 | + | fn parses_decoration_after_hash() { |
|
| 160 | + | let commit = Commit::from_str("commit abc (HEAD -> main)\n\n").expect("parses"); |
|
| 161 | + | ||
| 162 | + | assert_eq!(commit.hash.as_str(), "abc"); |
|
| 163 | + | assert_eq!(commit.decoration.as_deref(), Some("(HEAD -> main)")); |
|
| 164 | + | } |
|
| 165 | + | ||
| 166 | + | #[test] |
|
| 167 | + | fn parses_message_with_indent_preserved() { |
|
| 168 | + | let input = "commit abc\nAuthor: A\n\n Subject\n\n Body\n"; |
|
| 169 | + | let commit = Commit::from_str(input).expect("parses"); |
|
| 170 | + | ||
| 171 | + | assert_eq!(commit.message, " Subject\n\n Body\n"); |
|
| 172 | + | } |
|
| 173 | + | ||
| 174 | + | #[test] |
|
| 175 | + | fn rejects_input_without_commit_line() { |
|
| 176 | + | assert_eq!( |
|
| 177 | + | Commit::from_str("Author: A\nDate: B\n"), |
|
| 178 | + | Err(ParseCommitError), |
|
| 179 | + | ); |
|
| 180 | + | assert_eq!(Commit::from_str(""), Err(ParseCommitError)); |
|
| 181 | + | } |
|
| 182 | + | ||
| 183 | + | #[test] |
|
| 184 | + | fn rejects_non_hex_hash() { |
|
| 185 | + | assert_eq!( |
|
| 186 | + | Commit::from_str("commit not-hex\n\n"), |
|
| 187 | + | Err(ParseCommitError), |
|
| 188 | + | ); |
|
| 189 | + | } |
|
| 190 | + | ||
| 191 | + | #[test] |
|
| 192 | + | fn display_matches_decoder_line_encoding() { |
|
| 193 | + | let lines = [ |
|
| 194 | + | "commit abc", |
|
| 195 | + | "Author: A", |
|
| 196 | + | "", |
|
| 197 | + | " Subject", |
|
| 198 | + | "", |
|
| 199 | + | " Body", |
|
| 200 | + | "", |
|
| 201 | + | ]; |
|
| 202 | + | let commit: Commit = lines.join("\n").parse().expect("parses"); |
|
| 203 | + | let expected: String = lines.iter().map(|line| format!("{line}\n")).collect(); |
|
| 204 | + | ||
| 205 | + | assert_eq!(commit.to_string(), expected); |
|
| 206 | + | } |
|
| 207 | + | } |
crates/diff/src/decode.rs
+1 -1
| 132 | 132 | } |
|
| 133 | 133 | } |
|
| 134 | 134 | ||
| 135 | 135 | /// Decodes a full document from the remaining input. |
|
| 136 | 136 | fn decode_document(&mut self) -> Result<DiffDocument, DecodeError> { |
|
| 137 | - | let mut preamble = Vec::new(); |
|
| 137 | + | let mut commit = Vec::new(); |
|
| 138 | 138 | while self.has_more()? && !self.peek_starts_with(b" |
{
+0 -0
+0 -0
=> break,
+0 -0
a.rs
+0 -0
new.rs
+0 -0
old.rs
+0 -0
to.rs
+0 -0
copy2.rs
+0 -0
mode.sh
+0 -0
image.png
+0 -0
a
+0 -0
a
+0 -0
a
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/src/document.rs
+3 -3
| 108 | 108 | } |
|
| 109 | 109 | ||
| 110 | 110 | /// A complete unified diff document. |
|
| 111 | 111 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
|
| 112 | 112 | pub struct DiffDocument { |
|
| 113 | - | /// Lines before the first `diff --git` section. |
|
| 114 | - | pub preamble: Vec<String>, |
|
| 113 | + | /// Commit preamble before the first `diff --git` section, if present. |
|
| 114 | + | pub commit: Option<crate::Commit>, |
|
| 115 | 115 | /// File diffs in input order. |
|
| 116 | 116 | pub files: Vec<FileDiff>, |
|
| 117 | 117 | } |
|
| 118 | 118 | ||
| 119 | 119 | /// A single file section in a unified diff. |
| 608 | 608 | impl DiffDocument { |
|
| 609 | 609 | /// Returns a single-file document with the given hunks, suitable for |
|
| 610 | 610 | /// `git apply`. The preamble is dropped. |
|
| 611 | 611 | pub fn for_hunks(file: &FileDiff, hunks: impl IntoIterator<Item = Hunk>) -> Self { |
|
| 612 | 612 | Self { |
|
| 613 | - | preamble: Vec::new(), |
|
| 613 | + | commit: None, |
|
| 614 | 614 | files: vec![FileDiff { |
|
| 615 | 615 | id: file.id, |
|
| 616 | 616 | header: file.header.clone(), |
|
| 617 | 617 | hunks: hunks.into_iter().collect(), |
|
| 618 | 618 | }], |
a.txt
+0 -0
a
+0 -0
crates/diff/src/encode.rs
+2 -2
| 22 | 22 | } |
|
| 23 | 23 | } |
|
| 24 | 24 | ||
| 25 | 25 | impl Encode for DiffDocument { |
|
| 26 | 26 | fn encode(&self, out: &mut impl Write) -> io::Result<()> { |
|
| 27 | - | for line in &self.preamble { |
|
| 28 | - | writeln!(out, "{line}")?; |
|
| 27 | + | if let Some(commit) = &self.commit { |
|
| 28 | + | write!(out, "{commit}")?; |
|
| 29 | 29 | } |
|
| 30 | 30 | for file in &self.files { |
|
| 31 | 31 | file.encode(out)?; |
|
| 32 | 32 | } |
|
| 33 | 33 | Ok(()) |
a.txt
+0 -0
b.txt
+0 -0
crates/diff/src/lib.rs
+2 -0
| 6 | 6 | //! terminals, events, and background workers. |
|
| 7 | 7 | #![deny(unsafe_code)] |
|
| 8 | 8 | #![warn(missing_docs)] |
|
| 9 | 9 | #![warn(clippy::unwrap_used)] |
|
| 10 | 10 | ||
| 11 | + | mod commit; |
|
| 11 | 12 | mod document; |
|
| 12 | 13 | mod highlight; |
|
| 13 | 14 | mod renderer; |
|
| 14 | 15 | ||
| 15 | 16 | pub mod decode; |
|
| 16 | 17 | pub mod encode; |
|
| 17 | 18 | ||
| 19 | + | pub use commit::{Commit, Header, ParseCommitError}; |
|
| 18 | 20 | pub use decode::Decode; |
|
| 19 | 21 | pub use document::{ |
|
| 20 | 22 | DecodeError, DiffDocument, DiffLine, DiffLineKind, FileDiff, FileHeader, FileId, FileKind, |
|
| 21 | 23 | FileMode, FileStats, Hunk, HunkHeader, HunkId, Oid, OidError, RowId, Stats, |
|
| 22 | 24 | }; |
crates/diff/src/renderer.rs
+120 -19
| 5 | 5 | ||
| 6 | 6 | use rustik_highlight as syntax; |
|
| 7 | 7 | use rustik_tui as tui; |
|
| 8 | 8 | use tui::{ |
|
| 9 | 9 | Buffer, Cell, Color, Constraint, HStack, Label, Line, Placement, Rect, Scroll, ScrollView, |
|
| 10 | - | ScrollbarStyle, Size, Style, Text, View, Viewport, Widget, colors, |
|
| 10 | + | ScrollbarStyle, Size, Style, Table, TableOptions, Text, VStack, View, Viewport, Widget, colors, |
|
| 11 | 11 | }; |
|
| 12 | 12 | ||
| 13 | 13 | use crate::highlight::{HighlightKeyInput, HighlightResult}; |
|
| 14 | 14 | use crate::{ |
|
| 15 | - | DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId, FileKind, HighlightKey, |
|
| 16 | - | HighlightRequest, HighlightSource, Hunk, HunkId, RowId, |
|
| 15 | + | Commit, DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId, FileKind, |
|
| 16 | + | HighlightKey, HighlightRequest, HighlightSource, Hunk, HunkId, RowId, |
|
| 17 | 17 | }; |
|
| 18 | 18 | ||
| 19 | 19 | /// Minimum width before the renderer attempts table-style diff rows. |
|
| 20 | 20 | const MIN_RENDER_COLS: usize = 12; |
|
| 21 | 21 | /// Width of old and new line number cells. |
| 138 | 138 | state: DiffState, |
|
| 139 | 139 | /// Highlighted row cache. |
|
| 140 | 140 | highlights: BTreeMap<HighlightKey, Line>, |
|
| 141 | 141 | /// Requests already handed to a worker. |
|
| 142 | 142 | pending_highlights: BTreeSet<HighlightKey>, |
|
| 143 | + | /// Rendered commit-preamble rows for the most recent body width. |
|
| 144 | + | preamble: Vec<Line>, |
|
| 143 | 145 | } |
|
| 144 | 146 | ||
| 145 | 147 | /// One rendered row before viewport projection. |
|
| 146 | 148 | #[derive(Clone, Debug)] |
|
| 147 | 149 | struct RenderRow { |
| 246 | 248 | } |
|
| 247 | 249 | ||
| 248 | 250 | impl DiffViewer { |
|
| 249 | 251 | /// Creates a viewer with plain initial rendering state. |
|
| 250 | 252 | pub fn new(document: DiffDocument, options: DiffOptions) -> Self { |
|
| 253 | + | let preamble = Self::preamble(document.commit.as_ref(), MIN_RENDER_COLS); |
|
| 254 | + | ||
| 251 | 255 | Self { |
|
| 252 | 256 | document, |
|
| 253 | 257 | options, |
|
| 254 | 258 | state: DiffState::default(), |
|
| 255 | 259 | highlights: BTreeMap::new(), |
|
| 256 | 260 | pending_highlights: BTreeSet::new(), |
|
| 261 | + | preamble, |
|
| 257 | 262 | } |
|
| 258 | 263 | } |
|
| 259 | 264 | ||
| 260 | 265 | /// Returns the viewed document. |
|
| 261 | 266 | pub const fn document(&self) -> &DiffDocument { |
| 291 | 296 | pub fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 292 | 297 | if area.is_empty() { |
|
| 293 | 298 | return; |
|
| 294 | 299 | } |
|
| 295 | 300 | let body_cols = ScrollView::body_cols(area); |
|
| 301 | + | self.preamble = Self::preamble(self.document.commit.as_ref(), body_cols); |
|
| 302 | + | ||
| 296 | 303 | let scroll = self.state.scroll; |
|
| 297 | 304 | let content_rows = self.row_count(); |
|
| 298 | 305 | let sticky = self.sticky_rows(scroll, area.rows, body_cols); |
|
| 299 | 306 | // Body offset shifts past sticky so the file chrome is not repeated. |
|
| 300 | 307 | let body_offset = scroll.saturating_add(sticky.len().min(area.rows)); |
| 307 | 314 | .sticky(sticky) |
|
| 308 | 315 | .scrollbar(Self::scrollbar_style()) |
|
| 309 | 316 | .render(area, buf); |
|
| 310 | 317 | } |
|
| 311 | 318 | ||
| 312 | - | /// Returns the number of rendered rows at a given terminal width. |
|
| 313 | - | pub fn content_rows(&self, _cols: usize) -> usize { |
|
| 314 | - | self.row_count() |
|
| 315 | - | } |
|
| 316 | - | ||
| 317 | 319 | /// Applies an action to the viewer state. |
|
| 318 | 320 | pub fn apply(&mut self, action: DiffAction, area: Rect) -> ViewChange { |
|
| 319 | 321 | let before = self.state.clone(); |
|
| 320 | 322 | ||
| 321 | 323 | match action { |
| 360 | 362 | ViewChange::Changed |
|
| 361 | 363 | } |
|
| 362 | 364 | } |
|
| 363 | 365 | ||
| 364 | 366 | /// Returns highlight requests for visible source rows plus configured lookahead. |
|
| 365 | - | pub fn visible_highlight_requests(&mut self, area: Rect) -> Vec<HighlightRequest> { |
|
| 367 | + | pub fn highlight_requests(&mut self, area: Rect) -> Vec<HighlightRequest> { |
|
| 366 | 368 | let take = area.rows.saturating_add(self.options.highlight_lookahead); |
|
| 367 | 369 | let view = View::new(self.state.scroll, self.row_count(), area.rows); |
|
| 368 | 370 | let start = view |
|
| 369 | 371 | .offset |
|
| 370 | 372 | .saturating_add(self.sticky_row_count(view.offset, area.rows)); |
|
| 371 | 373 | let mut requests = Vec::new(); |
|
| 372 | 374 | let mut sources = BTreeMap::new(); |
|
| 375 | + | ||
| 373 | 376 | for row in start..start.saturating_add(take) { |
|
| 374 | 377 | let Some(meta) = self.diff_meta_at(row) else { |
|
| 375 | 378 | continue; |
|
| 376 | 379 | }; |
|
| 377 | 380 | let key = HighlightKey::new(HighlightKeyInput { |
| 411 | 414 | } |
|
| 412 | 415 | changed |
|
| 413 | 416 | } |
|
| 414 | 417 | ||
| 415 | 418 | /// Returns the content row count without building render rows. |
|
| 416 | - | fn row_count(&self) -> usize { |
|
| 417 | - | self.document.preamble.len() |
|
| 419 | + | pub fn row_count(&self) -> usize { |
|
| 420 | + | self.preamble.len() |
|
| 418 | 421 | + self |
|
| 419 | 422 | .document |
|
| 420 | 423 | .files |
|
| 421 | 424 | .iter() |
|
| 422 | - | .map(|file| self.file_row_count(file)) |
|
| 425 | + | .map(|file| self.file_rows(file)) |
|
| 423 | 426 | .sum::<usize>() |
|
| 424 | 427 | } |
|
| 425 | 428 | ||
| 426 | 429 | /// Returns the row count for one file card. |
|
| 427 | - | fn file_row_count(&self, file: &FileDiff) -> usize { |
|
| 430 | + | fn file_rows(&self, file: &FileDiff) -> usize { |
|
| 428 | 431 | file.rendered_rows(self.state.collapsed_files.contains(&file.id)) |
|
| 429 | 432 | } |
|
| 430 | 433 | ||
| 431 | 434 | /// Returns one rendered row by global row index. |
|
| 432 | 435 | fn render_row(&self, row: usize, cols: usize) -> Option<RenderRow> { |
|
| 433 | 436 | let cols = cols.max(MIN_RENDER_COLS); |
|
| 434 | - | if let Some(line) = self.document.preamble.get(row) { |
|
| 437 | + | if let Some(line) = self.preamble.get(row) { |
|
| 435 | 438 | return Some(RenderRow { |
|
| 436 | - | line: Line::new(line.clone()).style(Style::default().fg(colors::rgb::DIM)), |
|
| 439 | + | line: line.clone(), |
|
| 437 | 440 | diff: None, |
|
| 438 | 441 | }); |
|
| 439 | 442 | } |
|
| 440 | 443 | ||
| 441 | 444 | self.file_row_location(row) |
| 540 | 543 | None |
|
| 541 | 544 | } |
|
| 542 | 545 | ||
| 543 | 546 | /// Returns the file location for a global rendered row. |
|
| 544 | 547 | fn file_row_location(&self, row: usize) -> Option<FileRowLocation<'_>> { |
|
| 545 | - | let mut start = self.document.preamble.len(); |
|
| 548 | + | let mut start = self.preamble.len(); |
|
| 546 | 549 | for file in &self.document.files { |
|
| 547 | - | let end = start + self.file_row_count(file); |
|
| 550 | + | let end = start + self.file_rows(file); |
|
| 548 | 551 | if (start..end).contains(&row) { |
|
| 549 | 552 | return Some(FileRowLocation { |
|
| 550 | 553 | file, |
|
| 551 | 554 | start, |
|
| 552 | 555 | end, |
| 556 | 559 | start = end; |
|
| 557 | 560 | } |
|
| 558 | 561 | None |
|
| 559 | 562 | } |
|
| 560 | 563 | ||
| 564 | + | /// Lays out the commit preamble for the given body width. |
|
| 565 | + | fn preamble(commit: Option<&Commit>, cols: usize) -> Vec<Line> { |
|
| 566 | + | let Some(commit) = commit else { |
|
| 567 | + | return Vec::new(); |
|
| 568 | + | }; |
|
| 569 | + | let message = Self::preamble_message_lines(commit); |
|
| 570 | + | let hash_width = commit.hash.as_str().width() |
|
| 571 | + | + commit.decoration.as_deref().map_or(0, |d| 1 + d.width()); |
|
| 572 | + | let value_width = std::iter::once(hash_width) |
|
| 573 | + | .chain(commit.headers.iter().map(|h| h.value.as_str().width())) |
|
| 574 | + | .max() |
|
| 575 | + | .unwrap_or_default(); |
|
| 576 | + | let mut table = Table::<2, Line>::new(TableOptions { |
|
| 577 | + | overflow: true, |
|
| 578 | + | spacing: 2, |
|
| 579 | + | ..TableOptions::default() |
|
| 580 | + | }); |
|
| 581 | + | table.push([ |
|
| 582 | + | Line::new(Label::new("commit").fg(colors::rgb::DIM)), |
|
| 583 | + | Self::preamble_hash_line(&commit.hash, commit.decoration.as_deref(), value_width), |
|
| 584 | + | ]); |
|
| 585 | + | for header in &commit.headers { |
|
| 586 | + | let key = header.key.to_ascii_lowercase(); |
|
| 587 | + | table.push([ |
|
| 588 | + | Line::new(Label::new(&key).fg(colors::rgb::DIM)), |
|
| 589 | + | Self::preamble_header_line(&header.key, &header.value), |
|
| 590 | + | ]); |
|
| 591 | + | } |
|
| 592 | + | let mut stack = VStack::default() |
|
| 593 | + | .border(Some(tui::Border::new(colors::FAINT))) |
|
| 594 | + | .padding(1) |
|
| 595 | + | .child(table); |
|
| 596 | + | ||
| 597 | + | if !message.is_empty() { |
|
| 598 | + | stack = stack.divider_cell(Cell::new(DIVIDER, Style::new(colors::FAINT))); |
|
| 599 | + | for line in message { |
|
| 600 | + | stack.push(line); |
|
| 601 | + | } |
|
| 602 | + | } |
|
| 603 | + | stack.lines(Constraint::max(Size::new(cols, usize::MAX))) |
|
| 604 | + | } |
|
| 605 | + | ||
| 606 | + | /// Returns rendered commit message lines, with the subject line bold. |
|
| 607 | + | fn preamble_message_lines(commit: &Commit) -> Vec<Line> { |
|
| 608 | + | let texts: Vec<&str> = commit |
|
| 609 | + | .message |
|
| 610 | + | .split('\n') |
|
| 611 | + | .map(|raw| { |
|
| 612 | + | let trimmed = raw.trim_end(); |
|
| 613 | + | trimmed.strip_prefix(" ").unwrap_or(trimmed) |
|
| 614 | + | }) |
|
| 615 | + | .collect(); |
|
| 616 | + | let start = texts.iter().position(|text| !text.is_empty()); |
|
| 617 | + | let end = texts.iter().rposition(|text| !text.is_empty()); |
|
| 618 | + | let Some((start, end)) = start.zip(end) else { |
|
| 619 | + | return Vec::new(); |
|
| 620 | + | }; |
|
| 621 | + | texts[start..=end] |
|
| 622 | + | .iter() |
|
| 623 | + | .enumerate() |
|
| 624 | + | .map(|(index, &text)| { |
|
| 625 | + | let line = if text.is_empty() { |
|
| 626 | + | Line::blank() |
|
| 627 | + | } else { |
|
| 628 | + | Line::new(text.to_owned()) |
|
| 629 | + | }; |
|
| 630 | + | if index == 0 { |
|
| 631 | + | line.style(Style::default().bold()) |
|
| 632 | + | } else { |
|
| 633 | + | line |
|
| 634 | + | } |
|
| 635 | + | }) |
|
| 636 | + | .collect() |
|
| 637 | + | } |
|
| 638 | + | ||
| 561 | 639 | /// Wraps one file-card child row with padding and borders. |
|
| 562 | 640 | fn file_child_line(mut line: Line, child_cols: usize, border: tui::Border) -> Line { |
|
| 563 | 641 | Line::truncate(&mut line, child_cols, "…"); |
|
| 564 | 642 | Line::pad(&mut line, child_cols); |
|
| 565 | 643 | Line::new(border.left(1)).extend(line).item(border.right(1)) |
|
| 566 | 644 | } |
|
| 567 | 645 | ||
| 646 | + | /// Styles the commit hash row: hash highlighted, decoration suffix dim. |
|
| 647 | + | fn preamble_hash_line(hash: &crate::Oid, decoration: Option<&str>, value_width: usize) -> Line { |
|
| 648 | + | let hex = Line::new(Label::new(hash.as_str()).fg(colors::rgb::ORANGE)); |
|
| 649 | + | let Some(decoration) = decoration else { |
|
| 650 | + | return hex; |
|
| 651 | + | }; |
|
| 652 | + | HStack::default() |
|
| 653 | + | .child(hex) |
|
| 654 | + | .spacer() |
|
| 655 | + | .child(Line::new(Label::new(decoration).fg(colors::rgb::DIM))) |
|
| 656 | + | .to_line(Constraint::tight(value_width)) |
|
| 657 | + | } |
|
| 658 | + | ||
| 659 | + | /// Styles one non-hash commit header value. |
|
| 660 | + | fn preamble_header_line(key: &str, value: &str) -> Line { |
|
| 661 | + | let line = Line::new(value.to_owned()); |
|
| 662 | + | if key.eq_ignore_ascii_case("date") { |
|
| 663 | + | line.style(Style::new(colors::rgb::DIM)) |
|
| 664 | + | } else { |
|
| 665 | + | line |
|
| 666 | + | } |
|
| 667 | + | } |
|
| 668 | + | ||
| 568 | 669 | /// Returns sticky file-header rows for the current offset. |
|
| 569 | 670 | fn sticky_rows(&self, scroll: usize, visible_rows: usize, cols: usize) -> Vec<Line> { |
|
| 570 | 671 | self.sticky_start(scroll, visible_rows) |
|
| 571 | 672 | .map(|start| { |
|
| 572 | 673 | (0..STICKY_HEADER_ROWS) |
| 649 | 750 | } |
|
| 650 | 751 | ||
| 651 | 752 | /// Returns focusable targets in rendered row order. |
|
| 652 | 753 | fn focus_targets(&self, filter: FocusFilter) -> Vec<FocusTarget> { |
|
| 653 | 754 | let mut targets = Vec::new(); |
|
| 654 | - | let mut row = self.document.preamble.len(); |
|
| 755 | + | let mut row = self.preamble.len(); |
|
| 655 | 756 | ||
| 656 | 757 | for file in &self.document.files { |
|
| 657 | 758 | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 658 | 759 | if filter == FocusFilter::File { |
|
| 659 | 760 | targets.push(FocusTarget { |
| 891 | 992 | let cols = if parent.max.cols == usize::MAX { |
|
| 892 | 993 | MIN_RENDER_COLS |
|
| 893 | 994 | } else { |
|
| 894 | 995 | parent.max.cols |
|
| 895 | 996 | }; |
|
| 896 | - | Size::new(cols, self.content_rows(cols)).constrain(parent) |
|
| 997 | + | Size::new(cols, self.row_count()).constrain(parent) |
|
| 897 | 998 | } |
|
| 898 | 999 | ||
| 899 | 1000 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 900 | 1001 | DiffViewer::render(self, area, buf); |
|
| 901 | 1002 | } |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/diff/tests/golden.rs
+10 -6
| 140 | 140 | } |
|
| 141 | 141 | ||
| 142 | 142 | #[test] |
|
| 143 | 143 | fn preamble_and_no_newline_matches_golden_frame() { |
|
| 144 | 144 | assert_golden( |
|
| 145 | - | "preamble_and_no_newline_64x10.txt", |
|
| 146 | - | include_str!("golden/preamble_and_no_newline_64x10.txt"), |
|
| 145 | + | "preamble_and_no_newline_64x17.txt", |
|
| 146 | + | include_str!("golden/preamble_and_no_newline_64x17.txt"), |
|
| 147 | 147 | preamble_no_newline_viewer(), |
|
| 148 | 148 | 64, |
|
| 149 | - | 10, |
|
| 149 | + | 17, |
|
| 150 | 150 | ); |
|
| 151 | 151 | } |
|
| 152 | 152 | ||
| 153 | 153 | #[test] |
|
| 154 | 154 | fn file_kinds_match_golden_frame() { |
| 163 | 163 | ||
| 164 | 164 | #[test] |
|
| 165 | 165 | fn highlighted_source_matches_golden_frame() { |
|
| 166 | 166 | let mut viewer = sample_viewer(); |
|
| 167 | 167 | let request = viewer |
|
| 168 | - | .visible_highlight_requests(Rect::new(0, 0, 64, 5)) |
|
| 168 | + | .highlight_requests(Rect::new(0, 0, 64, 5)) |
|
| 169 | 169 | .into_iter() |
|
| 170 | 170 | .next() |
|
| 171 | 171 | .unwrap(); |
|
| 172 | 172 | viewer.apply_highlights([HighlightResult { |
|
| 173 | 173 | key: request.key, |
| 186 | 186 | fn assert_golden(name: &str, expected: &str, viewer: DiffViewer, cols: usize, rows: usize) { |
|
| 187 | 187 | let actual = render_frame(viewer, cols, rows); |
|
| 188 | 188 | assert_eq!(actual, expected.trim_end_matches('\n'), "{name}"); |
|
| 189 | 189 | } |
|
| 190 | 190 | ||
| 191 | - | fn render_frame(mut viewer: DiffViewer, cols: usize, rows: usize) -> String { |
|
| 191 | + | fn render_frame(viewer: DiffViewer, cols: usize, rows: usize) -> String { |
|
| 192 | + | render_buffer(viewer, cols, rows).lines().join("\n") |
|
| 193 | + | } |
|
| 194 | + | ||
| 195 | + | fn render_buffer(mut viewer: DiffViewer, cols: usize, rows: usize) -> Buffer { |
|
| 192 | 196 | let area = Rect::new(0, 0, cols, rows); |
|
| 193 | 197 | let mut buffer = Buffer::empty(area); |
|
| 194 | 198 | ||
| 195 | 199 | viewer.render(area, &mut buffer); |
|
| 196 | - | buffer.lines().join("\n") |
|
| 200 | + | buffer |
|
| 197 | 201 | } |
|
| 198 | 202 | ||
| 199 | 203 | fn sample_viewer() -> DiffViewer { |
|
| 200 | 204 | viewer_from_str(include_str!("golden/sample.diff")) |
|
| 201 | 205 | } |
crates/diff/tests/golden/preamble_and_no_newline_64x10.txt
deleted
+0 -10
| 1 | - | commit abc ▓ |
|
| 2 | - | Author: A ▓ |
|
| 3 | - | ▓ |
|
| 4 | - | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 5 | - | │ ○ a.rs +1 -1 │▓ |
|
| 6 | - | ├─────────────────────────────────────────────────────────────┤▓ |
|
| 7 | - | │ @@ -1,1 +1,1 @@ │▓ |
|
| 8 | - | │ 1 - old │▓ |
|
| 9 | - | │ 1 + new │▓ |
|
| 10 | - | │ \ No newline at end of file │🮘 |
crates/diff/tests/golden/preamble_and_no_newline_64x17.txt
added
+17 -0
| 1 | + | ╭──────────────────────────────────────────────────╮ ▓ |
|
| 2 | + | │ commit 1234567890abcdef1234567890abcdef12345678 │ ▓ |
|
| 3 | + | │ author A <a@example.com> │ ▓ |
|
| 4 | + | │ date Tue May 12 10:20:30 2026 +0200 │ ▓ |
|
| 5 | + | ├──────────────────────────────────────────────────┤ ▓ |
|
| 6 | + | │ Commit summary │ ▓ |
|
| 7 | + | │ │ ▓ |
|
| 8 | + | │ Message body text 1. │ ▓ |
|
| 9 | + | │ Message body text 2. │ ▓ |
|
| 10 | + | ╰──────────────────────────────────────────────────╯ ▓ |
|
| 11 | + | ╭─────────────────────────────────────────────────────────────╮▓ |
|
| 12 | + | │ ○ a.rs +1 -1 │▓ |
|
| 13 | + | ├─────────────────────────────────────────────────────────────┤▓ |
|
| 14 | + | │ @@ -1,1 +1,1 @@ │▓ |
|
| 15 | + | │ 1 - old │▓ |
|
| 16 | + | │ 1 + new │▓ |
|
| 17 | + | │ \ No newline at end of file │🮘 |
crates/diff/tests/golden/preamble_no_newline.diff
+8 -2
| 1 | - | commit abc |
|
| 2 | - | Author: A |
|
| 1 | + | commit 1234567890abcdef1234567890abcdef12345678 |
|
| 2 | + | Author: A <a@example.com> |
|
| 3 | + | Date: Tue May 12 10:20:30 2026 +0200 |
|
| 4 | + | ||
| 5 | + | Commit summary |
|
| 6 | + | ||
| 7 | + | Message body text 1. |
|
| 8 | + | Message body text 2. |
|
| 3 | 9 | ||
| 4 | 10 |
a.rs
+0 -0