rpi-tui 0.36.2

Terminal User Interface for pi-rust
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
//! Diff rendering for tool results — colored `+`/`-`/` ` lines with
//! intra-line word-diff highlighting.
//!
//! Port of the TypeScript `renderDiff`
//! (`packages/coding-agent/.../components/diff.ts`).
//!
//! The `edit` tool already produces a display-oriented diff string in
//! `result.details.diff` (lines shaped `+123 …` / `-123 …` / ` 123 …`;
//! see `rpi-tools/src/tools/edit_diff.rs::generate_diff_string`). This module
//! consumes that *string* (no generator, no `rpi-tools` dependency) and renders
//! it as ANSI-styled terminal lines:
//!
//! - removed (`-`) → red (`theme.colors.error`)
//! - added   (`+`) → green (`theme.colors.success`)
//! - context (` `) → muted/dim (`theme.colors.muted`)
//! - for a single removed→added pair, the changed *words* are inverse-highlighted
//!   so you can see what moved within the line.

use std::time::{Duration, Instant};

use similar::TextDiff;

use crate::ansi::strip_ansi;
use crate::theme::theme;
use crate::utils::{truncate_to_width, visible_width};

/// Render a display-diff string into styled terminal lines.
///
/// `width` is the available column count; long diff lines are clipped with
/// `…` rather than wrapped (a wrapped diff is unreadable).
///
/// Layout: a fixed 4-col gutter `SIGN NUM│ ` where SIGN is `+`/`-`/` `, NUM
/// is the right-aligned line number, and `│` is a thin themed rule. The
/// content body follows after the gutter. This lines every diff row up on a
/// shared left edge (the old `+123 content` format let the content column
/// drift with line-number width) and gives +/- a consistent glyph column.
pub fn render_diff(diff_text: &str, width: usize) -> Vec<String> {
    let colors = &theme().colors;
    let removed_color = colors.tool_diff_removed; // red
    let added_color = colors.tool_diff_added; // green
    let context_color = colors.tool_diff_context; // dim
    let gutter_color = colors.dim;

    let lines: Vec<&str> = diff_text.split('\n').collect();
    // Right-align line numbers to a shared width so the gutter rule `│`
    // stays in one column across the whole hunk.
    let num_width = lines
        .iter()
        .filter_map(|l| parse_diff_line(l))
        .map(|p| p.line_num.len())
        .max()
        .unwrap_or(1);
    let mut out: Vec<String> = Vec::with_capacity(lines.len());
    let word_diff_deadline = Instant::now() + WORD_DIFF_BUDGET;

    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        let Some(parsed) = parse_diff_line(line) else {
            // Unparseable line — render as dim context with no gutter.
            out.push(context_color.fg(&clip(line, width)));
            i += 1;
            continue;
        };

        if parsed.prefix == '-' {
            // Collect consecutive removed lines.
            let mut removed: Vec<ParsedLine> = Vec::new();
            while i < lines.len() {
                let Some(p) = parse_diff_line(lines[i]) else {
                    break;
                };
                if p.prefix != '-' {
                    break;
                }
                removed.push(p);
                i += 1;
            }
            // Collect consecutive added lines immediately after.
            let mut added: Vec<ParsedLine> = Vec::new();
            while i < lines.len() {
                let Some(p) = parse_diff_line(lines[i]) else {
                    break;
                };
                if p.prefix != '+' {
                    break;
                }
                added.push(p);
                i += 1;
            }

            if removed.len() == 1 && added.len() == 1 {
                // Single-line modification → intra-line word diff.
                let (rem_body, add_body) = render_intra_line_diff(
                    &replace_tabs(&removed[0].content),
                    &replace_tabs(&added[0].content),
                    word_diff_deadline,
                );
                out.push(render_diff_row(
                    '-',
                    &removed[0].line_num,
                    &rem_body,
                    removed_color,
                    gutter_color,
                    num_width,
                    width,
                ));
                out.push(render_diff_row(
                    '+',
                    &added[0].line_num,
                    &add_body,
                    added_color,
                    gutter_color,
                    num_width,
                    width,
                ));
            } else {
                for r in &removed {
                    out.push(render_diff_row(
                        '-',
                        &r.line_num,
                        &replace_tabs(&r.content),
                        removed_color,
                        gutter_color,
                        num_width,
                        width,
                    ));
                }
                for a in &added {
                    out.push(render_diff_row(
                        '+',
                        &a.line_num,
                        &replace_tabs(&a.content),
                        added_color,
                        gutter_color,
                        num_width,
                        width,
                    ));
                }
            }
        } else if parsed.prefix == '+' {
            out.push(render_diff_row(
                '+',
                &parsed.line_num,
                &replace_tabs(&parsed.content),
                added_color,
                gutter_color,
                num_width,
                width,
            ));
            i += 1;
        } else {
            // Context line.
            out.push(render_diff_row(
                ' ',
                &parsed.line_num,
                &replace_tabs(&parsed.content),
                context_color,
                gutter_color,
                num_width,
                width,
            ));
            i += 1;
        }
    }

    out
}

/// Render one diff row with a themed gutter: `SIGN NUM│ body`.
/// `body` may already carry ANSI (intra-line inverse highlights); `clip`
/// handles that. The sign and the gutter rule use `gutter_color` so the
/// colored body (red/green/dim) reads as the payload, not the chrome.
fn render_diff_row(
    sign: char,
    line_num: &str,
    body: &str,
    body_color: crate::theme::Color,
    gutter_color: crate::theme::Color,
    num_width: usize,
    width: usize,
) -> String {
    let num = format!("{num:>num_width$}", num = line_num, num_width = num_width);
    let gutter = format!(
        "{} {}{}",
        gutter_color.fg(&sign.to_string()),
        gutter_color.fg(&num),
        gutter_color.fg("│")
    );
    let body = format!(" {}", body_color.fg(body));
    clip(&format!("{}{}", gutter, body), width)
}

#[derive(Debug)]
struct ParsedLine {
    prefix: char,
    line_num: String,
    content: String,
}

/// Parse a diff line: `"+123 content"`, `"-5 content"`, `" 7 context"`,
/// or `"  ..."`. Returns `(prefix, line_num, content)` where `prefix` is the
/// leading `+`/`-`/` ` character.
fn parse_diff_line(line: &str) -> Option<ParsedLine> {
    let mut chars = line.chars();
    let prefix_char = chars.next()?;
    if prefix_char != '+' && prefix_char != '-' && prefix_char != ' ' {
        return None;
    }
    // The TS regex: `^([+-\s])(\s*\d*)\s(.*)$` — optional spaces, then digits,
    // then a single separating space, then content.
    let after_prefix = &line[prefix_char.len_utf8()..];
    let mut rest = after_prefix;
    while rest.starts_with(' ') {
        rest = &rest[1..];
    }
    let mut num = String::new();
    while let Some(c) = rest.chars().next() {
        if c.is_ascii_digit() {
            num.push(c);
            rest = &rest[c.len_utf8()..];
        } else {
            break;
        }
    }
    if let Some(stripped) = rest.strip_prefix(' ') {
        rest = stripped;
    }
    Some(ParsedLine {
        prefix: prefix_char,
        line_num: num,
        content: rest.to_string(),
    })
}

/// Replace tabs with 3 spaces (matches the TS `replaceTabs`).
fn replace_tabs(text: &str) -> String {
    text.replace('\t', "   ")
}

/// Clip a (possibly already styled) line to `width` columns with a `…` suffix.
fn clip(line: &str, width: usize) -> String {
    if width == 0 {
        return line.to_string();
    }
    let vw = visible_width(line);
    if vw <= width {
        line.to_string()
    } else {
        truncate_to_width(line, width, "…")
    }
}

const MAX_WORD_DIFF_BYTES: usize = 4 * 1024;
const WORD_DIFF_BUDGET: Duration = Duration::from_millis(20);

/// Compute a word-level diff between two lines and return the removed/added
/// renderings with changed parts wrapped in inverse video (`\x1b[7m`).
///
/// Port of `renderIntraLineDiff` (diff.ts:26). Leading whitespace is not
/// inverse-highlighted (it would visually smear the indentation).
fn render_intra_line_diff(
    old_content: &str,
    new_content: &str,
    deadline: Instant,
) -> (String, String) {
    if old_content.len().saturating_add(new_content.len()) > MAX_WORD_DIFF_BYTES
        || Instant::now() >= deadline
    {
        return (old_content.to_owned(), new_content.to_owned());
    }
    let diff = TextDiff::configure()
        .deadline(deadline)
        .diff_unicode_words(old_content, new_content);
    if Instant::now() >= deadline {
        return (old_content.to_owned(), new_content.to_owned());
    }

    let mut removed_line = String::new();
    let mut added_line = String::new();
    let mut first_removed = true;
    let mut first_added = true;

    for change in diff.iter_all_changes() {
        let value = change.value();
        match change.tag() {
            similar::ChangeTag::Delete => {
                let (leading, rest) = split_leading_ws(value);
                if first_removed {
                    removed_line.push_str(leading);
                    first_removed = false;
                }
                if !rest.is_empty() {
                    removed_line.push_str(&inverse(rest));
                }
            }
            similar::ChangeTag::Insert => {
                let (leading, rest) = split_leading_ws(value);
                if first_added {
                    added_line.push_str(leading);
                    first_added = false;
                }
                if !rest.is_empty() {
                    added_line.push_str(&inverse(rest));
                }
            }
            similar::ChangeTag::Equal => {
                removed_line.push_str(value);
                added_line.push_str(value);
            }
        }
    }

    (removed_line, added_line)
}

/// Split a string into its leading whitespace and the remainder.
fn split_leading_ws(s: &str) -> (&str, &str) {
    let idx = s.find(|c: char| !c.is_whitespace()).unwrap_or(s.len());
    (&s[..idx], &s[idx..])
}

/// Apply ANSI inverse video to `text`, then reset. We strip any pre-existing
/// styling from the wrapped segment so the inverse reads cleanly.
fn inverse(text: &str) -> String {
    let clean = strip_ansi(text);
    format!("\x1b[7m{clean}\x1b[27m")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn expired_word_diff_budget_preserves_unhighlighted_text() {
        assert_eq!(
            render_intra_line_diff("old", "new", Instant::now()),
            ("old".into(), "new".into())
        );
    }

    #[test]
    fn mutation_preview_long_word_diff_uses_plain_colors() {
        let removed = "oldword ".repeat(3_000);
        let added = "newword ".repeat(3_000);
        let input = format!("-1 {removed}\n+1 {added}");
        let started = std::time::Instant::now();
        let rendered = render_diff(&input, 80);
        assert_eq!(rendered.len(), 2);
        assert!(strip_ansi(&rendered[0]).contains("oldword"));
        assert!(strip_ansi(&rendered[1]).contains("newword"));
        assert!(
            rendered.iter().all(|line| !line.contains("\x1b[7m")),
            "long rows enabled expensive word highlighting, elapsed: {:?}",
            started.elapsed()
        );
    }

    #[test]
    fn test_render_diff_colors_removed_and_added() {
        let diff = "-1 old\n+1 new";
        let lines = render_diff(diff, 80);
        assert_eq!(lines.len(), 2);
        // Red (error) fg escape for the removed line.
        assert!(lines[0].contains("old"));
        // Green (success) fg escape for the added line.
        assert!(lines[1].contains("new"));
    }

    #[test]
    fn test_render_diff_intra_line_word_highlight() {
        let diff = "-1 hello world\n+1 hello earth";
        let lines = render_diff(diff, 80);
        // The removed line highlights "world", the added line highlights "earth".
        assert!(
            lines[0].contains("\x1b[7m"),
            "removed intra-line not inversed"
        );
        assert!(
            lines[1].contains("\x1b[7m"),
            "added intra-line not inversed"
        );
        assert!(lines[1].contains("earth"));
    }

    #[test]
    fn test_render_diff_context_dim() {
        let diff = " 5 context line";
        let lines = render_diff(diff, 80);
        assert_eq!(lines.len(), 1);
        // Muted context color (Ansi256(244) by default — tool_diff_context).
        assert!(lines[0].contains("\x1b[38;5;244m"), "context not dimmed");
    }

    #[test]
    fn test_render_diff_clips_long_lines() {
        let long = format!("+1 {}", "x".repeat(200));
        let lines = render_diff(&long, 40);
        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains('…'));
        assert!(visible_width(&lines[0]) <= 40);
    }
}