tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use tui_lipan::prelude::*;

struct DiffHunkNavigation;

struct PatchExample {
    title: &'static str,
    path: &'static str,
    patch: &'static str,
}

struct GlobalHunk {
    patch_index: usize,
    hunk_index: usize,
    anchor: DiffHunkAnchor,
}

const PATCHES: &[PatchExample] = &[
    PatchExample {
        title: "Renderer hover states",
        path: "src/backend/ratatui_backend/renderers/text_area.rs",
        patch: concat!(
            "diff --git a/src/backend/ratatui_backend/renderers/text_area.rs b/src/backend/ratatui_backend/renderers/text_area.rs\n",
            "--- a/src/backend/ratatui_backend/renderers/text_area.rs\n",
            "+++ b/src/backend/ratatui_backend/renderers/text_area.rs\n",
            "@@ -24,6 +24,15 @@ fn render_text_area_row(row: Row) {\n",
            "     let mut style = row.style;\n",
            "     if row.selected {\n",
            "         style = style.patch(theme.selection);\n",
            "     }\n",
            "+    if row.context_separator && row.hovered {\n",
            "+        style = style.patch(theme.diff_context_hover);\n",
            "+    }\n",
            "+\n",
            "+    if row.context_separator {\n",
            "+        render_full_width_background(row.area, style);\n",
            "+    }\n",
            "     draw_spans(row.spans, style);\n",
            " }\n",
            "@@ -86,7 +95,11 @@ fn row_hit_test(line: usize, x: u16) -> Hit {\n",
            "     if x < gutter_width {\n",
            "         return Hit::Gutter;\n",
            "     }\n",
            "-    Hit::Content(line)\n",
            "+    if context_separator_lines.contains(&line) {\n",
            "+        Hit::ContextSeparator(line)\n",
            "+    } else {\n",
            "+        Hit::Content(line)\n",
            "+    }\n",
            " }\n",
        ),
    },
    PatchExample {
        title: "Session keybindings",
        path: "src/screens/diff_viewer.rs",
        patch: concat!(
            "diff --git a/src/screens/diff_viewer.rs b/src/screens/diff_viewer.rs\n",
            "--- a/src/screens/diff_viewer.rs\n",
            "+++ b/src/screens/diff_viewer.rs\n",
            "@@ -12,6 +12,7 @@ pub enum DiffViewerMsg {\n",
            "     Close,\n",
            "     Scroll(usize),\n",
            "     ToggleWrap,\n",
            "+    JumpToHunk(usize),\n",
            " }\n",
            "@@ -48,8 +49,16 @@ impl Component for DiffViewer {\n",
            "         match key.code {\n",
            "             KeyCode::Esc => ctx.emit(DiffViewerMsg::Close),\n",
            "             KeyCode::Char('w') => ctx.emit(DiffViewerMsg::ToggleWrap),\n",
            "+            KeyCode::Char(']') => {\n",
            "+                let next = self.current_hunk.saturating_add(1);\n",
            "+                ctx.emit(DiffViewerMsg::JumpToHunk(next));\n",
            "+            }\n",
            "+            KeyCode::Char('[') => {\n",
            "+                let prev = self.current_hunk.saturating_sub(1);\n",
            "+                ctx.emit(DiffViewerMsg::JumpToHunk(prev));\n",
            "+            }\n",
            "             _ => return KeyUpdate::unhandled(Update::none()),\n",
            "         }\n",
            "@@ -92,7 +101,9 @@ fn view_diff(patch: &str, state: &State) -> Element {\n",
            "     DiffView::from_patch(patch)\n",
            "         .mode(DiffViewMode::Unified)\n",
            "         .wrap(state.wrap)\n",
            "-        .height(Length::Flex(1))\n",
            "+        .height(Length::Flex(1))\n",
            "+        .scroll_to_hunk(state.current_hunk)\n",
            "+        .context_lines(4)\n",
            "         .into()\n",
            " }\n",
        ),
    },
    PatchExample {
        title: "Context collapse docs",
        path: "docs/widgets/input.md",
        patch: concat!(
            "diff --git a/docs/widgets/input.md b/docs/widgets/input.md\n",
            "--- a/docs/widgets/input.md\n",
            "+++ b/docs/widgets/input.md\n",
            "@@ -700,6 +700,10 @@ DiffView supports collapsed context regions.\n",
            " Use `context_lines(n)` to keep a compact window around changes.\n",
            " The separator line is styled through `DiffPalette`.\n",
            "\n",
            "+Patch-backed views also expose hunk anchors for keyboard navigation.\n",
            "+Use `DiffData::hunk_anchors(...)` for labels and `DiffView::scroll_to_hunk(...)`\n",
            "+to let the active backend resolve wrapped visual rows after layout.\n",
            "+\n",
            " ```rust\n",
            " DiffView::from_patch(patch)\n",
            "     .context_lines(3)\n",
            "@@ -728,7 +732,7 @@ ScrollView::new()\n",
            "     .children(messages.iter().map(render_message));\n",
            " ```\n",
            "\n",
            "-Outer scroll views own timeline positioning.\n",
            "+Outer scroll views own timeline positioning; inner DiffViews own hunk positioning.\n",
        ),
    },
];

#[derive(Default)]
struct State {
    global_hunk: usize,
}

#[derive(Clone, Debug)]
enum Msg {}

impl Component for DiffHunkNavigation {
    type Message = Msg;
    type Properties = ();
    type State = State;

    fn create_state(&self, _props: &Self::Properties) -> Self::State {
        State::default()
    }

    fn update(&mut self, _msg: Self::Message, _ctx: &mut Context<Self>) -> Update {
        Update::none()
    }

    fn on_key(&mut self, key: KeyEvent, ctx: &mut Context<Self>) -> KeyUpdate {
        match key.code {
            KeyCode::Char(']') => {
                move_global_hunk(&mut ctx.state, 1);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Char('[') => {
                move_global_hunk(&mut ctx.state, -1);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Char('1') => {
                select_patch(&mut ctx.state, 0);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Char('2') => {
                select_patch(&mut ctx.state, 1);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Char('3') => {
                select_patch(&mut ctx.state, 2);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Tab => {
                move_patch(&mut ctx.state, 1);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::BackTab => {
                move_patch(&mut ctx.state, -1);
                KeyUpdate::handled(Update::full())
            }
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
                ctx.quit();
                KeyUpdate::handled(Update::full())
            }
            _ => KeyUpdate::unhandled(Update::none()),
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Element {
        let hunks = global_hunks();
        let current = current_global_hunk(&ctx.state, &hunks);
        let active = current
            .map(|hunk| hunk.patch_index)
            .unwrap_or_default()
            .min(PATCHES.len().saturating_sub(1));
        let active_key = patch_key(active);
        let active_offset = current.map(hunk_outer_offset).unwrap_or_default();
        let active_hunk = current.map(|hunk| hunk.hunk_index).unwrap_or_default();
        let active_count = hunk_count(PATCHES[active].patch);

        Frame::new()
            .title("DiffView hunk navigation")
            .status("[ previous global hunk | ] next global hunk | 1/2/3 first hunk in file | Tab file | q quit")
            .border(true)
            .border_style(BorderStyle::Rounded)
            .padding(1)
            .child(
                VStack::new()
                    .gap(1)
                    .child(summary(
                        active,
                        active_hunk,
                        active_count,
                        current.map(|hunk| hunk.anchor.logical_line).unwrap_or_default(),
                        ctx.state.global_hunk.min(hunks.len().saturating_sub(1)),
                        hunks.len(),
                    ))
                    .child(
                        ScrollView::new()
                            .border(true)
                            .border_style(BorderStyle::Rounded)
                            .scrollbar(true)
                            .show_scroll_indicators(true)
                            .padding(1)
                            .gap(1)
                            .scroll_to_key_offset(active_key, active_offset)
                            .children(
                                PATCHES
                                    .iter()
                                    .enumerate()
                                    .map(|(index, _)| render_patch_card(index, current)),
                            ),
                    ),
            )
            .into()
    }
}

fn global_hunks() -> Vec<GlobalHunk> {
    PATCHES
        .iter()
        .enumerate()
        .flat_map(|(patch_index, patch)| {
            DiffData::from_patch(patch.patch)
                .hunk_anchors(DiffViewMode::Unified)
                .into_iter()
                .map(move |anchor| GlobalHunk {
                    patch_index,
                    hunk_index: anchor.index,
                    anchor,
                })
        })
        .collect()
}

fn current_global_hunk<'a>(state: &State, hunks: &'a [GlobalHunk]) -> Option<&'a GlobalHunk> {
    hunks.get(state.global_hunk.min(hunks.len().saturating_sub(1)))
}

fn move_global_hunk(state: &mut State, delta: isize) {
    let count = global_hunks().len();
    if count == 0 {
        return;
    }

    state.global_hunk = if delta.is_negative() {
        state.global_hunk.checked_sub(1).unwrap_or(count - 1)
    } else {
        (state.global_hunk + 1) % count
    };
}

fn select_patch(state: &mut State, patch_index: usize) {
    let patch_index = patch_index.min(PATCHES.len().saturating_sub(1));
    let hunks = global_hunks();
    if let Some(index) = hunks
        .iter()
        .position(|hunk| hunk.patch_index == patch_index)
    {
        state.global_hunk = index;
    }
}

fn move_patch(state: &mut State, delta: isize) {
    let hunks = global_hunks();
    let Some(current) = current_global_hunk(state, &hunks) else {
        return;
    };

    let next_patch = if delta.is_negative() {
        current
            .patch_index
            .checked_sub(1)
            .unwrap_or_else(|| PATCHES.len().saturating_sub(1))
    } else {
        (current.patch_index + 1) % PATCHES.len()
    };
    select_patch(state, next_patch);
}

fn hunk_count(patch: &str) -> usize {
    DiffData::from_patch(patch)
        .hunk_anchors(DiffViewMode::Unified)
        .len()
}

fn summary(
    active: usize,
    active_hunk: usize,
    hunk_count: usize,
    logical_line: usize,
    global_hunk: usize,
    global_count: usize,
) -> Element {
    Frame::new()
        .title("How this works")
        .height(Length::Auto)
        .border(true)
        .border_style(BorderStyle::Plain)
        .padding(1)
        .child(
            VStack::new()
                .gap(1)
                .child(Text::new(format!(
                    "Global hunk {} of {} -> file {} of {}, hunk {} of {}, pre-collapse row {}",
                    global_hunk + 1,
                    global_count.max(1),
                    active + 1,
                    PATCHES.len(),
                    active_hunk + 1,
                    hunk_count.max(1),
                    logical_line,
                )))
                .child(Text::new(
                    "One global cursor chooses a hunk row. The outer ScrollView reveals that keyed row directly.",
                ))
                .child(
                    Text::new(
                        "With auto-height DiffViews, the outer ScrollView owns hunk visibility; inner diff scrolling is disabled.",
                    )
                    .style(Style::new().fg(Color::DarkGray)),
                ),
        )
        .into()
}

fn hunk_outer_offset(hunk: &GlobalHunk) -> usize {
    // Card border + padding + path row + gap + anchor row + gap before the DiffView content.
    6usize.saturating_add(hunk.anchor.logical_line)
}

fn render_patch_card(index: usize, current: Option<&GlobalHunk>) -> Element {
    let patch = &PATCHES[index];
    let is_active = current.is_some_and(|hunk| hunk.patch_index == index);
    let hunk_index = current
        .filter(|hunk| hunk.patch_index == index)
        .map(|hunk| hunk.hunk_index)
        .unwrap_or_default();
    let anchors = DiffData::from_patch(patch.patch).hunk_anchors(DiffViewMode::Unified);
    let hunk_count = anchors.len();
    let anchor_text = anchors
        .iter()
        .map(|anchor| {
            let marker = if is_active && anchor.index == hunk_index {
                "*"
            } else {
                " "
            };
            format!(
                "{marker}#{idx}: row {row}, -{old:?} +{new:?}",
                idx = anchor.index + 1,
                row = anchor.logical_line,
                old = anchor.old_start,
                new = anchor.new_start,
            )
        })
        .collect::<Vec<_>>()
        .join("  ");
    let title = if is_active {
        format!("> {}", patch.title)
    } else {
        patch.title.to_string()
    };

    let diff = DiffView::from_patch(patch.patch)
        .mode(DiffViewMode::Unified)
        .backend(DiffViewBackend::DocumentView)
        .height(Length::Auto)
        .wrap(false)
        .line_numbers(true)
        .highlight_full_width(true)
        .context_separator_hover_style(Style::new().underline())
        .panels_border(false)
        .border(false)
        .scrollbar(false);

    Element::from(
        Frame::new()
            .title(title)
            .status(format!(
                "{} hunk(s) | file {} | {}",
                hunk_count,
                index + 1,
                if is_active { "active" } else { "inactive" }
            ))
            .height(Length::Auto)
            .border(true)
            .border_style(BorderStyle::Rounded)
            .title_style(if is_active {
                Style::new().fg(Color::LightCyan).bold()
            } else {
                Style::default()
            })
            .padding(1)
            .child(
                VStack::new()
                    .gap(1)
                    .child(Text::new(patch.path).style(Style::new().fg(Color::DarkGray)))
                    .child(Text::new(anchor_text))
                    .child(diff),
            ),
    )
    .key(patch_key(index))
}

fn patch_key(index: usize) -> String {
    format!("patch-{index}")
}

fn main() -> Result<()> {
    App::new()
        .title("Diff hunk navigation example")
        .mount(DiffHunkNavigation)
        .run()
}