limn
    ↑↓ ChooseEnter OpenEsc Close
    GitHub

    Components/Data

    CodeDiff

    A read-only view of the rows of a diff: line numbers, a mark for each added or removed row, and the text. It scrolls and builds only the rows in view.

    Native componentGuide source

    Preview

    CodeSource
    fn rows() -> Arc<[CodeDiffLine]> {
        vec![
            CodeDiffLine { kind: CodeDiffLineKind::Context, old_line: Some(41), new_line: Some(41), text: "fn total() -> u32 {".into() },
            CodeDiffLine { kind: CodeDiffLineKind::Removed, old_line: Some(42), new_line: None, text: "    0".into() },
            CodeDiffLine { kind: CodeDiffLineKind::Added, old_line: None, new_line: Some(42), text: "    1".into() },
        ]
        .into()
    }
    
    fn view(state: &State, palette: ComponentPalette) -> Element {
        CodeDiff::new("review", state.rows.clone()).palette(palette).render()
    }

    Examples

    Each one is the library drawing it. Press, type and hover.

    Code diff2 / 5
    CodeSource
    framed(
        CodeDiff::new("code-diff-single", patch())
            .language(CodeLanguage::Rust)
            .single_gutter(true)
            .height(height_of(PATCH.len())),
    )
    Code diff3 / 5
    CodeSource
    framed(CodeDiff::new("code-diff-long", long).language(CodeLanguage::Rust).focusable(true).height(152.0))
    Code diff4 / 5
    CodeSource
    row(vec![
        outline(format!("code-diff-tail={}", arrived + 1), "Append a row"),
        outline(format!("code-diff-tail={}", arrived + 20), "Append 20"),
        outline("code-diff-tail=6".to_owned(), "Reset"),
    ]),
    framed(CodeDiff::new("code-diff-tail", tail).single_gutter(true).follow_tail(true).height(height_of(5)))
    Code diff5 / 5
    CodeSource
    framed(CodeDiff::new("code-diff-empty", Vec::new()).height(height_of(2)))

    CodeDiff

    A read-only view of the rows of a diff: line numbers, a mark for each added or removed row, and the text. It scrolls and builds only the rows in view.

    Import

    use limn::{CodeDiff, CodeDiffLine, CodeDiffLineKind, CodeLanguage, Component};

    Usage

    The host supplies the rows; the view never computes or applies a patch, and it shows each row’s text exactly as given. A row is a CodeDiffLine: its kind, its number on the old side and on the new side, and its text.

    fn rows() -> Arc<[CodeDiffLine]> {
        vec![
            CodeDiffLine { kind: CodeDiffLineKind::Context, old_line: Some(41), new_line: Some(41), text: "fn total() -> u32 {".into() },
            CodeDiffLine { kind: CodeDiffLineKind::Removed, old_line: Some(42), new_line: None, text: "    0".into() },
            CodeDiffLine { kind: CodeDiffLineKind::Added, old_line: None, new_line: Some(42), text: "    1".into() },
        ]
        .into()
    }
    
    fn view(state: &State, palette: ComponentPalette) -> Element {
        CodeDiff::new("review", state.rows.clone()).palette(palette).render()
    }

    Keep the rows in the state as an Arc<[CodeDiffLine]> and pass a clone of it each frame. The view colors its rows once for each allocation, so rows rebuilt every frame are colored again every frame. Without palette the diff uses the neutral dark palette.

    The diff fills the width it is given. It reports no actions.

    Height

    CodeDiff::new("review", rows).height(240.0).render()

    height is in logical pixels, never under 24; the default is 320. Rows past it scroll under the wheel or the scrollbar.

    font_size(size) changes the text, gutter and row layout together, 13 logical pixels by default. Invalid numbers and nonpositive sizes are ignored; valid sizes are kept within 1 to 96. diff_backgrounds(false) hides the full row tints while preserving the +/- marks and colored edge bands.

    Language

    CodeDiff::new("review", rows).language(CodeLanguage::Rust).render()

    language chooses how the text is colored: comments, strings, numbers and the keywords of that language. The default, CodeLanguage::Plain, colors nothing. tab_width is how many columns a tab takes, from 1 to 16; the default is 4.

    One gutter

    CodeDiff::new("captured", rows).single_gutter(true).render()

    single_gutter(true) draws one column of line numbers, the new number or else the old one, and no column of marks. Added and removed rows keep their colors.

    Focus

    CodeDiff::new("document", rows).focusable(true).render()

    A diff takes no focus by default, as a preview inside a conversation. focusable(true) lets a click or Tab focus it, for a diff that stands as a document. It stays read-only: typing, pasting and deleting change nothing.

    Following the last row

    CodeDiff::new("stream", rows).follow_tail(true).render()

    With follow_tail(true) a diff that is at its last row stays there as rows are appended. Scrolling up stops it, and scrolling back to the end resumes it.

    API reference

    The guide covers composition, options and actions. Generate the complete Rust type and builder reference from the checkout:

    cargo doc -p limn --open

    Component API overview · State and actions

    The example is drawn by crates/limn-gallery/src/pages/code.rs.