tui-core: Fix line truncation bug

42ec5b90886030a92c3053544d0b80a90a083a35
Alexis Sellier committed ago 1 parent 5207d7b5
crates/diff/src/viewer.rs +23 -22
1504 1504
        file: &FileDiff,
1505 1505
        hunk: &crate::Hunk,
1506 1506
        line_index: usize,
1507 1507
        inner: usize,
1508 1508
    ) -> RenderRow {
1509 -
        let source_cols = inner.saturating_sub(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH);
1510 1509
        let (old_line, new_line) = Self::hunk_line_numbers(hunk, line_index);
1511 1510
        let line = &hunk.lines[line_index];
1512 1511
        let meta = DiffLineMeta::new(file, hunk.id, line, old_line, new_line);
1513 1512
1514 -
        self.hunk_row(meta, line, old_line, source_cols)
1513 +
        self.hunk_row(meta, line, old_line, inner)
1515 1514
    }
1516 1515
1517 1516
    /// Returns source metadata for one rendered hunk body row.
1518 1517
    fn hunk_meta_at(&self, file: &FileDiff, hunk: &crate::Hunk, line_index: usize) -> DiffLineMeta {
1519 1518
        let (old_line, new_line) = Self::hunk_line_numbers(hunk, line_index);
1535 1534
    fn hunk_row(
1536 1535
        &self,
1537 1536
        meta: DiffLineMeta,
1538 1537
        line: &DiffLine,
1539 1538
        old_line: u32,
1540 -
        source_cols: usize,
1539 +
        row_cols: usize,
1541 1540
    ) -> RenderRow {
1542 1541
        let (cells, source, row_style, source_style) = match line.kind {
1543 1542
            DiffLineKind::Addition => (
1544 1543
                [
1545 1544
                    Self::blank_number(self.options.diff_palette.positive_row_bg),
1548 1547
                        LINE_NUMBER_FG,
1549 1548
                        self.options.diff_palette.positive_row_bg,
1550 1549
                    ),
1551 1550
                    Self::marker_cell(" + ", self.options.diff_palette.positive_fg),
1552 1551
                ],
1553 -
                self.rendered_source(&meta, source_cols),
1552 +
                self.rendered_source(&meta),
1554 1553
                Style::default().bg(self.options.diff_palette.positive_row_bg),
1555 1554
                Style::default().bg(self.options.diff_palette.positive_row_bg),
1556 1555
            ),
1557 1556
            DiffLineKind::Deletion => (
1558 1557
                [
1562 1561
                        self.options.diff_palette.negative_row_bg,
1563 1562
                    ),
1564 1563
                    Self::blank_number(self.options.diff_palette.negative_row_bg),
1565 1564
                    Self::marker_cell(" - ", self.options.diff_palette.negative_fg),
1566 1565
                ],
1567 -
                self.rendered_source(&meta, source_cols),
1566 +
                self.rendered_source(&meta),
1568 1567
                Style::default().bg(self.options.diff_palette.negative_row_bg),
1569 1568
                Style::default()
1570 1569
                    .bg(self.options.diff_palette.negative_row_bg)
1571 1570
                    .dim(),
1572 1571
            ),
1574 1573
                [
1575 1574
                    Self::number_cell(old_line, LINE_NUMBER_FG, Color::Unset),
1576 1575
                    Self::number_cell(meta.source_line, LINE_NUMBER_FG, Color::Unset),
1577 1576
                    Self::marker_cell("   ", Color::Unset),
1578 1577
                ],
1579 -
                self.rendered_source(&meta, source_cols),
1578 +
                self.rendered_source(&meta),
1580 1579
                Style::default(),
1581 1580
                Style::default(),
1582 1581
            ),
1583 1582
            DiffLineKind::NoNewline => (
1584 1583
                [
1589 1588
                Line::new(NO_NEWLINE_MARKER).style(Style::default().fg(colors::rgb::DIM)),
1590 1589
                Style::default(),
1591 1590
                Style::default(),
1592 1591
            ),
1593 1592
        };
1594 -
        let rendered = self.hunk_line(cells, source, source_cols, row_style, source_style);
1593 +
        let rendered = self.hunk_line(cells, source, row_cols, row_style, source_style);
1595 1594
1596 1595
        RenderRow {
1597 1596
            line: rendered,
1598 1597
            diff: Some(meta),
1599 1598
        }
1602 1601
    /// Returns a fixed-column hunk row line.
1603 1602
    fn hunk_line(
1604 1603
        &self,
1605 1604
        cells: [Line; 3],
1606 1605
        mut source: Line,
1607 -
        source_cols: usize,
1606 +
        row_cols: usize,
1608 1607
        row_style: Style,
1609 1608
        source_style: Style,
1610 1609
    ) -> Line {
1611 -
        let inner = source_cols.saturating_add(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH);
1612 -
1613 -
        Line::truncate(&mut source, source_cols, "…");
1614 -
        let source_width = source.width();
1615 -
        if source_width < source_cols {
1616 -
            let padding = " ".repeat(source_cols - source_width);
1617 -
            source.push(Label::new(&padding));
1610 +
        let source_cols = row_cols.saturating_sub(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH);
1611 +
1612 +
        if source_cols > 0 {
1613 +
            Line::truncate(&mut source, source_cols, "…");
1614 +
            let source_width = source.width();
1615 +
            if source_width < source_cols {
1616 +
                let padding = " ".repeat(source_cols - source_width);
1617 +
                source.push(Label::new(&padding));
1618 +
            }
1618 1619
        }
1619 1620
1620 1621
        let source = source.style(source_style);
1621 1622
        let mut line = Line::blank()
1622 1623
            .extend(cells[0].clone())
1623 1624
            .extend(cells[1].clone())
1624 1625
            .extend(cells[2].clone())
1625 1626
            .extend(source);
1626 1627
1627 -
        Line::pad(&mut line, inner);
1628 -
        Line::truncate(&mut line, inner, "…");
1628 +
        Line::pad(&mut line, row_cols);
1629 +
        Line::truncate(&mut line, row_cols, "…");
1629 1630
        line.style(row_style)
1630 1631
    }
1631 1632
1632 1633
    /// Returns the final source-text `Line` for a row, ready to render.
1633 1634
    ///
1634 1635
    /// Cached entries already include the word-diff overlay (applied once in
1635 1636
    /// [`Self::apply_highlights`]). On a cache miss -- the window before the
1636 1637
    /// highlight worker responds -- a plain Line is returned; the word-diff
1637 1638
    /// background lands once the cached overlaid line arrives.
1638 -
    fn rendered_source(&self, meta: &DiffLineMeta, cols: usize) -> Line {
1639 +
    fn rendered_source(&self, meta: &DiffLineMeta) -> Line {
1639 1640
        let has_tabs = meta.text.contains('\t');
1640 1641
        if let Some(line) = self.highlights.get(&self.key_for(meta)) {
1641 1642
            let line = line.clone();
1642 1643
            return if has_tabs {
1643 -
                Self::expand_source_tabs(line).truncated(cols, "…")
1644 +
                Self::expand_source_tabs(line)
1644 1645
            } else {
1645 -
                line.truncated(cols, "…")
1646 +
                line
1646 1647
            };
1647 1648
        }
1648 1649
        if has_tabs {
1649 -
            Self::expand_source_tabs(Line::new(meta.text.as_str())).truncated(cols, "…")
1650 +
            Self::expand_source_tabs(Line::new(meta.text.as_str()))
1650 1651
        } else {
1651 -
            Line::new(Text::truncate(meta.text.as_str(), cols, "…"))
1652 +
            Line::new(meta.text.as_str())
1652 1653
        }
1653 1654
    }
1654 1655
1655 1656
    /// Returns a source line with tabs rendered as fixed-width visible cells.
1656 1657
    fn expand_source_tabs(line: Line) -> Line {
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
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
src/app/a.rs +0 -0
src/lib/b.rs +0 -0
README.md +0 -0
a.rs +0 -0
b.rs +0 -0
assets/css/style.css +0 -0
assets/css/style.css +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
crates/diff/tests/golden/narrow_view_14x8.txt +6 -6
1 1
╭───────────╮▐
2 -
│ ▿ a.rs +2 │▐
2 +
│ ▿ a.rs +… │▐
3 3
├───────────┤▐
4 -
│ 1    1    │▐
5 -
│ 2         │▐
6 -
│      2    │▐
7 -
│      3    │▐
8 -
│ 3    4    │🮘
4 +
│ 1    1  … │▐
5 +
│ 2       … │▐
6 +
│      2  … │▐
7 +
│      3  … │▐
8 +
│ 3    4  … │🮘
crates/tui-core/src/primitive.rs +46 -2
217 217
        self
218 218
    }
219 219
220 220
    /// Truncate this line to the given width.
221 221
    pub fn truncate(&mut self, width: usize, delim: &str) {
222 +
        let delim_width = delim.width();
223 +
        let mut hidden_text = false;
224 +
222 225
        while self.width() > width {
223 226
            let total = self.width();
224 227
225 228
            if total - self.items.last().map_or(0, Text::width) > width {
226 -
                self.items.pop();
229 +
                if let Some(item) = self.items.pop() {
230 +
                    hidden_text |= !item.content().trim().is_empty();
231 +
                }
227 232
            } else if let Some(item) = self.items.last_mut() {
233 +
                let original = item.clone();
228 234
                *item = item.truncate(width - (total - Text::width(item)), delim);
235 +
236 +
                if !item.content().ends_with(delim) {
237 +
                    let hidden = original
238 +
                        .content()
239 +
                        .strip_prefix(item.content())
240 +
                        .unwrap_or_else(|| original.content());
241 +
                    hidden_text |= !hidden.trim().is_empty();
242 +
                }
229 243
            }
230 244
        }
245 +
246 +
        if delim.is_empty()
247 +
            || delim_width > width
248 +
            || !hidden_text
249 +
            || self
250 +
                .items
251 +
                .last()
252 +
                .is_some_and(|item| item.content().ends_with(delim))
253 +
        {
254 +
            return;
255 +
        }
256 +
        Line::truncate(self, width - delim_width, "");
257 +
        self.push(Label::new(delim));
231 258
    }
232 259
233 260
    /// Returns this line truncated to the given width.
234 261
    pub fn truncated(mut self, width: usize, delim: &str) -> Self {
235 262
        Line::truncate(&mut self, width, delim);
239 266
    /// Returns this line with one leading space and padding to the given width.
240 267
    pub fn inset(self, width: usize) -> Self {
241 268
        if width <= 2 {
242 269
            return self;
243 270
        }
244 -
245 271
        Line::new(" ").extend(self).padded(width)
246 272
    }
247 273
248 274
    /// Returns the actual column width of this line.
249 275
    pub fn width(&self) -> usize {
411 437
        let mut actual = line;
412 438
        Line::truncate(&mut actual, 0, "…");
413 439
        assert_eq!(actual.to_string(), "");
414 440
    }
415 441
442 +
    #[test]
443 +
    fn truncate_marks_hidden_text_across_labels() {
444 +
        let line = Line::default().item("foo").item(",").item(" ").item("bar");
445 +
446 +
        let mut actual = line;
447 +
        Line::truncate(&mut actual, 5, "…");
448 +
        assert_eq!(actual.to_string(), "foo,…");
449 +
    }
450 +
451 +
    #[test]
452 +
    fn truncate_omits_marker_for_hidden_trailing_space_across_labels() {
453 +
        let line = Line::default().item("foo").item("   ");
454 +
455 +
        let mut actual = line;
456 +
        Line::truncate(&mut actual, 5, "…");
457 +
        assert_eq!(actual.to_string(), "foo ");
458 +
    }
459 +
416 460
    #[test]
417 461
    fn test_width() {
418 462
        // Nb. This might not display correctly in some editors or terminals.
419 463
        let line = Line::new("Radicle Heartwood Protocol & Stack ❤️🪵");
420 464
        assert_eq!(line.width(), 39, "{line}");
crates/tui-widgets/src/panel.rs +0 -2
47 47
            visible: true,
48 48
            kind: PanelNodeKind::Leaf,
49 49
        }
50 50
    }
51 51
}
52 -
53 52
/// Layout role for a panel node.
54 53
#[derive(Clone, Debug)]
55 54
enum PanelNodeKind {
56 55
    /// Content leaf.
57 56
    Leaf,
574 573
        // Too little room for content collapses to the bare border.
575 574
        assert_eq!(Panel::frame_cols(5, 2), 2);
576 575
        assert_eq!(Panel::frame_cols(5, 1), 2);
577 576
    }
578 577
}
579 -