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
//! Per-row line builders (diff lines, comment/composer boxes, buttons).
use super::*;
impl App {
/// One logical split row expanded into its display lines. A single line
/// unless `wrap` is on and a `Pair` has code wide enough to wrap on either
/// side; the pair's height is the taller of the two columns, with the
/// shorter column blank-padded so the divider stays aligned.
pub(super) fn split_row_to_lines(
&self,
row: &SplitRow,
selected: bool,
side_w: usize,
divider: &str,
) -> Vec<Line<'static>> {
match &row.kind {
SplitRowKind::Pair { left, right } if self.wrap => {
let lr = self.side_line_rows(left.as_ref(), row.file_idx, side_w, selected);
let rr = self.side_line_rows(right.as_ref(), row.file_idx, side_w, selected);
let n = lr.len().max(rr.len());
let mut out = Vec::with_capacity(n);
for i in 0..n {
let mut spans = lr
.get(i)
.cloned()
.unwrap_or_else(|| self.blank_side(left.as_ref(), side_w, selected));
spans.push(Span::styled(
divider.to_string(),
Style::default().fg(theme().subtle),
));
spans.extend(
rr.get(i)
.cloned()
.unwrap_or_else(|| self.blank_side(right.as_ref(), side_w, selected)),
);
out.push(Line::from(spans));
}
out
}
_ => vec![self.split_row_to_line(row, selected, side_w, divider)],
}
}
/// A blank `width`-wide column for continuation rows of the shorter side of
/// a wrapped pair, tinted with that side's background so it reads as part
/// of the same cell.
pub(super) fn blank_side(
&self,
cell: Option<&SideCell>,
width: usize,
selected: bool,
) -> Vec<Span<'static>> {
let st = match cell {
None => Style::default().bg(theme().comment_bg),
Some(c) => {
let bg = if selected {
Some(self.diff_cursor_bg())
} else {
match c.kind {
LineKind::Addition => Some(theme().add_bg),
LineKind::Deletion => Some(theme().del_bg),
LineKind::Context => None,
}
};
bg.map_or(Style::default(), |b| Style::default().bg(b))
}
};
vec![Span::styled(" ".repeat(width), st)]
}
pub(super) fn split_row_to_line(
&self,
row: &SplitRow,
selected: bool,
side_w: usize,
divider: &str,
) -> Line<'static> {
match &row.kind {
SplitRowKind::FileHeader => Line::from(Span::styled(
format!("▌ {}", row.text),
Style::default()
.fg(theme().text_strong)
.bg(theme().file_header_bg)
.add_modifier(Modifier::BOLD),
)),
SplitRowKind::HunkHeader => Line::from(Span::styled(
row.text.clone(),
Style::default()
.fg(theme().faint)
.add_modifier(Modifier::ITALIC),
)),
SplitRowKind::Pair { left, right } => {
let mut spans = self.side_spans(left.as_ref(), row.file_idx, side_w, selected);
spans.push(Span::styled(
divider.to_string(),
Style::default().fg(theme().subtle),
));
spans.extend(self.side_spans(right.as_ref(), row.file_idx, side_w, selected));
Line::from(spans)
}
SplitRowKind::Comment { side, line: cl } => {
// Render the thread under the column it is anchored to: old
// (deletion) comments on the left, new (addition) comments on
// the right. The opposite column is left blank.
let body = self.comment_line_to_line(cl, selected, side_w).spans;
match side {
Side::Old => {
let mut spans = body;
// Pad out to the divider + right column so the row
// fills its width.
spans.push(Span::styled(
" ".repeat(divider.chars().count() + side_w),
Style::default(),
));
Line::from(spans)
}
Side::New => {
let pad = side_w + divider.chars().count();
let mut spans = vec![Span::styled(" ".repeat(pad), Style::default())];
spans.extend(body);
Line::from(spans)
}
}
}
SplitRowKind::Composer { side, line: cl } => {
// Same side-aware placement as comment rows: render under the
// anchored column, blank the other.
let body = self.composer_line_to_line(cl, side_w).spans;
match side {
Side::Old => {
let mut spans = body;
spans.push(Span::styled(
" ".repeat(divider.chars().count() + side_w),
Style::default(),
));
Line::from(spans)
}
Side::New => {
let pad = side_w + divider.chars().count();
let mut spans = vec![Span::styled(" ".repeat(pad), Style::default())];
spans.extend(body);
Line::from(spans)
}
}
}
}
}
/// Render one side (old/new) of a split pair into spans of width `width`.
pub(super) fn side_spans(
&self,
cell: Option<&SideCell>,
file_idx: usize,
width: usize,
selected: bool,
) -> Vec<Span<'static>> {
const PREFIX: usize = 5; // line number(4) + space(1)
match cell {
None => vec![Span::styled(
" ".repeat(width),
Style::default().bg(theme().comment_bg),
)],
Some(c) => {
let num = c
.line
.map(|n| format!("{n:>4}"))
.unwrap_or_else(|| " ".into());
let bg = if selected {
Some(self.diff_cursor_bg())
} else {
match c.kind {
LineKind::Addition => Some(theme().add_bg),
LineKind::Deletion => Some(theme().del_bg),
LineKind::Context => None,
}
};
let mut spans = vec![Span::styled(
format!("{num} "),
Style::default().fg(theme().muted),
)];
spans.extend(self.styled_fit(file_idx, &c.text, width.saturating_sub(PREFIX), bg));
spans
}
}
}
/// Soft-wrap one side of a split pair into `>= 1` rows, each exactly `width`
/// cells wide (prefix + code + tint fill). The first row carries the line
/// number; continuation rows pad the 5-col prefix. Mirrors `side_spans` for
/// the non-wrapping single-row case.
pub(super) fn side_line_rows(
&self,
cell: Option<&SideCell>,
file_idx: usize,
width: usize,
selected: bool,
) -> Vec<Vec<Span<'static>>> {
const PREFIX: usize = 5;
let Some(c) = cell else {
return vec![vec![Span::styled(
" ".repeat(width),
Style::default().bg(theme().comment_bg),
)]];
};
let num = c
.line
.map(|n| format!("{n:>4}"))
.unwrap_or_else(|| " ".into());
let bg = if selected {
Some(self.diff_cursor_bg())
} else {
match c.kind {
LineKind::Addition => Some(theme().add_bg),
LineKind::Deletion => Some(theme().del_bg),
LineKind::Context => None,
}
};
let budget = width.saturating_sub(PREFIX);
let runs = self.hl.runs(file_idx, &c.text);
let wrapped = wrap_runs(&runs, budget, bg);
let mut out = Vec::with_capacity(wrapped.len());
for (i, (code_spans, code_w)) in wrapped.into_iter().enumerate() {
let prefix = if i == 0 {
format!("{num} ")
} else {
" ".repeat(PREFIX)
};
let mut spans = vec![Span::styled(prefix, Style::default().fg(theme().muted))];
spans.extend(code_spans);
let used = PREFIX + code_w;
// Pad to the full column width so the divider/right side align.
// `styled_fit` always pads (even with no bg), so match that to keep
// the column a fixed width regardless of tint.
if used < width {
let mut st = Style::default();
if let Some(b) = bg {
st = st.bg(b);
}
spans.push(Span::styled(" ".repeat(width - used), st));
}
out.push(spans);
}
out
}
/// One logical unified row expanded into its display lines. A single line
/// unless `wrap` is on and the row is a long code line, in which case the
/// code is soft-wrapped with continuation lines indented under it.
pub(super) fn row_to_lines(
&self,
row: &Row,
selected: bool,
width: usize,
) -> Vec<Line<'static>> {
match &row.kind {
RowKind::Line {
kind,
old_line,
new_line,
} if self.wrap => {
self.wrapped_code_lines(row, *kind, *old_line, *new_line, selected, width)
}
_ => vec![self.row_to_line(row, selected, width)],
}
}
/// Soft-wrap a unified code line. The first display line carries the line
/// numbers + sign; continuation lines pad that 13-col prefix so the code
/// stays aligned, and the diff background tint spans every line.
pub(super) fn wrapped_code_lines(
&self,
row: &Row,
kind: LineKind,
old_line: Option<u32>,
new_line: Option<u32>,
selected: bool,
width: usize,
) -> Vec<Line<'static>> {
let num = format!(
"{:>5} {:>5} ",
old_line.map(|n| n.to_string()).unwrap_or_default(),
new_line.map(|n| n.to_string()).unwrap_or_default(),
);
let (sign, code) = row.text.split_at(1);
let bg = if selected {
Some(self.diff_cursor_bg())
} else {
match kind {
LineKind::Addition => Some(theme().add_bg),
LineKind::Deletion => Some(theme().del_bg),
LineKind::Context => None,
}
};
let sign_color = match kind {
LineKind::Addition => theme().added,
LineKind::Deletion => theme().removed,
LineKind::Context => theme().muted,
};
let with_bg = |st: Style| match bg {
Some(b) => st.bg(b),
None => st,
};
let budget = width.saturating_sub(Self::UNI_PREFIX);
let runs = self.hl.runs(row.file_idx, code);
let wrapped = wrap_runs(&runs, budget, bg);
let mut out = Vec::with_capacity(wrapped.len());
for (i, (code_spans, code_w)) in wrapped.into_iter().enumerate() {
let mut spans: Vec<Span<'static>> = Vec::new();
if i == 0 {
spans.push(Span::styled(
num.clone(),
with_bg(Style::default().fg(theme().muted)),
));
spans.push(Span::styled(
sign.to_string(),
with_bg(Style::default().fg(sign_color)),
));
} else {
spans.push(Span::styled(
" ".repeat(Self::UNI_PREFIX),
with_bg(Style::default()),
));
}
// `code_w` is the visual line's content width, returned by wrap_runs.
spans.extend(code_spans);
let used = Self::UNI_PREFIX + code_w;
// Fill the rest so the tint / selection spans the whole line
// (only when there is a background to extend, matching `row_to_line`).
if bg.is_some() && used < width {
spans.push(Span::styled(
" ".repeat(width - used),
with_bg(Style::default()),
));
}
out.push(Line::from(spans));
}
out
}
pub(super) fn row_to_line(&self, row: &Row, selected: bool, width: usize) -> Line<'static> {
match &row.kind {
RowKind::FileHeader => {
let st = Style::default()
.fg(theme().text_strong)
.bg(theme().file_header_bg)
.add_modifier(Modifier::BOLD);
let text = format!("▌ {}", row.text);
let pad = width.saturating_sub(str_width(&text));
Line::from(vec![
Span::styled(text, st),
Span::styled(" ".repeat(pad), st),
])
}
RowKind::HunkHeader => Line::from(Span::styled(
row.text.clone(),
Style::default()
.fg(theme().faint)
.add_modifier(Modifier::ITALIC),
)),
RowKind::Line {
kind,
old_line,
new_line,
} => {
let num = format!(
"{:>5} {:>5} ",
old_line.map(|n| n.to_string()).unwrap_or_default(),
new_line.map(|n| n.to_string()).unwrap_or_default(),
);
let (sign, code) = row.text.split_at(1);
let bg = if selected {
Some(self.diff_cursor_bg())
} else {
match kind {
LineKind::Addition => Some(theme().add_bg),
LineKind::Deletion => Some(theme().del_bg),
LineKind::Context => None,
}
};
let sign_color = match kind {
LineKind::Addition => theme().added,
LineKind::Deletion => theme().removed,
LineKind::Context => theme().muted,
};
let with_bg = |st: Style| match bg {
Some(b) => st.bg(b),
None => st,
};
let mut used = str_width(&num) + 1;
let mut spans = vec![
Span::styled(num, with_bg(Style::default().fg(theme().muted))),
Span::styled(sign.to_string(), with_bg(Style::default().fg(sign_color))),
];
// Highlighted code, with the diff background tint behind it.
let hl = self.hl.runs(row.file_idx, code);
for (c, s) in hl.iter() {
used += str_width(s);
spans.push(Span::styled(s.clone(), with_bg(Style::default().fg(*c))));
}
// Fill the rest so the tint / selection spans the whole line.
if bg.is_some() && used < width {
spans.push(Span::styled(
" ".repeat(width - used),
with_bg(Style::default()),
));
}
Line::from(spans)
}
RowKind::Comment(cl) => self.comment_line_to_line(cl, selected, width),
RowKind::Composer(cl) => self.composer_line_to_line(cl, width),
}
}
}