diff: Add word-level diffs to viewer
2cc9bde71171f02f113dfbb8e0493300169605ea
1 parent
c4b8c855
crates/diff/benches/render.rs
+47 -0
| 150 | 150 | Collapse, |
|
| 151 | 151 | /// Visible highlight request and row-refresh workload. |
|
| 152 | 152 | Highlight, |
|
| 153 | 153 | /// Visible highlight request scheduling without drawing rows. |
|
| 154 | 154 | HighlightRequests, |
|
| 155 | + | /// Steady-state render of word-diff overlays on a warm highlight cache. |
|
| 156 | + | WordDiff, |
|
| 155 | 157 | /// Large file-count workload with short hunks. |
|
| 156 | 158 | ManyFiles, |
|
| 157 | 159 | /// Single huge file workload. |
|
| 158 | 160 | HugeFile, |
|
| 159 | 161 | /// Long and Unicode-heavy source line workload. |
| 172 | 174 | Self::Resize, |
|
| 173 | 175 | Self::Sticky, |
|
| 174 | 176 | Self::Collapse, |
|
| 175 | 177 | Self::Highlight, |
|
| 176 | 178 | Self::HighlightRequests, |
|
| 179 | + | Self::WordDiff, |
|
| 177 | 180 | Self::ManyFiles, |
|
| 178 | 181 | Self::HugeFile, |
|
| 179 | 182 | Self::LongLines, |
|
| 180 | 183 | ], |
|
| 181 | 184 | workload => vec![workload], |
| 190 | 193 | Self::Resize => "resize", |
|
| 191 | 194 | Self::Sticky => "sticky", |
|
| 192 | 195 | Self::Collapse => "collapse", |
|
| 193 | 196 | Self::Highlight => "highlight", |
|
| 194 | 197 | Self::HighlightRequests => "highlight-requests", |
|
| 198 | + | Self::WordDiff => "word-diff", |
|
| 195 | 199 | Self::ManyFiles => "many-files", |
|
| 196 | 200 | Self::HugeFile => "huge-file", |
|
| 197 | 201 | Self::LongLines => "long-lines", |
|
| 198 | 202 | Self::All => "all", |
|
| 199 | 203 | } |
| 207 | 211 | | Self::Session |
|
| 208 | 212 | | Self::Resize |
|
| 209 | 213 | | Self::Sticky |
|
| 210 | 214 | | Self::Collapse |
|
| 211 | 215 | | Self::Highlight |
|
| 216 | + | | Self::WordDiff |
|
| 212 | 217 | | Self::ManyFiles |
|
| 213 | 218 | | Self::HugeFile |
|
| 214 | 219 | | Self::LongLines |
|
| 215 | 220 | | Self::All => "fps", |
|
| 216 | 221 | } |
| 245 | 250 | }, |
|
| 246 | 251 | Self::Session |
|
| 247 | 252 | | Self::Resize |
|
| 248 | 253 | | Self::Highlight |
|
| 249 | 254 | | Self::HighlightRequests |
|
| 255 | + | | Self::WordDiff |
|
| 250 | 256 | | Self::WarmScroll |
|
| 251 | 257 | | Self::All => DiffShape { |
|
| 252 | 258 | files: args.files, |
|
| 253 | 259 | lines: args.lines, |
|
| 254 | 260 | hunk_lines: 64, |
| 268 | 274 | "resize" => Ok(Self::Resize), |
|
| 269 | 275 | "sticky" | "sticky-boundaries" => Ok(Self::Sticky), |
|
| 270 | 276 | "collapse" => Ok(Self::Collapse), |
|
| 271 | 277 | "highlight" | "highlights" => Ok(Self::Highlight), |
|
| 272 | 278 | "highlight-requests" | "requests" => Ok(Self::HighlightRequests), |
|
| 279 | + | "word-diff" | "worddiff" => Ok(Self::WordDiff), |
|
| 273 | 280 | "many-files" => Ok(Self::ManyFiles), |
|
| 274 | 281 | "huge-file" => Ok(Self::HugeFile), |
|
| 275 | 282 | "long-lines" => Ok(Self::LongLines), |
|
| 276 | 283 | "all" => Ok(Self::All), |
|
| 277 | 284 | _ => Err(()), |
| 386 | 393 | Workload::Resize => run_resize(document, frames), |
|
| 387 | 394 | Workload::Sticky => run_sticky(document, frames), |
|
| 388 | 395 | Workload::Collapse => run_collapse(document, frames), |
|
| 389 | 396 | Workload::Highlight => run_highlight(document, frames), |
|
| 390 | 397 | Workload::HighlightRequests => run_highlight_requests(document, frames), |
|
| 398 | + | Workload::WordDiff => run_word_diff(document, frames), |
|
| 391 | 399 | Workload::ManyFiles => run_session(document, frames), |
|
| 392 | 400 | Workload::HugeFile => run_sticky(document, frames), |
|
| 393 | 401 | Workload::LongLines => run_resize(document, frames), |
|
| 394 | 402 | Workload::All => unreachable!("run_once: all is expanded before dispatch"), |
|
| 395 | 403 | } |
| 515 | 523 | ); |
|
| 516 | 524 | AREA |
|
| 517 | 525 | }) |
|
| 518 | 526 | } |
|
| 519 | 527 | ||
| 528 | + | /// Renders cached word-diff overlays with a warm highlight cache. |
|
| 529 | + | /// |
|
| 530 | + | /// Pre-applies synthetic highlight results for the rows the workload will |
|
| 531 | + | /// scroll across, then measures render+scroll cost on already-overlaid lines. |
|
| 532 | + | /// Because every `-/+` pair in `synthetic_diff` passes the word-diff similarity |
|
| 533 | + | /// gate, this isolates the cached-render fast path that |
|
| 534 | + | /// [`DiffViewer::apply_highlights`] now bakes the overlay into. |
|
| 535 | + | fn run_word_diff(document: DiffDocument, frames: usize) -> (Duration, usize) { |
|
| 536 | + | let mut viewer = DiffViewer::new(document, DiffOptions::default()); |
|
| 537 | + | let mut buffer = Buffer::empty(AREA); |
|
| 538 | + | let span = AREA.rows.saturating_mul(2) as isize; |
|
| 539 | + | ||
| 540 | + | // Walk forward across the scroll span so every row we'll visit gets a |
|
| 541 | + | // highlight result. Then walk back to the start before timing begins. |
|
| 542 | + | for _ in 0..span { |
|
| 543 | + | let requests = viewer.highlight_requests(AREA); |
|
| 544 | + | viewer.apply_highlights(requests.into_iter().map(highlight_result)); |
|
| 545 | + | viewer.apply(DiffAction::Scroll(Scroll::Lines(1)), AREA); |
|
| 546 | + | } |
|
| 547 | + | viewer.apply(DiffAction::Scroll(Scroll::Lines(-span)), AREA); |
|
| 548 | + | ||
| 549 | + | let start = Instant::now(); |
|
| 550 | + | let mut checksum = 0usize; |
|
| 551 | + | ||
| 552 | + | for frame in 0..frames { |
|
| 553 | + | buffer.clear(); |
|
| 554 | + | viewer.render(AREA, &mut buffer); |
|
| 555 | + | checksum ^= sample_buffer(&buffer, frame); |
|
| 556 | + | let step = if (frame as isize / span) % 2 == 0 { |
|
| 557 | + | 1 |
|
| 558 | + | } else { |
|
| 559 | + | -1 |
|
| 560 | + | }; |
|
| 561 | + | viewer.apply(DiffAction::Scroll(Scroll::Lines(step)), AREA); |
|
| 562 | + | buffer.reset_dirty(); |
|
| 563 | + | } |
|
| 564 | + | (start.elapsed(), checksum) |
|
| 565 | + | } |
|
| 566 | + | ||
| 520 | 567 | /// Schedules visible highlight requests without drawing rendered rows. |
|
| 521 | 568 | fn run_highlight_requests(document: DiffDocument, frames: usize) -> (Duration, usize) { |
|
| 522 | 569 | let mut viewer = DiffViewer::new(document, DiffOptions::default()); |
|
| 523 | 570 | let start = Instant::now(); |
|
| 524 | 571 | let mut checksum = 0usize; |
src/file_{file:04}.rs\n"
+0 -0
crates/diff/src/lib.rs
+1 -0
| 10 | 10 | ||
| 11 | 11 | mod commit; |
|
| 12 | 12 | mod document; |
|
| 13 | 13 | mod highlight; |
|
| 14 | 14 | mod renderer; |
|
| 15 | + | mod word; |
|
| 15 | 16 | ||
| 16 | 17 | pub mod decode; |
|
| 17 | 18 | pub mod encode; |
|
| 18 | 19 | ||
| 19 | 20 | pub use commit::{Commit, Header, ParseCommitError}; |
crates/diff/src/renderer.rs
+197 -216
| 1 | 1 | //! Read-only diff viewer state and buffer renderer. |
|
| 2 | 2 | ||
| 3 | + | mod tree; |
|
| 4 | + | ||
| 5 | + | use std::collections::btree_map::Entry; |
|
| 3 | 6 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 4 | 7 | use std::path::PathBuf; |
|
| 5 | 8 | ||
| 6 | 9 | use rustik_highlight as syntax; |
|
| 7 | 10 | use rustik_tui as tui; |
| 10 | 13 | Placement, Rect, Scroll, ScrollView, ScrollbarStyle, Size, Style, Table, TableOptions, Text, |
|
| 11 | 14 | VStack, View, Viewport, Widget, colors, |
|
| 12 | 15 | }; |
|
| 13 | 16 | ||
| 14 | 17 | use crate::highlight::{HighlightKeyInput, HighlightResult}; |
|
| 18 | + | use crate::word; |
|
| 15 | 19 | use crate::{ |
|
| 16 | 20 | Commit, DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId, FileKind, |
|
| 17 | 21 | HighlightKey, HighlightRequest, HighlightSource, Hunk, HunkId, RowId, |
|
| 18 | 22 | }; |
|
| 19 | 23 | ||
| 20 | - | mod tree; |
|
| 21 | - | ||
| 22 | 24 | pub use tree::{FileSelection, FileTreeMode, FileTreeTarget}; |
|
| 23 | 25 | ||
| 24 | 26 | /// Minimum width before the renderer attempts table-style diff rows. |
|
| 25 | 27 | const MIN_RENDER_COLS: usize = 12; |
|
| 26 | 28 | /// Width of old and new line number cells. |
| 35 | 37 | const SCROLLBAR_THUMB: char = 'โ'; |
|
| 36 | 38 | /// Scrollbar thumb color, one fixed-color shade darker than dim chrome. |
|
| 37 | 39 | const SCROLLBAR_THUMB_COLOR: Color = Color::Fixed(238); |
|
| 38 | 40 | /// File-card divider glyph (used between header/hunks and between hunks). |
|
| 39 | 41 | const DIVIDER: char = 'โ'; |
|
| 42 | + | /// Shared border style for all file cards. |
|
| 43 | + | const FILE_BORDER: tui::Border = tui::Border::new(colors::FAINT); |
|
| 40 | 44 | /// Sticky file-header divider glyph. |
|
| 41 | 45 | const STICKY_DIVIDER: char = 'โด'; |
|
| 42 | 46 | /// Rename arrow. |
|
| 43 | 47 | const RENAME_ARROW: &str = "->"; |
|
| 44 | 48 | /// Missing trailing newline marker. |
| 51 | 55 | const DEFAULT_THEME: &str = "auto"; |
|
| 52 | 56 | /// Number of rows in a sticky file header. |
|
| 53 | 57 | const STICKY_HEADER_ROWS: usize = 3; |
|
| 54 | 58 | /// Number of body rows moved by line-scroll inputs. |
|
| 55 | 59 | const LINE_SCROLL_ROWS: isize = 3; |
|
| 60 | + | /// Local row of a file card's top border. |
|
| 61 | + | const FILE_TOP_ROW: usize = 0; |
|
| 62 | + | /// Local row of a file card's header. |
|
| 63 | + | const FILE_HEADER_ROW: usize = 1; |
|
| 64 | + | /// Local row of the divider between a file header and its hunk body. |
|
| 65 | + | const FILE_DIVIDER_ROW: usize = 2; |
|
| 66 | + | /// Local row of the first hunk body line after the fixed file-card chrome. |
|
| 67 | + | const HUNK_BODY_START: usize = FILE_DIVIDER_ROW + 1; |
|
| 68 | + | /// Total card height when the file has no visible body. |
|
| 69 | + | const EMPTY_CARD_ROWS: usize = FILE_DIVIDER_ROW + 1; |
|
| 56 | 70 | ||
| 57 | 71 | /// Mutable read-only diff viewport state. |
|
| 58 | 72 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
|
| 59 | 73 | pub struct DiffState { |
|
| 60 | 74 | /// First visible row in the rendered document. |
| 156 | 170 | pub struct DiffPalette { |
|
| 157 | 171 | /// Background color for removed source rows. |
|
| 158 | 172 | pub negative_row_bg: Color, |
|
| 159 | 173 | /// Background color for added source rows. |
|
| 160 | 174 | pub positive_row_bg: Color, |
|
| 175 | + | /// Background color for changed word spans inside removed rows. |
|
| 176 | + | pub negative_word_bg: Color, |
|
| 177 | + | /// Background color for changed word spans inside added rows. |
|
| 178 | + | pub positive_word_bg: Color, |
|
| 161 | 179 | /// Foreground color for removed stats, markers, and labels. |
|
| 162 | 180 | pub negative_fg: Color, |
|
| 163 | 181 | /// Foreground color for added stats, markers, and labels. |
|
| 164 | 182 | pub positive_fg: Color, |
|
| 165 | 183 | } |
| 175 | 193 | state: DiffState, |
|
| 176 | 194 | /// Highlighted row cache. |
|
| 177 | 195 | highlights: BTreeMap<HighlightKey, Line>, |
|
| 178 | 196 | /// Requests already handed to a worker. |
|
| 179 | 197 | pending_highlights: BTreeSet<HighlightKey>, |
|
| 198 | + | /// Cached intra-line word diffs, keyed by hunk. |
|
| 199 | + | word_diffs: BTreeMap<HunkId, word::WordChanges>, |
|
| 180 | 200 | /// Rendered commit-preamble rows for the most recent body width. |
|
| 181 | 201 | preamble: Vec<Line>, |
|
| 182 | 202 | /// File-tree pane state and model. |
|
| 183 | 203 | file_tree: tree::FileTreeViewer, |
|
| 184 | 204 | } |
| 249 | 269 | impl Default for DiffPalette { |
|
| 250 | 270 | fn default() -> Self { |
|
| 251 | 271 | Self { |
|
| 252 | 272 | negative_row_bg: colors::rgb::NEGATIVE_DARK, |
|
| 253 | 273 | positive_row_bg: colors::rgb::POSITIVE_DARK, |
|
| 274 | + | negative_word_bg: colors::rgb::NEGATIVE, |
|
| 275 | + | positive_word_bg: colors::rgb::POSITIVE, |
|
| 254 | 276 | negative_fg: colors::rgb::NEGATIVE_LIGHT, |
|
| 255 | 277 | positive_fg: colors::rgb::POSITIVE_LIGHT, |
|
| 256 | 278 | } |
|
| 257 | 279 | } |
|
| 258 | 280 | } |
| 275 | 297 | Self::theme_color(&theme.colors, "diffEditor.insertedLineBackground") |
|
| 276 | 298 | .unwrap_or(palette.positive_row_bg); |
|
| 277 | 299 | palette.negative_row_bg = |
|
| 278 | 300 | Self::theme_color(&theme.colors, "diffEditor.removedLineBackground") |
|
| 279 | 301 | .unwrap_or(palette.negative_row_bg); |
|
| 302 | + | palette.positive_word_bg = |
|
| 303 | + | Self::theme_color(&theme.colors, "diffEditor.insertedTextBackground") |
|
| 304 | + | .unwrap_or(palette.positive_word_bg); |
|
| 305 | + | palette.negative_word_bg = |
|
| 306 | + | Self::theme_color(&theme.colors, "diffEditor.removedTextBackground") |
|
| 307 | + | .unwrap_or(palette.negative_word_bg); |
|
| 280 | 308 | palette |
|
| 281 | 309 | } |
|
| 282 | 310 | } |
|
| 283 | 311 | ||
| 284 | 312 | impl DiffPalette { |
| 300 | 328 | document, |
|
| 301 | 329 | options, |
|
| 302 | 330 | state, |
|
| 303 | 331 | highlights: BTreeMap::new(), |
|
| 304 | 332 | pending_highlights: BTreeSet::new(), |
|
| 333 | + | word_diffs: BTreeMap::new(), |
|
| 305 | 334 | preamble, |
|
| 306 | 335 | file_tree, |
|
| 307 | 336 | } |
|
| 308 | 337 | } |
|
| 309 | 338 |
| 391 | 420 | } |
|
| 392 | 421 | ||
| 393 | 422 | /// Renders the main diff pane into `buf`. |
|
| 394 | 423 | fn render_diff(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 395 | 424 | let body_cols = ScrollView::body_cols(area); |
|
| 425 | + | // Rebuild preamble for the available body width so line wrapping matches |
|
| 426 | + | // current viewport size. |
|
| 396 | 427 | self.preamble = Self::preamble(self.document.commit.as_ref(), body_cols); |
|
| 397 | 428 | ||
| 398 | 429 | let scroll = self.state.scroll; |
|
| 399 | 430 | let content_rows = self.row_count(); |
|
| 400 | 431 | let sticky = self.sticky_rows(scroll, area.rows, body_cols); |
|
| 401 | - | // Body offset shifts past sticky so the file chrome is not repeated. |
|
| 432 | + | // Body offset skips sticky rows so the main diff starts below the fixed |
|
| 433 | + | // header strip. |
|
| 402 | 434 | let body_offset = scroll.saturating_add(sticky.len().min(area.rows)); |
|
| 403 | 435 | ||
| 404 | 436 | ScrollView::virtualized(content_rows, body_offset, |row, cols| { |
|
| 405 | 437 | self.render_row(row, cols) |
|
| 406 | 438 | .map(|row| row.line) |
| 583 | 615 | let view = View::new(self.state.scroll, self.row_count(), diff.rows); |
|
| 584 | 616 | let start = view |
|
| 585 | 617 | .offset |
|
| 586 | 618 | .saturating_add(self.sticky_row_count(view.offset, diff.rows)); |
|
| 587 | 619 | let mut requests = Vec::new(); |
|
| 588 | - | let mut sources = BTreeMap::new(); |
|
| 589 | 620 | ||
| 590 | 621 | for row in start..start.saturating_add(take) { |
|
| 591 | 622 | let Some(meta) = self.diff_meta_at(row) else { |
|
| 592 | 623 | continue; |
|
| 593 | 624 | }; |
|
| 594 | - | let key = HighlightKey::new(HighlightKeyInput { |
|
| 595 | - | file: meta.file, |
|
| 596 | - | hunk: meta.hunk, |
|
| 597 | - | row: meta.row, |
|
| 598 | - | side: meta.side, |
|
| 599 | - | source_line: meta.source_line, |
|
| 600 | - | text: &meta.text, |
|
| 601 | - | syntax: self.options.syntax.as_deref(), |
|
| 602 | - | theme: &self.options.theme, |
|
| 603 | - | }); |
|
| 625 | + | let key = self.key_for(&meta); |
|
| 604 | 626 | if self.highlights.contains_key(&key) || self.pending_highlights.contains(&key) { |
|
| 605 | 627 | continue; |
|
| 606 | 628 | } |
|
| 607 | 629 | self.pending_highlights.insert(key.clone()); |
|
| 608 | - | let source = sources |
|
| 609 | - | .entry((meta.file, meta.hunk, meta.side)) |
|
| 610 | - | .or_insert_with(|| self.highlight_source(meta.file, meta.hunk, meta.side)) |
|
| 611 | - | .clone(); |
|
| 612 | 630 | requests.push(HighlightRequest { |
|
| 613 | 631 | key, |
|
| 614 | 632 | path: meta.path, |
|
| 615 | - | source, |
|
| 633 | + | source: HighlightSource::new(meta.text, meta.source_line), |
|
| 616 | 634 | }); |
|
| 617 | 635 | } |
|
| 618 | 636 | requests |
|
| 619 | 637 | } |
|
| 620 | 638 | ||
| 639 | + | /// Returns the highlight-cache key for one diff row. |
|
| 640 | + | fn key_for(&self, meta: &DiffLineMeta) -> HighlightKey { |
|
| 641 | + | HighlightKey::new(HighlightKeyInput { |
|
| 642 | + | file: meta.file, |
|
| 643 | + | hunk: meta.hunk, |
|
| 644 | + | row: meta.row, |
|
| 645 | + | side: meta.side, |
|
| 646 | + | source_line: meta.source_line, |
|
| 647 | + | text: &meta.text, |
|
| 648 | + | syntax: self.options.syntax.as_deref(), |
|
| 649 | + | theme: &self.options.theme, |
|
| 650 | + | }) |
|
| 651 | + | } |
|
| 652 | + | ||
| 621 | 653 | /// Merges highlighted rows and returns whether a redraw is needed. |
|
| 654 | + | /// |
|
| 655 | + | /// The word-diff overlay is applied here, once, so the cached `Line` is |
|
| 656 | + | /// ready to render with no per-frame merge work. All paths that write to the |
|
| 657 | + | /// cached highlighted rows keep this invariant: cached lines are render-ready |
|
| 658 | + | /// and already overlaid. |
|
| 622 | 659 | pub fn apply_highlights(&mut self, results: impl IntoIterator<Item = HighlightResult>) -> bool { |
|
| 623 | 660 | let mut changed = false; |
|
| 624 | 661 | for result in results { |
|
| 625 | 662 | self.pending_highlights.remove(&result.key); |
|
| 626 | - | self.highlights.insert(result.key, result.line); |
|
| 663 | + | ||
| 664 | + | if let Entry::Vacant(slot) = self.word_diffs.entry(result.key.hunk) |
|
| 665 | + | && let Some(hunk) = self |
|
| 666 | + | .document |
|
| 667 | + | .files |
|
| 668 | + | .iter() |
|
| 669 | + | .find(|f| f.id == result.key.file) |
|
| 670 | + | .and_then(|f| f.hunks.iter().find(|h| h.id == result.key.hunk)) |
|
| 671 | + | { |
|
| 672 | + | slot.insert(crate::word::compute(hunk)); |
|
| 673 | + | } |
|
| 674 | + | let bg = match result.key.side { |
|
| 675 | + | DiffSide::New => self.options.diff_palette.positive_word_bg, |
|
| 676 | + | DiffSide::Old => self.options.diff_palette.negative_word_bg, |
|
| 677 | + | }; |
|
| 678 | + | let line = if let Some(ranges) = self |
|
| 679 | + | .word_diffs |
|
| 680 | + | .get(&result.key.hunk) |
|
| 681 | + | .and_then(|changes| changes.get(&result.key.row)) |
|
| 682 | + | { |
|
| 683 | + | result.line.overlay_ranges(ranges, Style::default().bg(bg)) |
|
| 684 | + | } else { |
|
| 685 | + | result.line |
|
| 686 | + | }; |
|
| 687 | + | self.highlights.insert(result.key, line); |
|
| 627 | 688 | changed = true; |
|
| 628 | 689 | } |
|
| 629 | 690 | changed |
|
| 630 | 691 | } |
|
| 631 | 692 |
| 658 | 719 | return Some(RenderRow { |
|
| 659 | 720 | line: line.clone(), |
|
| 660 | 721 | diff: None, |
|
| 661 | 722 | }); |
|
| 662 | 723 | } |
|
| 663 | - | ||
| 664 | 724 | self.file_row_location(row) |
|
| 665 | 725 | .map(|location| self.render_file_row(location, cols)) |
|
| 666 | 726 | } |
|
| 667 | 727 | ||
| 668 | - | /// Returns source-side text for highlighting a file side. |
|
| 669 | - | fn highlight_source(&self, file: FileId, hunk: HunkId, side: DiffSide) -> HighlightSource { |
|
| 670 | - | self.document |
|
| 671 | - | .files |
|
| 672 | - | .iter() |
|
| 673 | - | .find(|candidate| candidate.id == file) |
|
| 674 | - | .map(|file| file.highlight_source(hunk, side)) |
|
| 675 | - | .unwrap_or_else(|| HighlightSource::new("", 1)) |
|
| 676 | - | } |
|
| 677 | - | ||
| 678 | 728 | /// Returns one rendered row inside a file card. |
|
| 679 | 729 | fn render_file_row(&self, location: FileRowLocation<'_>, cols: usize) -> RenderRow { |
|
| 680 | 730 | let file = location.file; |
|
| 681 | - | let border = Self::file_border(); |
|
| 731 | + | let border = FILE_BORDER; |
|
| 682 | 732 | let inner_cols = cols.saturating_sub(2); |
|
| 683 | 733 | let child_cols = cols.saturating_sub(4); |
|
| 684 | 734 | let collapsed = self.state.collapsed_files.contains(&file.id); |
|
| 685 | 735 | let local = location.local; |
|
| 686 | 736 | let bottom = location.end - location.start - 1; |
|
| 687 | - | let wrap = |line| Self::file_child_line(line, child_cols, border); |
|
| 737 | + | // Child rows are full-width content lines rendered inside a bordered card. |
|
| 738 | + | let wrap_child = |mut line: Line| { |
|
| 739 | + | Line::truncate(&mut line, child_cols, "โฆ"); |
|
| 740 | + | Line::pad(&mut line, child_cols); |
|
| 741 | + | Line::new(border.left(1)).extend(line).item(border.right(1)) |
|
| 742 | + | }; |
|
| 743 | + | // Dedicated hunk-gap marker row keeps spacing between non-adjacent hunks. |
|
| 744 | + | let hunk_gap_line = Line::blank() |
|
| 745 | + | .extend(Line::blank().padded(LINE_NUMBER_WIDTH)) |
|
| 746 | + | .extend(Line::blank().padded(LINE_NUMBER_WIDTH)) |
|
| 747 | + | .extend(Self::marker_cell(HUNK_GAP_MARKER_CELL, colors::rgb::DIM)); |
|
| 688 | 748 | ||
| 689 | - | if local == 0 { |
|
| 690 | - | return RenderRow { |
|
| 691 | - | line: border.top(inner_cols), |
|
| 692 | - | diff: None, |
|
| 693 | - | }; |
|
| 694 | - | } |
|
| 695 | - | if local == 1 { |
|
| 696 | - | return RenderRow { |
|
| 697 | - | line: wrap(file.header_line(child_cols, self.options.diff_palette)), |
|
| 698 | - | diff: None, |
|
| 699 | - | }; |
|
| 700 | - | } |
|
| 701 | - | if local == bottom { |
|
| 702 | - | return RenderRow { |
|
| 703 | - | line: border.bottom(inner_cols), |
|
| 704 | - | diff: None, |
|
| 705 | - | }; |
|
| 706 | - | } |
|
| 707 | - | if !file.has_body(collapsed) { |
|
| 708 | - | return RenderRow { |
|
| 709 | - | line: border.bottom(inner_cols), |
|
| 710 | - | diff: None, |
|
| 711 | - | }; |
|
| 712 | - | } |
|
| 713 | - | if local == 2 { |
|
| 714 | - | return RenderRow { |
|
| 715 | - | line: border.divider(inner_cols, DIVIDER), |
|
| 716 | - | diff: None, |
|
| 717 | - | }; |
|
| 749 | + | match local { |
|
| 750 | + | FILE_TOP_ROW => { |
|
| 751 | + | return RenderRow { |
|
| 752 | + | line: border.top(inner_cols), |
|
| 753 | + | diff: None, |
|
| 754 | + | }; |
|
| 755 | + | } |
|
| 756 | + | FILE_HEADER_ROW => { |
|
| 757 | + | return RenderRow { |
|
| 758 | + | line: wrap_child(file.header_line(child_cols, self.options.diff_palette)), |
|
| 759 | + | diff: None, |
|
| 760 | + | }; |
|
| 761 | + | } |
|
| 762 | + | row if row == bottom => { |
|
| 763 | + | return RenderRow { |
|
| 764 | + | line: border.bottom(inner_cols), |
|
| 765 | + | diff: None, |
|
| 766 | + | }; |
|
| 767 | + | } |
|
| 768 | + | _ if !file.has_body(collapsed) => { |
|
| 769 | + | return RenderRow { |
|
| 770 | + | line: border.bottom(inner_cols), |
|
| 771 | + | diff: None, |
|
| 772 | + | }; |
|
| 773 | + | } |
|
| 774 | + | FILE_DIVIDER_ROW => { |
|
| 775 | + | return RenderRow { |
|
| 776 | + | line: border.divider(inner_cols, DIVIDER), |
|
| 777 | + | diff: None, |
|
| 778 | + | }; |
|
| 779 | + | } |
|
| 780 | + | _ => {} |
|
| 718 | 781 | } |
|
| 719 | 782 | let mut body_rows = file.hunk_body_rows(collapsed).peekable(); |
|
| 720 | 783 | while let Some(&(hunk, body_start)) = body_rows.peek() { |
|
| 721 | 784 | let body_end = body_start + hunk.lines.len(); |
|
| 722 | 785 | if local < body_start { |
|
| 723 | 786 | return RenderRow { |
|
| 724 | - | line: wrap(Self::hunk_gap_line()), |
|
| 787 | + | line: wrap_child(hunk_gap_line), |
|
| 725 | 788 | diff: None, |
|
| 726 | 789 | }; |
|
| 727 | 790 | } |
|
| 728 | 791 | if local < body_end { |
|
| 729 | 792 | let row = self.hunk_row_at(file, hunk, local - body_start, child_cols); |
|
| 730 | 793 | return RenderRow { |
|
| 731 | - | line: wrap(row.line), |
|
| 794 | + | line: wrap_child(row.line), |
|
| 732 | 795 | diff: row.diff, |
|
| 733 | 796 | }; |
|
| 734 | 797 | } |
|
| 735 | 798 | body_rows.next(); |
|
| 736 | 799 | } |
| 780 | 843 | /// Lays out the commit preamble for the given body width. |
|
| 781 | 844 | fn preamble(commit: Option<&Commit>, cols: usize) -> Vec<Line> { |
|
| 782 | 845 | let Some(commit) = commit else { |
|
| 783 | 846 | return Vec::new(); |
|
| 784 | 847 | }; |
|
| 785 | - | let message = Self::preamble_message_lines(commit); |
|
| 848 | + | // Normalize the message body first, then trim only outer empty lines. |
|
| 849 | + | let message_lines = commit |
|
| 850 | + | .message |
|
| 851 | + | .split('\n') |
|
| 852 | + | .map(|raw| { |
|
| 853 | + | let trimmed = raw.trim_end(); |
|
| 854 | + | trimmed.strip_prefix(" ").unwrap_or(trimmed) |
|
| 855 | + | }) |
|
| 856 | + | .collect::<Vec<_>>(); |
|
| 857 | + | let message = { |
|
| 858 | + | let first_non_empty = message_lines.iter().position(|text| !text.is_empty()); |
|
| 859 | + | let last_non_empty = message_lines.iter().rposition(|text| !text.is_empty()); |
|
| 860 | + | if let (Some(start), Some(end)) = (first_non_empty, last_non_empty) { |
|
| 861 | + | message_lines[start..=end] |
|
| 862 | + | .iter() |
|
| 863 | + | .enumerate() |
|
| 864 | + | .map(|(index, &text)| { |
|
| 865 | + | let line = if text.is_empty() { |
|
| 866 | + | Line::blank() |
|
| 867 | + | } else { |
|
| 868 | + | Line::new(text.to_owned()) |
|
| 869 | + | }; |
|
| 870 | + | if index == 0 { |
|
| 871 | + | line.style(Style::default().bold()) |
|
| 872 | + | } else { |
|
| 873 | + | line |
|
| 874 | + | } |
|
| 875 | + | }) |
|
| 876 | + | .collect() |
|
| 877 | + | } else { |
|
| 878 | + | Vec::new() |
|
| 879 | + | } |
|
| 880 | + | }; |
|
| 786 | 881 | let hash_width = commit.hash.as_str().width() |
|
| 787 | 882 | + commit.decoration.as_deref().map_or(0, |d| 1 + d.width()); |
|
| 788 | 883 | let value_width = std::iter::once(hash_width) |
|
| 789 | 884 | .chain(commit.headers.iter().map(|h| h.value.as_str().width())) |
|
| 790 | 885 | .max() |
|
| 791 | 886 | .unwrap_or_default(); |
|
| 887 | + | // Hash row is a two-part component (hash plus optional signature marker). |
|
| 888 | + | let hash_line = Line::new(Label::new(commit.hash.as_str()).fg(colors::rgb::ORANGE)); |
|
| 889 | + | let hash_value = if let Some(decoration) = commit.decoration.as_deref() { |
|
| 890 | + | HStack::default() |
|
| 891 | + | .child(hash_line) |
|
| 892 | + | .spacer() |
|
| 893 | + | .child(Line::new(Label::new(decoration).fg(colors::rgb::DIM))) |
|
| 894 | + | .to_line(Constraint::tight(value_width)) |
|
| 895 | + | } else { |
|
| 896 | + | hash_line |
|
| 897 | + | }; |
|
| 898 | + | // Commit header table is the base preamble chrome (commit id and metadata rows). |
|
| 792 | 899 | let mut table = Table::<2, Line>::new(TableOptions { |
|
| 793 | 900 | overflow: true, |
|
| 794 | 901 | spacing: 2, |
|
| 795 | 902 | ..TableOptions::default() |
|
| 796 | 903 | }); |
|
| 797 | 904 | table.push([ |
|
| 798 | 905 | Line::new(Label::new("commit").fg(colors::rgb::DIM)), |
|
| 799 | - | Self::preamble_hash_line(&commit.hash, commit.decoration.as_deref(), value_width), |
|
| 906 | + | hash_value, |
|
| 800 | 907 | ]); |
|
| 908 | + | // One row per commit header key/value, with date kept dim for softer emphasis. |
|
| 801 | 909 | for header in &commit.headers { |
|
| 802 | 910 | let key = header.key.to_ascii_lowercase(); |
|
| 911 | + | let header_value = if header.key.eq_ignore_ascii_case("date") { |
|
| 912 | + | Line::new(header.value.as_str()).style(Style::new(colors::rgb::DIM)) |
|
| 913 | + | } else { |
|
| 914 | + | Line::new(header.value.as_str()) |
|
| 915 | + | }; |
|
| 803 | 916 | table.push([ |
|
| 804 | 917 | Line::new(Label::new(&key).fg(colors::rgb::DIM)), |
|
| 805 | - | Self::preamble_header_line(&header.key, &header.value), |
|
| 918 | + | header_value, |
|
| 806 | 919 | ]); |
|
| 807 | 920 | } |
|
| 921 | + | // Optional message section is rendered as stacked lines after a divider. |
|
| 808 | 922 | let mut stack = VStack::default() |
|
| 809 | 923 | .border(Some(tui::Border::new(colors::FAINT))) |
|
| 810 | 924 | .padding(1) |
|
| 811 | 925 | .child(table); |
|
| 812 | 926 |
| 817 | 931 | } |
|
| 818 | 932 | } |
|
| 819 | 933 | stack.lines(Constraint::max(Size::new(cols, usize::MAX))) |
|
| 820 | 934 | } |
|
| 821 | 935 | ||
| 822 | - | /// Returns rendered commit message lines, with the subject line bold. |
|
| 823 | - | fn preamble_message_lines(commit: &Commit) -> Vec<Line> { |
|
| 824 | - | let texts: Vec<&str> = commit |
|
| 825 | - | .message |
|
| 826 | - | .split('\n') |
|
| 827 | - | .map(|raw| { |
|
| 828 | - | let trimmed = raw.trim_end(); |
|
| 829 | - | trimmed.strip_prefix(" ").unwrap_or(trimmed) |
|
| 830 | - | }) |
|
| 831 | - | .collect(); |
|
| 832 | - | let start = texts.iter().position(|text| !text.is_empty()); |
|
| 833 | - | let end = texts.iter().rposition(|text| !text.is_empty()); |
|
| 834 | - | let Some((start, end)) = start.zip(end) else { |
|
| 835 | - | return Vec::new(); |
|
| 836 | - | }; |
|
| 837 | - | texts[start..=end] |
|
| 838 | - | .iter() |
|
| 839 | - | .enumerate() |
|
| 840 | - | .map(|(index, &text)| { |
|
| 841 | - | let line = if text.is_empty() { |
|
| 842 | - | Line::blank() |
|
| 843 | - | } else { |
|
| 844 | - | Line::new(text.to_owned()) |
|
| 845 | - | }; |
|
| 846 | - | if index == 0 { |
|
| 847 | - | line.style(Style::default().bold()) |
|
| 848 | - | } else { |
|
| 849 | - | line |
|
| 850 | - | } |
|
| 851 | - | }) |
|
| 852 | - | .collect() |
|
| 853 | - | } |
|
| 854 | - | ||
| 855 | - | /// Wraps one file-card child row with padding and borders. |
|
| 856 | - | fn file_child_line(mut line: Line, child_cols: usize, border: tui::Border) -> Line { |
|
| 857 | - | Line::truncate(&mut line, child_cols, "โฆ"); |
|
| 858 | - | Line::pad(&mut line, child_cols); |
|
| 859 | - | Line::new(border.left(1)).extend(line).item(border.right(1)) |
|
| 860 | - | } |
|
| 861 | - | ||
| 862 | - | /// Styles the commit hash row: hash highlighted, decoration suffix dim. |
|
| 863 | - | fn preamble_hash_line(hash: &crate::Oid, decoration: Option<&str>, value_width: usize) -> Line { |
|
| 864 | - | let hex = Line::new(Label::new(hash.as_str()).fg(colors::rgb::ORANGE)); |
|
| 865 | - | let Some(decoration) = decoration else { |
|
| 866 | - | return hex; |
|
| 867 | - | }; |
|
| 868 | - | HStack::default() |
|
| 869 | - | .child(hex) |
|
| 870 | - | .spacer() |
|
| 871 | - | .child(Line::new(Label::new(decoration).fg(colors::rgb::DIM))) |
|
| 872 | - | .to_line(Constraint::tight(value_width)) |
|
| 873 | - | } |
|
| 874 | - | ||
| 875 | - | /// Styles one non-hash commit header value. |
|
| 876 | - | fn preamble_header_line(key: &str, value: &str) -> Line { |
|
| 877 | - | let line = Line::new(value.to_owned()); |
|
| 878 | - | if key.eq_ignore_ascii_case("date") { |
|
| 879 | - | line.style(Style::new(colors::rgb::DIM)) |
|
| 880 | - | } else { |
|
| 881 | - | line |
|
| 882 | - | } |
|
| 883 | - | } |
|
| 884 | - | ||
| 885 | 936 | /// Returns sticky file-header rows for the current offset. |
|
| 886 | 937 | fn sticky_rows(&self, scroll: usize, visible_rows: usize, cols: usize) -> Vec<Line> { |
|
| 938 | + | // Build the fixed sticky header strip directly from current scroll context. |
|
| 887 | 939 | self.sticky_start(scroll, visible_rows) |
|
| 888 | 940 | .map(|start| { |
|
| 889 | 941 | (0..STICKY_HEADER_ROWS) |
|
| 890 | 942 | .map(|index| { |
|
| 891 | 943 | if index == STICKY_HEADER_ROWS - 1 { |
|
| 892 | - | Self::sticky_divider(cols) |
|
| 944 | + | FILE_BORDER.divider(cols.saturating_sub(2), STICKY_DIVIDER) |
|
| 893 | 945 | } else { |
|
| 894 | 946 | self.render_row(start + index, cols) |
|
| 895 | 947 | .map(|row| row.line) |
|
| 896 | 948 | .unwrap_or_default() |
|
| 897 | 949 | } |
| 1076 | 1128 | LINE_NUMBER_FG, |
|
| 1077 | 1129 | self.options.diff_palette.positive_row_bg, |
|
| 1078 | 1130 | ), |
|
| 1079 | 1131 | Self::marker_cell(" + ", self.options.diff_palette.positive_fg), |
|
| 1080 | 1132 | ], |
|
| 1081 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 1133 | + | self.rendered_source(&meta, source_cols), |
|
| 1082 | 1134 | Style::default().bg(self.options.diff_palette.positive_row_bg), |
|
| 1083 | 1135 | Style::default().bg(self.options.diff_palette.positive_row_bg), |
|
| 1084 | 1136 | ), |
|
| 1085 | 1137 | DiffLineKind::Deletion => ( |
|
| 1086 | 1138 | [ |
| 1090 | 1142 | self.options.diff_palette.negative_row_bg, |
|
| 1091 | 1143 | ), |
|
| 1092 | 1144 | Self::blank_number(self.options.diff_palette.negative_row_bg), |
|
| 1093 | 1145 | Self::marker_cell(" - ", self.options.diff_palette.negative_fg), |
|
| 1094 | 1146 | ], |
|
| 1095 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 1147 | + | self.rendered_source(&meta, source_cols), |
|
| 1096 | 1148 | Style::default().bg(self.options.diff_palette.negative_row_bg), |
|
| 1097 | 1149 | Style::default() |
|
| 1098 | 1150 | .bg(self.options.diff_palette.negative_row_bg) |
|
| 1099 | 1151 | .dim(), |
|
| 1100 | 1152 | ), |
| 1102 | 1154 | [ |
|
| 1103 | 1155 | Self::number_cell(old_line, LINE_NUMBER_FG, Color::Unset), |
|
| 1104 | 1156 | Self::number_cell(meta.source_line, LINE_NUMBER_FG, Color::Unset), |
|
| 1105 | 1157 | Self::marker_cell(" ", Color::Unset), |
|
| 1106 | 1158 | ], |
|
| 1107 | - | self.highlighted_or_plain(&meta, source_cols), |
|
| 1159 | + | self.rendered_source(&meta, source_cols), |
|
| 1108 | 1160 | Style::default(), |
|
| 1109 | 1161 | Style::default(), |
|
| 1110 | 1162 | ), |
|
| 1111 | 1163 | DiffLineKind::NoNewline => ( |
|
| 1112 | 1164 | [ |
| 1135 | 1187 | source_cols: usize, |
|
| 1136 | 1188 | row_style: Style, |
|
| 1137 | 1189 | source_style: Style, |
|
| 1138 | 1190 | ) -> Line { |
|
| 1139 | 1191 | let inner = source_cols.saturating_add(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH); |
|
| 1192 | + | ||
| 1140 | 1193 | Line::truncate(&mut source, source_cols, "โฆ"); |
|
| 1141 | 1194 | Line::pad(&mut source, source_cols); |
|
| 1195 | + | ||
| 1142 | 1196 | let source = source.style(source_style); |
|
| 1143 | 1197 | let mut line = Line::blank() |
|
| 1144 | 1198 | .extend(cells[0].clone()) |
|
| 1145 | 1199 | .extend(cells[1].clone()) |
|
| 1146 | 1200 | .extend(cells[2].clone()) |
|
| 1147 | 1201 | .extend(source); |
|
| 1202 | + | ||
| 1148 | 1203 | Line::pad(&mut line, inner); |
|
| 1149 | 1204 | Line::truncate(&mut line, inner, "โฆ"); |
|
| 1150 | 1205 | line.style(row_style) |
|
| 1151 | 1206 | } |
|
| 1152 | 1207 | ||
| 1153 | - | /// Returns a highlighted line from cache or plain text when no result has arrived. |
|
| 1154 | - | fn highlighted_or_plain(&self, meta: &DiffLineMeta, cols: usize) -> Line { |
|
| 1155 | - | let key = HighlightKey::new(HighlightKeyInput { |
|
| 1156 | - | file: meta.file, |
|
| 1157 | - | hunk: meta.hunk, |
|
| 1158 | - | row: meta.row, |
|
| 1159 | - | side: meta.side, |
|
| 1160 | - | source_line: meta.source_line, |
|
| 1161 | - | text: &meta.text, |
|
| 1162 | - | syntax: self.options.syntax.as_deref(), |
|
| 1163 | - | theme: &self.options.theme, |
|
| 1164 | - | }); |
|
| 1208 | + | /// Returns the final source-text `Line` for a row, ready to render. |
|
| 1209 | + | /// |
|
| 1210 | + | /// Cached entries already include the word-diff overlay (applied once in |
|
| 1211 | + | /// [`Self::apply_highlights`]). On a cache miss -- the window before the |
|
| 1212 | + | /// highlight worker responds -- a plain Line is returned; the word-diff |
|
| 1213 | + | /// background lands once the cached overlaid line arrives. |
|
| 1214 | + | fn rendered_source(&self, meta: &DiffLineMeta, cols: usize) -> Line { |
|
| 1165 | 1215 | self.highlights |
|
| 1166 | - | .get(&key) |
|
| 1216 | + | .get(&self.key_for(meta)) |
|
| 1167 | 1217 | .cloned() |
|
| 1168 | 1218 | .unwrap_or_else(|| Line::new(Text::truncate(meta.text.as_str(), cols, "โฆ"))) |
|
| 1169 | 1219 | } |
|
| 1170 | 1220 | ||
| 1171 | 1221 | /// Returns a line-number cell. |
| 1182 | 1232 | /// Returns a marker cell. |
|
| 1183 | 1233 | fn marker_cell(marker: &str, foreground: Color) -> Line { |
|
| 1184 | 1234 | Line::new(Label::new(marker).fg(foreground)).padded(MARKER_WIDTH) |
|
| 1185 | 1235 | } |
|
| 1186 | 1236 | ||
| 1187 | - | /// Returns the border for a file card. |
|
| 1188 | - | fn file_border() -> tui::Border { |
|
| 1189 | - | tui::Border::new(colors::FAINT) |
|
| 1190 | - | } |
|
| 1191 | - | ||
| 1192 | - | /// Returns the compact marker row between hunks in one file. |
|
| 1193 | - | fn hunk_gap_line() -> Line { |
|
| 1194 | - | Line::blank() |
|
| 1195 | - | .extend(Line::blank().padded(LINE_NUMBER_WIDTH)) |
|
| 1196 | - | .extend(Line::blank().padded(LINE_NUMBER_WIDTH)) |
|
| 1197 | - | .extend(Self::marker_cell(HUNK_GAP_MARKER_CELL, colors::rgb::DIM)) |
|
| 1198 | - | } |
|
| 1199 | - | ||
| 1200 | - | /// Returns a sticky divider line that matches a bordered file card's width. |
|
| 1201 | - | fn sticky_divider(cols: usize) -> Line { |
|
| 1202 | - | Self::file_border().divider(cols.saturating_sub(2), STICKY_DIVIDER) |
|
| 1203 | - | } |
|
| 1204 | - | ||
| 1205 | 1237 | /// Returns the diff scrollbar style. |
|
| 1206 | 1238 | fn scrollbar_style() -> ScrollbarStyle { |
|
| 1207 | 1239 | ScrollbarStyle::new( |
|
| 1208 | 1240 | Cell::new(SCROLLBAR_TRACK, Style::default().fg(colors::FAINT)), |
|
| 1209 | 1241 | Cell::new(SCROLLBAR_THUMB, Style::default().fg(SCROLLBAR_THUMB_COLOR)), |
| 1252 | 1284 | File, |
|
| 1253 | 1285 | /// Hunk targets only. |
|
| 1254 | 1286 | Hunk, |
|
| 1255 | 1287 | } |
|
| 1256 | 1288 | ||
| 1257 | - | /// Local row of the first hunk body line (top border + file header + divider). |
|
| 1258 | - | const HUNK_BODY_START: usize = 3; |
|
| 1259 | - | /// Total card height when the file has no visible body (just borders + header). |
|
| 1260 | - | const EMPTY_CARD_ROWS: usize = 3; |
|
| 1261 | - | ||
| 1262 | 1289 | impl FileDiff { |
|
| 1263 | 1290 | /// Returns whether this file card should render hunk rows. |
|
| 1264 | 1291 | fn has_body(&self, collapsed: bool) -> bool { |
|
| 1265 | 1292 | !collapsed && !self.hunks.is_empty() |
|
| 1266 | 1293 | } |
| 1309 | 1336 | .clone() |
|
| 1310 | 1337 | .or_else(|| self.header.old_path.clone()), |
|
| 1311 | 1338 | } |
|
| 1312 | 1339 | } |
|
| 1313 | 1340 | ||
| 1314 | - | /// Returns source-side text assembled from one hunk. |
|
| 1315 | - | fn highlight_source(&self, hunk: HunkId, side: DiffSide) -> HighlightSource { |
|
| 1316 | - | self.hunks |
|
| 1317 | - | .iter() |
|
| 1318 | - | .find(|candidate| candidate.id == hunk) |
|
| 1319 | - | .map(|hunk| hunk.highlight_source(side)) |
|
| 1320 | - | .unwrap_or_else(|| HighlightSource::new("", 1)) |
|
| 1321 | - | } |
|
| 1322 | - | ||
| 1323 | 1341 | /// Returns a file header line. |
|
| 1324 | 1342 | fn header_line(&self, cols: usize, palette: DiffPalette) -> Line { |
|
| 1325 | 1343 | let mut left = Line::blank(); |
|
| 1326 | 1344 | left.push(Label::from_char(self.header.kind.marker()).fg(LINE_NUMBER_FG)); |
|
| 1327 | 1345 | left.push(Label::space()); |
| 1410 | 1428 | fn display_path(path: Option<&PathBuf>) -> String { |
|
| 1411 | 1429 | path.map_or_else(|| "<unknown>".to_owned(), |path| path.display().to_string()) |
|
| 1412 | 1430 | } |
|
| 1413 | 1431 | } |
|
| 1414 | 1432 | ||
| 1415 | - | impl crate::Hunk { |
|
| 1416 | - | /// Returns the starting line for a source side. |
|
| 1417 | - | fn source_start(&self, side: DiffSide) -> u32 { |
|
| 1418 | - | match side { |
|
| 1419 | - | DiffSide::Old => self.header.old_line_no, |
|
| 1420 | - | DiffSide::New => self.header.new_line_no, |
|
| 1421 | - | } |
|
| 1422 | - | } |
|
| 1423 | - | ||
| 1424 | - | /// Returns source-side text assembled from this hunk. |
|
| 1425 | - | fn highlight_source(&self, side: DiffSide) -> HighlightSource { |
|
| 1426 | - | let mut text = String::new(); |
|
| 1427 | - | ||
| 1428 | - | for line in &self.lines { |
|
| 1429 | - | if line.kind.appears_on(side) { |
|
| 1430 | - | text.push_str(&line.text); |
|
| 1431 | - | text.push('\n'); |
|
| 1432 | - | } |
|
| 1433 | - | } |
|
| 1434 | - | HighlightSource::new(text, self.source_start(side)) |
|
| 1435 | - | } |
|
| 1436 | - | } |
|
| 1437 | - | ||
| 1438 | - | impl DiffLineKind { |
|
| 1439 | - | /// Returns whether this line exists on a source side. |
|
| 1440 | - | fn appears_on(self, side: DiffSide) -> bool { |
|
| 1441 | - | match (self, side) { |
|
| 1442 | - | (Self::Addition, DiffSide::New) |
|
| 1443 | - | | (Self::Deletion, DiffSide::Old) |
|
| 1444 | - | | (Self::Context, _) => true, |
|
| 1445 | - | (Self::Addition, DiffSide::Old) |
|
| 1446 | - | | (Self::Deletion, DiffSide::New) |
|
| 1447 | - | | (Self::NoNewline, _) => false, |
|
| 1448 | - | } |
|
| 1449 | - | } |
|
| 1450 | - | } |
|
| 1451 | - | ||
| 1452 | 1433 | impl DiffLineMeta { |
|
| 1453 | 1434 | /// Creates metadata for a source row. |
|
| 1454 | 1435 | fn new(file: &FileDiff, hunk: HunkId, line: &DiffLine, old_line: u32, new_line: u32) -> Self { |
|
| 1455 | 1436 | let side = match line.kind { |
|
| 1456 | 1437 | DiffLineKind::Deletion => DiffSide::Old, |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
crates/diff/src/themes/rustik-catppuccin-mocha.json
+2 -2
| 82 | 82 | "debugToolBar.border": "#00000000", |
|
| 83 | 83 | "descriptionForeground": "#cdd6f4", |
|
| 84 | 84 | "diffEditor.border": "#585b70", |
|
| 85 | 85 | "diffEditor.diagonalFill": "#585b7099", |
|
| 86 | 86 | "diffEditor.insertedLineBackground": "#a6e3a126", |
|
| 87 | - | "diffEditor.insertedTextBackground": "#a6e3a133", |
|
| 87 | + | "diffEditor.insertedTextBackground": "#a6e3a144", |
|
| 88 | 88 | "diffEditor.removedLineBackground": "#f38ba826", |
|
| 89 | - | "diffEditor.removedTextBackground": "#f38ba833", |
|
| 89 | + | "diffEditor.removedTextBackground": "#f38ba844", |
|
| 90 | 90 | "diffEditorOverview.insertedForeground": "#a6e3a1cc", |
|
| 91 | 91 | "diffEditorOverview.removedForeground": "#f38ba8cc", |
|
| 92 | 92 | "disabledForeground": "#a6adc8", |
|
| 93 | 93 | "dropdown.background": "#181825", |
|
| 94 | 94 | "dropdown.border": "#cba6f7", |
crates/diff/src/word.rs
added
+273 -0
| 1 | + | //! Per-pair intra-line word diff. |
|
| 2 | + | //! |
|
| 3 | + | //! Walks each hunk's change blocks (consecutive `-`/`+` runs separated by |
|
| 4 | + | //! context lines) and pairs the i-th deletion with the i-th addition in the |
|
| 5 | + | //! same block. For each pair, the line texts are tokenized and an LCS is |
|
| 6 | + | //! computed to find the byte ranges that differ between them. |
|
| 7 | + | //! |
|
| 8 | + | //! A similarity gate filters out pairs whose lines share too few tokens -- |
|
| 9 | + | //! those are two distinct edits that happen to be adjacent, not two versions |
|
| 10 | + | //! of the same line. Highlighting their internal differences only produces |
|
| 11 | + | //! noise, so the overlay is suppressed and the dim row tint stands alone. |
|
| 12 | + | //! |
|
| 13 | + | //! The same gate naturally handles pure-addition and pure-deletion runs: |
|
| 14 | + | //! no pairs form, so no overlay is emitted. |
|
| 15 | + | ||
| 16 | + | use std::collections::BTreeMap; |
|
| 17 | + | use std::ops::Range; |
|
| 18 | + | ||
| 19 | + | use crate::{DiffLine, DiffLineKind, Hunk, RowId}; |
|
| 20 | + | ||
| 21 | + | /// Numerator of the minimum LCS-match ratio. A pair must match at least |
|
| 22 | + | /// `SIMILARITY_NUMERATOR / SIMILARITY_DENOMINATOR` of the longer line's |
|
| 23 | + | /// tokens or it is treated as two unrelated edits and skipped. |
|
| 24 | + | const SIMILARITY_NUMERATOR: usize = 1; |
|
| 25 | + | /// Denominator of the minimum LCS-match ratio. See [`SIMILARITY_NUMERATOR`]. |
|
| 26 | + | const SIMILARITY_DENOMINATOR: usize = 2; |
|
| 27 | + | ||
| 28 | + | /// Byte ranges per row that mark changed spans inside a hunk. |
|
| 29 | + | pub type WordChanges = BTreeMap<RowId, Vec<Range<usize>>>; |
|
| 30 | + | ||
| 31 | + | /// A single token taken from one diff line. |
|
| 32 | + | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 33 | + | struct Token<'a> { |
|
| 34 | + | /// Row this token came from. |
|
| 35 | + | row: RowId, |
|
| 36 | + | /// Byte range within the row's text. |
|
| 37 | + | range: Range<usize>, |
|
| 38 | + | /// Token text (a slice of the row's text). |
|
| 39 | + | text: &'a str, |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Token category, used to decide where a token boundary falls. |
|
| 43 | + | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 44 | + | enum Class { |
|
| 45 | + | /// Run of `[A-Za-z0-9_]`. |
|
| 46 | + | Ident, |
|
| 47 | + | /// Run of `' '` or `'\t'`. |
|
| 48 | + | Space, |
|
| 49 | + | /// Any other character (one per token). |
|
| 50 | + | Other, |
|
| 51 | + | } |
|
| 52 | + | ||
| 53 | + | impl Class { |
|
| 54 | + | /// Classifies one character. |
|
| 55 | + | fn of(ch: char) -> Self { |
|
| 56 | + | if ch.is_ascii_alphanumeric() || ch == '_' { |
|
| 57 | + | Self::Ident |
|
| 58 | + | } else if ch == ' ' || ch == '\t' { |
|
| 59 | + | Self::Space |
|
| 60 | + | } else { |
|
| 61 | + | Self::Other |
|
| 62 | + | } |
|
| 63 | + | } |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Iterator over tokens in one diff line's text. |
|
| 67 | + | struct Tokens<'a> { |
|
| 68 | + | /// Row identifier carried into each yielded token. |
|
| 69 | + | row: RowId, |
|
| 70 | + | /// Full line text being tokenized. |
|
| 71 | + | text: &'a str, |
|
| 72 | + | /// Byte cursor into the line text. |
|
| 73 | + | cursor: usize, |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | impl<'a> Iterator for Tokens<'a> { |
|
| 77 | + | type Item = Token<'a>; |
|
| 78 | + | ||
| 79 | + | fn next(&mut self) -> Option<Self::Item> { |
|
| 80 | + | let rest = self.text.get(self.cursor..)?; |
|
| 81 | + | let mut chars = rest.char_indices(); |
|
| 82 | + | let (_, first) = chars.next()?; |
|
| 83 | + | let class = Class::of(first); |
|
| 84 | + | let end_local = if matches!(class, Class::Ident | Class::Space) { |
|
| 85 | + | chars |
|
| 86 | + | .find(|(_, ch)| Class::of(*ch) != class) |
|
| 87 | + | .map_or(rest.len(), |(i, _)| i) |
|
| 88 | + | } else { |
|
| 89 | + | first.len_utf8() |
|
| 90 | + | }; |
|
| 91 | + | let start = self.cursor; |
|
| 92 | + | let end = self.cursor + end_local; |
|
| 93 | + | self.cursor = end; |
|
| 94 | + | ||
| 95 | + | Some(Token { |
|
| 96 | + | row: self.row, |
|
| 97 | + | range: start..end, |
|
| 98 | + | text: &self.text[start..end], |
|
| 99 | + | }) |
|
| 100 | + | } |
|
| 101 | + | } |
|
| 102 | + | ||
| 103 | + | impl Tokens<'_> { |
|
| 104 | + | /// Returns an iterator over tokens in one diff line. |
|
| 105 | + | fn new(line: &DiffLine) -> Tokens<'_> { |
|
| 106 | + | Tokens { |
|
| 107 | + | row: line.id, |
|
| 108 | + | text: line.text.as_str(), |
|
| 109 | + | cursor: 0, |
|
| 110 | + | } |
|
| 111 | + | } |
|
| 112 | + | } |
|
| 113 | + | ||
| 114 | + | /// Returns the per-row changed byte ranges for one hunk. |
|
| 115 | + | pub fn compute(hunk: &Hunk) -> WordChanges { |
|
| 116 | + | let mut changes = WordChanges::new(); |
|
| 117 | + | let mut dels: Vec<&DiffLine> = Vec::new(); |
|
| 118 | + | let mut adds: Vec<&DiffLine> = Vec::new(); |
|
| 119 | + | for line in &hunk.lines { |
|
| 120 | + | match line.kind { |
|
| 121 | + | DiffLineKind::Deletion => dels.push(line), |
|
| 122 | + | DiffLineKind::Addition => adds.push(line), |
|
| 123 | + | DiffLineKind::Context => { |
|
| 124 | + | flush_block(&mut dels, &mut adds, &mut changes); |
|
| 125 | + | } |
|
| 126 | + | DiffLineKind::NoNewline => {} |
|
| 127 | + | } |
|
| 128 | + | } |
|
| 129 | + | flush_block(&mut dels, &mut adds, &mut changes); |
|
| 130 | + | ||
| 131 | + | // The LCS walks back-to-front, so per-row ranges arrive in reverse order; |
|
| 132 | + | // sort and collapse adjacent ranges into clean spans. |
|
| 133 | + | for ranges in changes.values_mut() { |
|
| 134 | + | ranges.sort_by_key(|range| range.start); |
|
| 135 | + | ranges.dedup_by(|next, prev| { |
|
| 136 | + | if prev.end >= next.start { |
|
| 137 | + | prev.end = prev.end.max(next.end); |
|
| 138 | + | true |
|
| 139 | + | } else { |
|
| 140 | + | false |
|
| 141 | + | } |
|
| 142 | + | }); |
|
| 143 | + | } |
|
| 144 | + | changes |
|
| 145 | + | } |
|
| 146 | + | ||
| 147 | + | /// Pairs up the deletions and additions in one change block and emits ranges |
|
| 148 | + | /// for each pair that passes the similarity gate. Clears both buffers so the |
|
| 149 | + | /// next block starts fresh. |
|
| 150 | + | fn flush_block(dels: &mut Vec<&DiffLine>, adds: &mut Vec<&DiffLine>, changes: &mut WordChanges) { |
|
| 151 | + | let pairs = dels.len().min(adds.len()); |
|
| 152 | + | for i in 0..pairs { |
|
| 153 | + | lcs(dels[i], adds[i], changes); |
|
| 154 | + | } |
|
| 155 | + | dels.clear(); |
|
| 156 | + | adds.clear(); |
|
| 157 | + | } |
|
| 158 | + | ||
| 159 | + | /// Runs an LCS between one deletion and one addition line. If the pair shares |
|
| 160 | + | /// enough tokens to look like two edits of the same line, the differing byte |
|
| 161 | + | /// ranges on each side are pushed into `changes`. Otherwise the pair is |
|
| 162 | + | /// treated as unrelated and skipped. |
|
| 163 | + | fn lcs(del: &DiffLine, add: &DiffLine, changes: &mut WordChanges) { |
|
| 164 | + | let old: Vec<Token<'_>> = Tokens::new(del).collect(); |
|
| 165 | + | let new: Vec<Token<'_>> = Tokens::new(add).collect(); |
|
| 166 | + | if old.is_empty() || new.is_empty() { |
|
| 167 | + | return; |
|
| 168 | + | } |
|
| 169 | + | ||
| 170 | + | // Build the standard LCS length table over token texts. |
|
| 171 | + | let rows = old.len() + 1; |
|
| 172 | + | let cols = new.len() + 1; |
|
| 173 | + | let mut table = vec![vec![0_u32; cols]; rows]; |
|
| 174 | + | for i in 1..rows { |
|
| 175 | + | for j in 1..cols { |
|
| 176 | + | table[i][j] = if old[i - 1].text == new[j - 1].text { |
|
| 177 | + | table[i - 1][j - 1] + 1 |
|
| 178 | + | } else { |
|
| 179 | + | table[i - 1][j].max(table[i][j - 1]) |
|
| 180 | + | }; |
|
| 181 | + | } |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | // Similarity gate: the LCS length must cover at least the configured |
|
| 185 | + | // fraction of the longer line, or the lines are too different to usefully |
|
| 186 | + | // word-diff against each other. |
|
| 187 | + | let common = table[old.len()][new.len()] as usize; |
|
| 188 | + | let max_len = old.len().max(new.len()); |
|
| 189 | + | if common * SIMILARITY_DENOMINATOR < max_len * SIMILARITY_NUMERATOR { |
|
| 190 | + | return; |
|
| 191 | + | } |
|
| 192 | + | ||
| 193 | + | // Walk the table backwards from the bottom-right corner. Each step that |
|
| 194 | + | // advances along only one axis is an old-only or new-only token, whose |
|
| 195 | + | // byte range we record under its source row. |
|
| 196 | + | let mut i = old.len(); |
|
| 197 | + | let mut j = new.len(); |
|
| 198 | + | while i > 0 && j > 0 { |
|
| 199 | + | if old[i - 1].text == new[j - 1].text { |
|
| 200 | + | i -= 1; |
|
| 201 | + | j -= 1; |
|
| 202 | + | } else if table[i - 1][j] >= table[i][j - 1] { |
|
| 203 | + | i -= 1; |
|
| 204 | + | changes |
|
| 205 | + | .entry(old[i].row) |
|
| 206 | + | .or_default() |
|
| 207 | + | .push(old[i].range.clone()); |
|
| 208 | + | } else { |
|
| 209 | + | j -= 1; |
|
| 210 | + | changes |
|
| 211 | + | .entry(new[j].row) |
|
| 212 | + | .or_default() |
|
| 213 | + | .push(new[j].range.clone()); |
|
| 214 | + | } |
|
| 215 | + | } |
|
| 216 | + | while i > 0 { |
|
| 217 | + | i -= 1; |
|
| 218 | + | changes |
|
| 219 | + | .entry(old[i].row) |
|
| 220 | + | .or_default() |
|
| 221 | + | .push(old[i].range.clone()); |
|
| 222 | + | } |
|
| 223 | + | while j > 0 { |
|
| 224 | + | j -= 1; |
|
| 225 | + | changes |
|
| 226 | + | .entry(new[j].row) |
|
| 227 | + | .or_default() |
|
| 228 | + | .push(new[j].range.clone()); |
|
| 229 | + | } |
|
| 230 | + | } |
|
| 231 | + | ||
| 232 | + | #[cfg(test)] |
|
| 233 | + | #[allow(clippy::unwrap_used)] |
|
| 234 | + | mod tests { |
|
| 235 | + | use super::*; |
|
| 236 | + | use crate::{Decode, DiffDocument}; |
|
| 237 | + | ||
| 238 | + | fn parse(input: &str) -> DiffDocument { |
|
| 239 | + | DiffDocument::decode_str(input).unwrap() |
|
| 240 | + | } |
|
| 241 | + | ||
| 242 | + | fn tokens(line: &DiffLine) -> Vec<&str> { |
|
| 243 | + | Tokens::new(line).map(|t| t.text).collect() |
|
| 244 | + | } |
|
| 245 | + | ||
| 246 | + | #[test] |
|
| 247 | + | fn tokenizer_splits_idents_whitespace_and_punctuation() { |
|
| 248 | + | let line = DiffLine { |
|
| 249 | + | id: RowId::new(0), |
|
| 250 | + | kind: DiffLineKind::Context, |
|
| 251 | + | text: "foo bar+baz".to_owned(), |
|
| 252 | + | }; |
|
| 253 | + | assert_eq!(tokens(&line), vec!["foo", " ", "bar", "+", "baz"]); |
|
| 254 | + | } |
|
| 255 | + | ||
| 256 | + | #[test] |
|
| 257 | + | fn tokenizer_records_byte_ranges() { |
|
| 258 | + | let line = DiffLine { |
|
| 259 | + | id: RowId::new(0), |
|
| 260 | + | kind: DiffLineKind::Context, |
|
| 261 | + | text: "ab cd".to_owned(), |
|
| 262 | + | }; |
|
| 263 | + | let collected: Vec<_> = Tokens::new(&line).collect(); |
|
| 264 | + | assert_eq!(collected[0].range, 0..2); |
|
| 265 | + | assert_eq!(collected[1].range, 2..3); |
|
| 266 | + | assert_eq!(collected[2].range, 3..5); |
|
| 267 | + | } |
|
| 268 | + | ||
| 269 | + | #[test] |
|
| 270 | + | fn similar_lines_get_word_diff() { |
|
| 271 | + | let doc = parse( |
|
| 272 | + | "\ |
|
| 273 | + |
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
crates/tui-core/src/colors.rs
+5 -4
| 11 | 11 | ||
| 12 | 12 | /// RGB (24-bit) colors supported by modern terminals. |
|
| 13 | 13 | pub mod rgb { |
|
| 14 | 14 | use super::Color; |
|
| 15 | 15 | ||
| 16 | - | /// Background color for removed or failing content. |
|
| 17 | - | pub const NEGATIVE: Color = Color::RGB(60, 10, 20); |
|
| 18 | - | /// Background color for added or successful content. |
|
| 19 | - | pub const POSITIVE: Color = Color::RGB(10, 60, 20); |
|
| 16 | + | /// Background color for removed or failing content. Sits between |
|
| 17 | + | /// [`NEGATIVE_DARK`] and [`NEGATIVE_LIGHT`]. |
|
| 18 | + | pub const NEGATIVE: Color = Color::RGB(95, 25, 45); |
|
| 19 | + | /// Background color for added or successful content. See [`NEGATIVE`]. |
|
| 20 | + | pub const POSITIVE: Color = Color::RGB(25, 95, 45); |
|
| 20 | 21 | /// Darker negative background color. |
|
| 21 | 22 | pub const NEGATIVE_DARK: Color = Color::RGB(30, 10, 20); |
|
| 22 | 23 | /// Darker positive background color. |
|
| 23 | 24 | pub const POSITIVE_DARK: Color = Color::RGB(10, 30, 20); |
|
| 24 | 25 | /// Lighter negative foreground color. |
crates/tui-core/src/primitive.rs
+96 -0
| 3 | 3 | //! `Constraint`, `Size`, and `Line` are the shared building blocks most widgets |
|
| 4 | 4 | //! depend on. This module keeps text-line composition and basic widget sizing |
|
| 5 | 5 | //! close together because width-aware rendering needs both. |
|
| 6 | 6 | ||
| 7 | 7 | use std::fmt; |
|
| 8 | + | use std::ops::Range; |
|
| 8 | 9 | use std::vec; |
|
| 9 | 10 | ||
| 10 | 11 | use super::cell::Text; |
|
| 11 | 12 | use crate::widget::Widget; |
|
| 12 | 13 |
| 109 | 110 | }) |
|
| 110 | 111 | .collect(), |
|
| 111 | 112 | } |
|
| 112 | 113 | } |
|
| 113 | 114 | ||
| 115 | + | /// Returns this line with `style` overlaid on the byte ranges in `ranges`. |
|
| 116 | + | /// |
|
| 117 | + | /// Labels are split at range boundaries; in-range sub-labels are styled as |
|
| 118 | + | /// `style.merge(original)` (see [`Style::merge`]), and out-of-range parts |
|
| 119 | + | /// keep their original style. Byte indices are counted across the line's |
|
| 120 | + | /// labels in order. |
|
| 121 | + | /// |
|
| 122 | + | /// `ranges` must be sorted and disjoint. Indices that fall outside any |
|
| 123 | + | /// label are ignored. |
|
| 124 | + | pub fn overlay_ranges(self, ranges: &[Range<usize>], style: Style) -> Self { |
|
| 125 | + | if ranges.is_empty() { |
|
| 126 | + | return self; |
|
| 127 | + | } |
|
| 128 | + | let mut out: Vec<Label> = Vec::new(); |
|
| 129 | + | let mut ranges = ranges.iter().peekable(); |
|
| 130 | + | let mut cursor: usize = 0; |
|
| 131 | + | ||
| 132 | + | for label in self.items { |
|
| 133 | + | let content = label.content(); |
|
| 134 | + | if content.is_empty() { |
|
| 135 | + | out.push(label); |
|
| 136 | + | continue; |
|
| 137 | + | } |
|
| 138 | + | let label_start = cursor; |
|
| 139 | + | let label_end = cursor + content.len(); |
|
| 140 | + | cursor = label_end; |
|
| 141 | + | ||
| 142 | + | let mut pos = label_start; |
|
| 143 | + | while pos < label_end { |
|
| 144 | + | while ranges.peek().is_some_and(|range| range.end <= pos) { |
|
| 145 | + | ranges.next(); |
|
| 146 | + | } |
|
| 147 | + | let (next, in_range) = match ranges.peek() { |
|
| 148 | + | Some(range) if range.start <= pos => (range.end.min(label_end), true), |
|
| 149 | + | Some(range) => (range.start.min(label_end), false), |
|
| 150 | + | None => (label_end, false), |
|
| 151 | + | }; |
|
| 152 | + | let sub_start = pos - label_start; |
|
| 153 | + | let sub_end = next - label_start; |
|
| 154 | + | ||
| 155 | + | if let Some(text) = content.get(sub_start..sub_end) { |
|
| 156 | + | let sub_style = if in_range { |
|
| 157 | + | style.merge(label.paint.style) |
|
| 158 | + | } else { |
|
| 159 | + | label.paint.style |
|
| 160 | + | }; |
|
| 161 | + | out.push(Label::raw(text.to_owned(), sub_style)); |
|
| 162 | + | } |
|
| 163 | + | pos = next; |
|
| 164 | + | } |
|
| 165 | + | } |
|
| 166 | + | Self { items: out } |
|
| 167 | + | } |
|
| 168 | + | ||
| 114 | 169 | /// Returns a line with a single space between the given labels. |
|
| 115 | 170 | // TODO: Use [`Iterator::intersperse`] when MSRV is updated. |
|
| 116 | 171 | pub fn spaced(items: impl IntoIterator<Item = Label>) -> Self { |
|
| 117 | 172 | let items = items.into_iter(); |
|
| 118 | 173 | let mut line = Self { |
| 388 | 443 | let line = Line::spaced([Label::new("banana"), Label::blank(), Label::new("apple")]); |
|
| 389 | 444 | ||
| 390 | 445 | assert_eq!(line.to_string(), "banana apple"); |
|
| 391 | 446 | } |
|
| 392 | 447 | ||
| 448 | + | #[test] |
|
| 449 | + | fn overlay_ranges_splits_labels_at_boundaries() { |
|
| 450 | + | // Single label "hello world", overlay bg on bytes 0..5 ("hello"). |
|
| 451 | + | let line = Line::new(Label::new("hello world").fg(Color::Red)); |
|
| 452 | + | let overlaid = line.overlay_ranges(&[0..5], Style::default().bg(Color::Blue)); |
|
| 453 | + | let labels = overlaid.labels(); |
|
| 454 | + | assert_eq!(labels.len(), 2); |
|
| 455 | + | assert_eq!(labels[0].content(), "hello"); |
|
| 456 | + | assert_eq!(labels[0].paint.style.fg_color(), Color::Red); |
|
| 457 | + | assert_eq!(labels[0].paint.style.bg_color(), Color::Blue); |
|
| 458 | + | assert_eq!(labels[1].content(), " world"); |
|
| 459 | + | assert_eq!(labels[1].paint.style.fg_color(), Color::Red); |
|
| 460 | + | assert_eq!(labels[1].paint.style.bg_color(), Color::Unset); |
|
| 461 | + | } |
|
| 462 | + | ||
| 463 | + | #[test] |
|
| 464 | + | fn overlay_ranges_spans_label_boundaries() { |
|
| 465 | + | // Range crosses two labels: "ab" + "cd", overlay 1..3 โ covers "b"+"c". |
|
| 466 | + | let line = Line::default() |
|
| 467 | + | .item(Label::new("ab")) |
|
| 468 | + | .item(Label::new("cd")); |
|
| 469 | + | let overlaid = line.overlay_ranges(&[1..3], Style::default().bg(Color::Green)); |
|
| 470 | + | let labels = overlaid.labels(); |
|
| 471 | + | let text: Vec<_> = labels.iter().map(|l| l.content()).collect(); |
|
| 472 | + | let bgs: Vec<_> = labels.iter().map(|l| l.paint.style.bg_color()).collect(); |
|
| 473 | + | assert_eq!(text, ["a", "b", "c", "d"]); |
|
| 474 | + | assert_eq!( |
|
| 475 | + | bgs, |
|
| 476 | + | [Color::Unset, Color::Green, Color::Green, Color::Unset] |
|
| 477 | + | ); |
|
| 478 | + | } |
|
| 479 | + | ||
| 480 | + | #[test] |
|
| 481 | + | fn overlay_ranges_empty_returns_unchanged() { |
|
| 482 | + | let line = Line::new(Label::new("hello")); |
|
| 483 | + | let overlaid = line.overlay_ranges(&[], Style::default().bg(Color::Blue)); |
|
| 484 | + | assert_eq!(overlaid.labels().len(), 1); |
|
| 485 | + | assert_eq!(overlaid.labels()[0].content(), "hello"); |
|
| 486 | + | assert_eq!(overlaid.labels()[0].paint.style.bg_color(), Color::Unset); |
|
| 487 | + | } |
|
| 488 | + | ||
| 393 | 489 | #[test] |
|
| 394 | 490 | fn line_styled_adapter_formats_label_styles_explicitly() { |
|
| 395 | 491 | let mut line = |
|
| 396 | 492 | Line::new(Label::new("x").fg(Color::Red)).item(Label::new("y").fg(Color::Blue)); |
|
| 397 | 493 |