hewdiff 0.6.2

High-performance review-first terminal diff viewer with PR-style comments
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
//! Comment/composer box and action-button line builders.

use super::*;

impl App {
    /// Buttons for a thread box's action row: reply, resolve/unresolve, and
    /// (only when the focused comment is a session-added one in this thread)
    /// delete. `(label-with-hotkey, action, label text color)` — the chip's
    /// background is a uniform subtle surface; this color is its foreground.
    pub(super) fn comment_buttons(&self, cl: &CommentLine) -> Vec<(String, ButtonAction, Color)> {
        let tid = cl.thread_id.clone();
        let mut v = vec![(
            "reply(r)".to_string(),
            ButtonAction::Reply(tid.clone()),
            theme().accent,
        )];
        if cl.resolved {
            v.push((
                "unresolve(R)".to_string(),
                ButtonAction::ToggleResolve(tid.clone()),
                theme().warn,
            ));
        } else {
            v.push((
                "resolve(R)".to_string(),
                ButtonAction::ToggleResolve(tid.clone()),
                theme().added,
            ));
        }
        if let Some((ftid, fcid)) = self.focused_comment() {
            if ftid == tid && !self.base_comment_ids.contains(&fcid) {
                v.push((
                    "delete(D)".to_string(),
                    ButtonAction::Delete(tid, fcid),
                    theme().removed,
                ));
            }
        }
        v
    }

    /// Buttons for the open composer's action row: submit and cancel.
    pub(super) fn composer_buttons(&self) -> Vec<(String, ButtonAction, Color)> {
        vec![
            (
                "submit(ctrl+s)".to_string(),
                ButtonAction::Submit,
                theme().accent,
            ),
            (
                "cancel(esc)".to_string(),
                ButtonAction::Cancel,
                theme().muted,
            ),
        ]
    }

    /// Build one full-width action-button row inside a box. The box (margin +
    /// `│ … │` + button chips) is `box_w` cells wide and starts `left_pad`
    /// cells into the row (for split-view side placement); the rest is padded
    /// to `width`. Each chip is a subtle raised surface with the action's color
    /// as its label text, and its on-screen rect is recorded (at `x0` + offset,
    /// row `y`) for click hit-testing.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn action_row_line(
        &self,
        btns: &[(String, ButtonAction, Color)],
        border: Color,
        x0: u16,
        y: u16,
        width: usize,
        left_pad: usize,
        box_w: usize,
    ) -> Line<'static> {
        const MARGIN: usize = 2;
        let bstyle = Style::default().fg(border);
        if box_w <= MARGIN + 2 {
            return Line::from(Span::raw(" ".repeat(width)));
        }
        let inner_w = box_w - MARGIN - 2;
        let mut spans: Vec<Span<'static>> = Vec::new();
        if left_pad > 0 {
            spans.push(Span::raw(" ".repeat(left_pad)));
        }
        spans.push(Span::raw(" ".repeat(MARGIN)));
        spans.push(Span::styled("".to_string(), bstyle));
        // Inner content: ` label ` chips packed left-to-right. Each chip is a
        // subtle raised surface with the action's color as the *text* (a soft,
        // toolbar-like button rather than a loud solid fill); the chip padding
        // and distinct text colors keep them readable as separate buttons even
        // packed tight, which matters in split view's narrow side column. Each
        // chip's screen rect is recorded for click hit-testing.
        let mut col = 0usize;
        let base_x = x0 + (left_pad + MARGIN + 1) as u16;
        let mut inner: Vec<Span<'static>> = Vec::new();
        for (i, (label, action, color)) in btns.iter().enumerate() {
            let chip = format!(" {label} ");
            let w = str_width(&chip);
            // A one-cell (box-background) gap before each chip after the first,
            // so the raised surfaces read as separate buttons. The gap is part
            // of the fit test, so it never dangles and drops the next chip.
            let gap = usize::from(i > 0);
            if col + gap + w > inner_w {
                break;
            }
            if gap > 0 {
                inner.push(Span::raw(" "));
                col += 1;
            }
            self.button_hits.borrow_mut().push((
                Rect {
                    x: base_x + col as u16,
                    y,
                    width: w as u16,
                    height: 1,
                },
                action.clone(),
            ));
            inner.push(Span::styled(
                chip,
                Style::default()
                    .bg(theme().subtle)
                    .fg(*color)
                    .add_modifier(Modifier::BOLD),
            ));
            col += w;
        }
        if col < inner_w {
            inner.push(Span::raw(" ".repeat(inner_w - col)));
        }
        spans.extend(inner);
        spans.push(Span::styled("".to_string(), bstyle));
        let used = left_pad + box_w;
        if used < width {
            spans.push(Span::raw(" ".repeat(width - used)));
        }
        Line::from(spans)
    }

    /// Overlay a right-aligned button chip on an already-built diff line: clip
    /// the line's content to leave room, then append ` label `. Used to float a
    /// `comment(i)` button at the end of the focused diff line (GitHub-style).
    /// Records the chip's screen rect (at `x0` + offset, row `y`) for clicks.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn append_right_button(
        &self,
        spans: Vec<Span<'static>>,
        width: usize,
        label: &str,
        color: Color,
        action: ButtonAction,
        x0: u16,
        y: u16,
    ) -> Line<'static> {
        let chip = format!(" {label} ");
        let cw = str_width(&chip);
        if width <= cw {
            return Line::from(spans);
        }
        let keep = width - cw;
        // Clip the existing spans to `keep` display cells (preserving styles,
        // so the line's selection tint carries up to the button).
        let mut out: Vec<Span<'static>> = Vec::new();
        let mut acc = 0usize;
        for s in spans {
            if acc >= keep {
                break;
            }
            let sw = str_width(&s.content);
            if acc + sw <= keep {
                acc += sw;
                out.push(s);
            } else {
                let (t, tw) = take_width(&s.content, keep - acc);
                out.push(Span::styled(t, s.style));
                acc += tw;
                break;
            }
        }
        if acc < keep {
            out.push(Span::raw(" ".repeat(keep - acc)));
        }
        self.button_hits.borrow_mut().push((
            Rect {
                x: x0 + keep as u16,
                y,
                width: cw as u16,
                height: 1,
            },
            action,
        ));
        out.push(Span::styled(
            chip,
            Style::default()
                .bg(theme().subtle)
                .fg(color)
                .add_modifier(Modifier::BOLD),
        ));
        Line::from(out)
    }

    /// Render one inline comment line as part of a rounded box spanning `width`
    /// (2-column left margin + `╭─╮`/`│ │`/`╰─╯` frame). `focused` is set for
    /// the rows of the message the cursor is on.
    pub(super) fn comment_line_to_line(
        &self,
        cl: &CommentLine,
        focused: bool,
        width: usize,
    ) -> Line<'static> {
        const MARGIN: usize = 2;
        // Border brightness signals focus: the focused message keeps the bright
        // (theme) border so the cursor is visible even on a resolved thread
        // (whose box is otherwise dimmed). Only an *unfocused* resolved thread
        // reads as fully settled. Without the focus check first, landing on a
        // resolved comment gave no visual feedback at all.
        let border_col = if focused {
            theme().border_focus
        } else if cl.resolved {
            theme().muted
        } else {
            theme().border_unfocus
        };
        let bstyle = Style::default().fg(border_col);
        // Box occupies cols [MARGIN, width); inner_w is the span between borders.
        let inner_w = width.saturating_sub(MARGIN + 2);
        if width <= MARGIN + 2 {
            return Line::from(Span::raw(" ".repeat(width)));
        }
        let margin = Span::raw(" ".repeat(MARGIN));
        match &cl.kind {
            CommentKind::Top => Line::from(vec![
                margin,
                Span::styled(format!("{}", "".repeat(inner_w)), bstyle),
            ]),
            CommentKind::Bottom => Line::from(vec![
                margin,
                Span::styled(format!("{}", "".repeat(inner_w)), bstyle),
            ]),
            // The action row is normally drawn by `action_row_line` straight
            // from the render loop (it needs the screen position to record
            // click rects); this is a blank-box fallback if it's ever reached.
            CommentKind::Actions => Line::from(vec![
                margin,
                Span::styled("".to_string(), bstyle),
                Span::raw(" ".repeat(inner_w)),
                Span::styled("".to_string(), bstyle),
            ]),
            CommentKind::Author { name, date } => {
                // Name on the left, date flush right (1-col gutter before the
                // border). Spans below total exactly `inner_w`.
                let left = format!(" @{name}");
                let llen = str_width(&left);
                let dlen = str_width(date);
                let name_col = if cl.resolved {
                    theme().muted
                } else {
                    theme().warn
                };
                let name_style = Style::default().fg(name_col).add_modifier(Modifier::BOLD);
                let inner = if llen + dlen + 2 <= inner_w {
                    vec![
                        Span::styled(left, name_style),
                        Span::raw(" ".repeat(inner_w - llen - dlen - 1)),
                        Span::styled(date.clone(), Style::default().fg(theme().muted)),
                        Span::raw(" "),
                    ]
                } else {
                    let (l, lw) = take_width(&left, inner_w);
                    let pad = inner_w - lw;
                    vec![Span::styled(l, name_style), Span::raw(" ".repeat(pad))]
                };
                let mut spans = vec![margin, Span::styled("".to_string(), bstyle)];
                spans.extend(inner);
                spans.push(Span::styled("".to_string(), bstyle));
                Line::from(spans)
            }
            _ => {
                let (content, color, bold) = match &cl.kind {
                    CommentKind::Head { replies } => (
                        format!(
                            "{} · {} message{}",
                            if cl.resolved { "resolved" } else { "open" },
                            replies,
                            if *replies == 1 { "" } else { "s" }
                        ),
                        if cl.resolved {
                            theme().muted
                        } else {
                            theme().accent
                        },
                        true,
                    ),
                    CommentKind::Body(b) => (
                        format!(" {b}"),
                        if cl.resolved {
                            theme().muted
                        } else {
                            theme().text
                        },
                        false,
                    ),
                    _ => (String::new(), theme().text, false), // Gap
                };
                let mut cstyle = Style::default().fg(color);
                if bold {
                    cstyle = cstyle.add_modifier(Modifier::BOLD);
                }
                let clen = str_width(&content);
                let content = if clen > inner_w {
                    take_width(&content, inner_w).0
                } else {
                    format!("{content}{}", " ".repeat(inner_w - clen))
                };
                Line::from(vec![
                    margin,
                    Span::styled("".to_string(), bstyle),
                    Span::styled(content, cstyle),
                    Span::styled("".to_string(), bstyle),
                ])
            }
        }
    }

    /// Render one inline composer line as part of a rounded accent box spanning
    /// `width` (2-col left margin + framed title / live body / key hint).
    pub(super) fn composer_line_to_line(&self, cl: &ComposerLine, width: usize) -> Line<'static> {
        const MARGIN: usize = 2;
        let bstyle = Style::default().fg(theme().accent);
        let inner_w = width.saturating_sub(MARGIN + 2);
        if width <= MARGIN + 2 {
            return Line::from(Span::raw(" ".repeat(width)));
        }
        let margin = Span::raw(" ".repeat(MARGIN));
        // Pad/truncate `s` to exactly `inner_w` display cells.
        let fit = |s: &str| -> String {
            let w = str_width(s);
            if w > inner_w {
                take_width(s, inner_w).0
            } else {
                format!("{s}{}", " ".repeat(inner_w - w))
            }
        };
        match &cl.kind {
            ComposerKind::Top { title } => {
                let t = take_width(title, inner_w).0;
                let dashes = inner_w.saturating_sub(str_width(&t));
                Line::from(vec![
                    margin,
                    Span::styled("".to_string(), bstyle),
                    Span::styled(
                        t,
                        Style::default()
                            .fg(theme().accent)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled("".repeat(dashes), bstyle),
                    Span::styled("".to_string(), bstyle),
                ])
            }
            ComposerKind::Bottom => Line::from(vec![
                margin,
                Span::styled(format!("{}", "".repeat(inner_w)), bstyle),
            ]),
            ComposerKind::Body(b) => {
                let text_style = Style::default().fg(theme().text);
                let mut spans = vec![margin, Span::styled("".to_string(), bstyle)];
                // The caret is an *overlay*, not a character: render the cell at
                // the cursor with a reversed (block) style instead of splicing a
                // glyph in, so the surrounding text never shifts.
                if let Some(idx) = b.find(COMPOSER_CARET) {
                    let pre = &b[..idx];
                    let after = &b[idx + COMPOSER_CARET.len_utf8()..];
                    // The character sitting under the cursor (or a space at EOL).
                    let mut chars = after.chars();
                    let under = chars.next();
                    let tail: String = chars.collect();
                    let mut cursor_cell =
                        under.map(|c| c.to_string()).unwrap_or_else(|| " ".into());
                    // Lay out exactly `inner_w` cells, honoring the same
                    // pad/truncate contract as `fit`: reserve the cursor cell
                    // first (so the caret is never the thing that gets clipped),
                    // then fit the lead text and tail around it. A wide cursor
                    // glyph wider than the whole box degrades to a space.
                    if str_width(&cursor_cell) > inner_w {
                        cursor_cell = " ".into();
                    }
                    let cursor_w = str_width(&cursor_cell);
                    let (lead, lead_w) = take_width(&format!(" {pre}"), inner_w - cursor_w);
                    let (tail, tail_w) = take_width(&tail, inner_w - lead_w - cursor_w);
                    let pad = inner_w - lead_w - cursor_w - tail_w;
                    spans.push(Span::styled(lead, text_style));
                    spans.push(Span::styled(
                        cursor_cell,
                        text_style.add_modifier(Modifier::REVERSED),
                    ));
                    spans.push(Span::styled(
                        format!("{tail}{}", " ".repeat(pad)),
                        text_style,
                    ));
                } else {
                    spans.push(Span::styled(fit(&format!(" {b}")), text_style));
                }
                spans.push(Span::styled("".to_string(), bstyle));
                Line::from(spans)
            }
            ComposerKind::Hint => Line::from(vec![
                margin,
                Span::styled("".to_string(), bstyle),
                Span::styled(
                    fit(" ctrl+s: submit · enter: newline · esc: cancel"),
                    Style::default().fg(theme().muted),
                ),
                Span::styled("".to_string(), bstyle),
            ]),
        }
    }
}