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
//! Render for Preview.
use super::{
Constraint, Direction, EntryKind, Frame, Layout, Line, Modifier, Paragraph, Preview,
PreviewContent, Rect, Span, Style, Theme, UnicodeWidthStr, Wrap, chip_line, fit_middle, lens,
};
impl Preview {
pub fn render(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) {
let sem = &theme.semantic;
// While finding, the title carries the query: `main.rs /ratatui`.
let title = match &self.find {
Some(f) => format!(" {} /{} ", self.title, f.query),
None => format!(" {} ", self.title),
};
let mut block = Self::pane_block(title, self.focused, theme);
if let Some(readout) = self.readout() {
block = block.title_bottom(
Line::from(Span::styled(
format!(" {readout} "),
Style::default().fg(sem.subtext0),
))
.right_aligned(),
);
}
// Footer: language + line count for real file content.
if self.numbered {
let meta = match &self.lang {
Some(lang) => format!(" {lang} · {} lines ", self.line_count),
None => format!(" {} lines ", self.line_count),
};
block = block.title_bottom(
Line::from(Span::styled(meta, Style::default().fg(sem.overlay0))).left_aligned(),
);
}
let cursored = self.line_count > 0;
let cursor = self.cursor;
let mut lines: Vec<Line> = match &self.content {
PreviewContent::Prose(text) => {
let inner = block.inner(area);
frame.render_widget(block, area);
self.prose_viewport.render(frame, area, inner, text, theme);
return;
}
PreviewContent::Empty => {
vec![Line::from(Span::styled(
"nothing selected",
Style::default().fg(sem.overlay0),
))]
}
PreviewContent::Highlighted(lines) => lines.clone(),
PreviewContent::Text(text) => text
.lines()
.map(|l| {
Line::from(Span::styled(
l.replace('\t', " "),
Style::default().fg(sem.text),
))
})
.collect(),
PreviewContent::DirSummary(children) => children
.iter()
.map(|e| match e.kind {
EntryKind::File => {
Line::from(Span::styled(e.name.clone(), Style::default().fg(sem.file)))
}
_ => Line::from(Span::styled(
format!("{}/", e.name),
Style::default()
.fg(sem.directory)
.add_modifier(Modifier::BOLD),
)),
})
.collect(),
PreviewContent::Binary { size } => vec![Line::from(Span::styled(
format!("binary file · {} bytes", size),
Style::default().fg(sem.warning),
))],
};
// Find chips: split spans at match boundaries; matches ride the
// search_match chip (grep-view parity), the current match the
// warning accent so n/N reads at a glance.
if self.numbered
&& let Some(find) = &self.find
&& !find.matches.is_empty()
{
let match_style = Style::default()
.fg(sem.crust)
.bg(sem.search_match)
.add_modifier(Modifier::BOLD);
let current_style = Style::default()
.fg(sem.crust)
.bg(sem.warning)
.add_modifier(Modifier::BOLD | Modifier::UNDERLINED);
for (i, line) in lines.iter_mut().enumerate() {
let ranges: Vec<(usize, usize, bool)> = find
.matches
.iter()
.enumerate()
.filter(|(_, m)| m.line == i)
.map(|(idx, m)| (m.start, m.end, idx == find.current))
.collect();
if !ranges.is_empty() {
*line = chip_line(line, &ranges, match_style, current_style);
}
}
}
// Selection tint on the cursor line (text content only).
if cursored && let Some(line) = lines.get_mut(cursor) {
line.style = Style::default().bg(sem.selection_bg);
}
// Visual-lines (vim V): the range tints like the cursor line.
if let Some((lo, hi)) = self.visual_range() {
for i in (lo as usize - 1)..=(hi as usize - 1).min(lines.len().saturating_sub(1)) {
if let Some(line) = lines.get_mut(i) {
line.style = Style::default().bg(sem.selection_bg);
}
}
}
// Line-number gutter, bat/helix style: sign column (▶ marks
// the cursor line, tuicr-style) + space + right-aligned dim
// numbers + a dim `│` divider before the content. The cursor
// line's number reads bold (vim CursorLineNr).
if self.numbered {
let width = self.line_count.max(1).to_string().len();
for (i, line) in lines.iter_mut().enumerate() {
let cursor_line = i == cursor;
let num_style = if cursor_line {
Style::default().fg(sem.text).add_modifier(Modifier::BOLD)
} else {
Style::default().fg(sem.overlay0)
};
line.spans
.insert(0, Span::styled(format!("{:>width$}", i + 1), num_style));
line.spans.insert(
0,
Span::styled(
if cursor_line { "▶ " } else { " " },
if cursor_line {
Style::default()
.fg(sem.border_focused)
.add_modifier(Modifier::BOLD)
} else {
Style::default()
},
),
);
line.spans
.insert(2, Span::styled(" │ ", Style::default().fg(sem.overlay0)));
}
}
// Blame lens (plans/0016 M1c): a margin before the gutter —
// sha + author at each run's first line, a dim dot leader on
// continuations (fugitive-style runs, not a per-line column).
if self.numbered
&& let Some(blame) = &self.blame
{
for (i, line) in lines.iter_mut().enumerate() {
let spans: Vec<Span<'static>> = match blame.get(i).cloned().flatten() {
Some(m) => vec![
Span::styled(m.sha, Style::default().fg(sem.warning)),
Span::styled(
// 8 cells: real names truncate, short ones pad.
format!(" {:<8.8}", m.author),
Style::default().fg(sem.subtext0),
),
Span::styled(" │ ".to_string(), Style::default().fg(sem.overlay0)),
],
None => vec![Span::styled(
// 19 cells: sha(7) + ' ' + author(8) + ' │ '.
" · ".to_string() + " │ ",
Style::default().fg(sem.overlay0),
)],
};
for (j, sp) in spans.into_iter().enumerate() {
line.spans.insert(j, sp);
}
}
}
// The header band (GitHub's file header, plans/0016 M1b): on
// file content one row under the border carries the full path
// — plus the at-commit context on the right when viewing
// history — on a surface0 strip so it reads as chrome, not
// content.
let band = self.numbered && self.band_path.is_some();
let inner = block.inner(area);
self.viewport = usize::from(inner.height.saturating_sub(band as u16));
self.clamp_scroll(self.viewport);
frame.render_widget(block, area);
let content_area = if band {
let rows = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(1), Constraint::Min(1)])
.split(inner);
let path = self.band_path.clone().unwrap_or_default();
let mut spans = vec![Span::styled(
format!(" {path}"),
Style::default()
.fg(sem.text)
.bg(sem.surface0)
.add_modifier(Modifier::BOLD),
)];
let left_w = 1 + UnicodeWidthStr::width(path.as_str());
if let Some(ctx) = &self.band_context {
// sha · subject · author · date — sha in the accent the
// history lens uses, the rest dim. Under width pressure
// the tail sheds date, then author; the sha and subject
// stay. One space of separation from the path, always.
let room = (inner.width as usize).saturating_sub(left_w + 1);
let full = format!(
"{} · {} · {} · {}",
ctx.sha, ctx.subject, ctx.author, ctx.date
);
let short = format!("{} · {} · {}", ctx.sha, ctx.subject, ctx.author);
let shorter = format!("{} · {}", ctx.sha, ctx.subject);
let fits = |s: &str| UnicodeWidthStr::width(s) <= room;
let text = if fits(&full) {
full
} else if fits(&short) {
short
} else if fits(&shorter) {
shorter
} else {
fit_middle(&ctx.sha, room)
};
let pad = room.saturating_sub(UnicodeWidthStr::width(text.as_str()));
spans.push(Span::styled(
" ".repeat(pad + 1),
Style::default().bg(sem.surface0),
));
// Segmented: sha hot, the rest dim — the row reads at a
// glance instead of shouting in one color.
spans.push(Span::styled(
ctx.sha
.chars()
.take(lens::sha_len(&text, ctx))
.collect::<String>(),
Style::default().fg(sem.warning).bg(sem.surface0),
));
spans.push(Span::styled(
text[lens::sha_len(&text, ctx)..].to_string(),
Style::default().fg(sem.subtext0).bg(sem.surface0),
));
}
let band_line = Line::from(spans);
let band_w: usize = band_line.spans.iter().map(|s| s.content.width()).sum();
let mut band_spans = band_line.spans;
band_spans.push(Span::styled(
" ".repeat((inner.width as usize).saturating_sub(band_w)),
Style::default().bg(sem.surface0),
));
frame.render_widget(Paragraph::new(Line::from(band_spans)), rows[0]);
rows[1]
} else {
inner
};
frame.render_widget(
Paragraph::new(lines.into_iter().skip(self.scroll).collect::<Vec<_>>())
.wrap(Wrap { trim: false }),
content_area,
);
// House style: anything that scrolls shows a scrollbar.
if cursored {
crate::components::scrollbar(
frame,
area,
self.viewport,
self.line_count,
self.scroll,
theme,
);
}
}
/// Border readout for text content: `3/41`, or `m/n · 3/41`
/// (match of matches · line of total) while a find is active.
pub(super) fn readout(&self) -> Option<String> {
if self.line_count == 0 {
return None;
}
let pos = format!("{}/{}", self.cursor + 1, self.line_count);
let pos = match self.visual_range() {
// VISUAL marker + the range — vim's -- VISUAL -- line.
Some((lo, hi)) => format!("VISUAL {lo}-{hi} · {pos}"),
None => pos,
};
match &self.find {
Some(f) if !f.query.is_empty() => {
let cur = if f.matches.is_empty() {
0
} else {
f.current + 1
};
Some(format!("{cur}/{} · {pos}", f.matches.len()))
}
_ => Some(pos),
}
}
}