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
//! Public frame-capture types for snapshot and visual tests.
use std::fmt::Write;
#[cfg(feature = "ui-snapshot-png")]
use std::path::PathBuf;
#[cfg(feature = "ui-snapshot-png")]
use std::sync::Arc;
use crate::style::ansi::write_cell_style_sgr;
use crate::style::{Color, Rect, Style};
#[cfg(feature = "ui-snapshot-png")]
mod png;
/// Captured terminal cell data converted to crate-owned style primitives.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapturedCell {
/// Rendered symbol at this cell.
pub symbol: String,
/// Foreground color.
pub fg: Color,
/// Background color.
pub bg: Color,
/// Underline color.
pub underline_color: Color,
/// Text modifiers active on this cell.
pub modifiers: CellModifiers,
}
/// Boolean modifier flags extracted from a rendered terminal cell.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CellModifiers {
/// Bold text.
pub bold: bool,
/// Dim text.
pub dim: bool,
/// Italic text.
pub italic: bool,
/// Underlined text.
pub underline: bool,
/// Reverse-video text.
pub reverse: bool,
/// Strikethrough text.
pub strikethrough: bool,
}
/// Cursor metadata captured from a rendered frame.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CursorState {
/// Cursor column.
pub x: u16,
/// Cursor row.
pub y: u16,
/// Whether the cursor should be shown.
pub visible: bool,
}
/// Complete frame snapshot produced by [`crate::TestBackend::capture_frame`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapturedFrame {
/// Viewport used for render/layout.
pub viewport: Rect,
/// Captured frame width.
pub width: u16,
/// Captured frame height.
pub height: u16,
/// Flattened row-major cell buffer (`width * height`).
pub cells: Vec<CapturedCell>,
/// Cursor state, when a widget requested cursor placement.
pub cursor: Option<CursorState>,
}
/// Options for rendering a [`CapturedFrame`] as PNG bytes.
#[cfg(feature = "ui-snapshot-png")]
#[derive(Clone, Debug)]
pub struct PngOptions {
/// Base width of each terminal cell before applying [`Self::scale`].
///
/// Defaults to `8`; zero is clamped to `1` while rendering.
pub cell_width: u16,
/// Base height of each terminal cell before applying [`Self::scale`].
///
/// Defaults to `16`; zero is clamped to `1` while rendering.
pub cell_height: u16,
/// Multiplier applied to cell dimensions while rendering.
///
/// Defaults to `2`; zero is clamped to `1` while rendering.
pub scale: u16,
/// Foreground color used when a cell foreground resolves to reset or transparent.
pub default_fg: Color,
/// Background color used when a cell background resolves to reset, transparent, or backdrop.
pub default_bg: Color,
/// Whether to draw a cursor outline when the captured frame contains a visible cursor.
///
/// Defaults to `true`.
pub render_cursor: bool,
/// Text renderer used for non-box/block glyphs.
///
/// Defaults to [`PngTextRenderer::Auto`], which tries a system font and falls back to bitmap.
pub text_renderer: PngTextRenderer,
/// Preferred system font family for font-backed rendering.
pub font_family: Option<Arc<str>>,
/// Explicit font file path for font-backed rendering.
pub font_path: Option<PathBuf>,
}
/// Text renderer used when converting a captured frame to PNG bytes.
#[cfg(feature = "ui-snapshot-png")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PngTextRenderer {
/// Try font-backed rendering and fall back to bitmap rendering when no font is available.
#[default]
Auto,
/// Try font-backed rendering and fall back to bitmap rendering when loading or glyph lookup fails.
Font,
/// Always use the built-in bitmap font renderer.
Bitmap,
}
#[cfg(feature = "ui-snapshot-png")]
impl Default for PngOptions {
fn default() -> Self {
Self {
cell_width: 8,
cell_height: 16,
scale: 2,
default_fg: Color::White,
default_bg: Color::Black,
render_cursor: true,
text_renderer: PngTextRenderer::Auto,
font_family: None,
font_path: None,
}
}
}
impl CapturedCell {
fn style(&self) -> Style {
Style {
fg: Some(self.fg.into()),
bg: Some(self.bg.into()),
fg_transform: None,
bg_transform: None,
contrast_policy: None,
bold: Some(self.modifiers.bold),
dim: Some(self.modifiers.dim),
italic: Some(self.modifiers.italic),
underline: Some(self.modifiers.underline),
reverse: Some(self.modifiers.reverse),
strikethrough: Some(self.modifiers.strikethrough),
underline_color: Some(self.underline_color.into()),
dim_amount: None,
tint: None,
}
}
/// Write ANSI SGR sequences for this cell's style into `out`.
pub fn write_ansi_style(&self, out: &mut String) {
let m = &self.modifiers;
write_cell_style_sgr(
out,
crate::style::ansi::CellStyleSgr {
fg: self.fg,
bg: self.bg,
underline_color: self.underline_color,
bold: m.bold,
dim: m.dim,
italic: m.italic,
underline: m.underline,
reverse: m.reverse,
strikethrough: m.strikethrough,
},
);
}
/// Returns true when this cell's visual style matches `other`.
pub fn ansi_style_matches(&self, other: &CapturedCell) -> bool {
self.fg == other.fg
&& self.bg == other.bg
&& self.underline_color == other.underline_color
&& self.modifiers == other.modifiers
}
}
impl CapturedFrame {
/// Encode this frame as a PNG byte buffer.
///
/// With [`PngTextRenderer::Auto`], text is rendered with a discovered system
/// font when available and falls back to the built-in bitmap renderer.
///
/// If PNG encoding fails, returns an empty buffer. Use [`Self::try_to_png`] when the
/// encoding error should be surfaced to the caller.
#[cfg(feature = "ui-snapshot-png")]
pub fn to_png(&self, options: &PngOptions) -> Vec<u8> {
png::encode_frame(self, options)
}
/// Encode this frame as PNG bytes and return any encoder error.
#[cfg(feature = "ui-snapshot-png")]
pub fn try_to_png(&self, options: &PngOptions) -> crate::Result<Vec<u8>> {
png::try_encode_frame(self, options)
.map_err(|err| std::io::Error::other(err.to_string()).into())
}
/// Returns captured text as newline-joined rows with trailing spaces trimmed.
pub fn plain_text(&self) -> String {
self.to_lines().join("\n")
}
/// Returns captured rows at full viewport width without trimming trailing spaces.
pub fn to_fixed_grid(&self) -> String {
self.to_fixed_grid_lines().join("\n")
}
/// Returns captured rows at full viewport width without trimming trailing spaces.
pub fn to_fixed_grid_lines(&self) -> Vec<String> {
(0..self.height)
.map(|y| {
let mut line = String::new();
for cell in self.row(y) {
line.push_str(&cell.symbol);
}
line
})
.collect()
}
/// Returns the frame rendered as a static ANSI string (full terminal repaint prelude).
pub fn to_ansi(&self) -> String {
self.to_ansi_diff(None)
}
/// Returns an ANSI string updating `prev` to this frame.
///
/// When `prev` is `None` or dimensions differ, emits a full clear + home cursor prelude
/// suitable for terminal backends (including xterm.js).
pub fn to_ansi_diff(&self, prev: Option<&CapturedFrame>) -> String {
let full_repaint = prev
.map(|p| p.width != self.width || p.height != self.height)
.unwrap_or(true);
let mut out = String::with_capacity(if full_repaint {
usize::from(self.width) * usize::from(self.height) * 12 + 64
} else {
usize::from(self.width) * usize::from(self.height) * 3 + 512
});
if full_repaint {
out.push_str("\x1b[2J\x1b[3J\x1b[H\x1b[?25l");
}
let w = usize::from(self.width);
let mut last_written: Option<(u16, u16)> = None;
let mut last_styled_cell: Option<&CapturedCell> = None;
for y in 0..self.height {
for x in 0..self.width {
let idx = usize::from(y) * w + usize::from(x);
let cell = &self.cells[idx];
if !full_repaint
&& let Some(prev_frame) = prev
&& prev_frame.cells[idx] == *cell
{
continue;
}
let contiguous = matches!(last_written, Some((lx, ly)) if ly == y && lx + 1 == x);
if !contiguous {
let _ = write!(out, "\x1b[{};{}H", y + 1, x + 1);
}
if !(contiguous
&& last_styled_cell.is_some_and(|prev_cell| prev_cell.ansi_style_matches(cell)))
{
cell.write_ansi_style(&mut out);
last_styled_cell = Some(cell);
}
out.push_str(&cell.symbol);
last_written = Some((x, y));
}
}
if let Some(c) = self.cursor.as_ref().filter(|c| c.visible) {
let _ = write!(out, "\x1b[{};{}H\x1b[?25h", c.y + 1, c.x + 1);
} else {
out.push_str("\x1b[?25l");
}
out
}
/// Returns captured rows with trailing spaces trimmed per row.
pub fn to_lines(&self) -> Vec<String> {
(0..self.height)
.map(|y| {
let mut line = String::new();
for cell in self.row(y) {
line.push_str(&cell.symbol);
}
line.trim_end_matches(' ').to_owned()
})
.collect()
}
/// Returns all cells for row `y`.
///
/// Panics if `y >= self.height`.
pub fn row(&self, y: u16) -> &[CapturedCell] {
assert!(y < self.height, "row y out of bounds");
let start = usize::from(y) * usize::from(self.width);
let end = start + usize::from(self.width);
&self.cells[start..end]
}
/// Returns a single cell at `(x, y)`.
///
/// Panics if `x >= self.width` or `y >= self.height`.
pub fn cell(&self, x: u16, y: u16) -> &CapturedCell {
assert!(x < self.width, "cell x out of bounds");
&self.row(y)[usize::from(x)]
}
/// Returns each row grouped into contiguous `(text, style)` runs.
pub fn styled_lines(&self) -> Vec<Vec<(String, Style)>> {
(0..self.height)
.map(|y| {
let mut runs: Vec<(String, Style)> = Vec::new();
for cell in self.row(y) {
let style = cell.style();
if let Some((text, run_style)) = runs.last_mut()
&& *run_style == style
{
text.push_str(&cell.symbol);
} else {
runs.push((cell.symbol.clone(), style));
}
}
runs
})
.collect()
}
}