62 lines
1.7 KiB
Rust
62 lines
1.7 KiB
Rust
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::style::Style;
|
|
use ratatui::widgets::{Block, Borders, List, ListItem, ListState};
|
|
|
|
fn render_list_into_buffer(
|
|
lines: &[&str],
|
|
marked: &[bool],
|
|
selected: Option<usize>,
|
|
width: u16,
|
|
height: u16,
|
|
) -> Buffer {
|
|
let backend = TestBackend::new(width.into(), height.into());
|
|
let mut terminal = Terminal::new(backend).expect("create terminal");
|
|
|
|
let lines_vec: Vec<ListItem> = lines
|
|
.iter()
|
|
.zip(marked.iter())
|
|
.map(|(text, marked)| {
|
|
let prefix = if *marked { 'x' } else { ' ' };
|
|
ListItem::new(format!("[{prefix}] {text}"))
|
|
})
|
|
.collect();
|
|
|
|
let mut state = ListState::default();
|
|
state.select(selected);
|
|
|
|
terminal
|
|
.draw(|f| {
|
|
let size = Rect::new(0, 0, width, height);
|
|
|
|
let list = List::new(lines_vec.clone())
|
|
.block(
|
|
Block::default()
|
|
.borders(Borders::ALL)
|
|
.title("Lines (space to mark, q to quit)"),
|
|
)
|
|
.highlight_style(Style::default());
|
|
|
|
f.render_stateful_widget(list, size, &mut state);
|
|
})
|
|
.expect("draw");
|
|
|
|
// Extract the underlying backend buffer (clone to own it)
|
|
let backend = terminal.backend();
|
|
let buf = backend.buffer().clone();
|
|
buf
|
|
}
|
|
|
|
#[test]
|
|
fn ui_snapshot_basic() {
|
|
let lines = ["first", "second", "third"];
|
|
let marked = [false, true, false];
|
|
let buf = render_list_into_buffer(&lines, &marked, Some(1), 40, 10);
|
|
|
|
// Use the buffer's debug representation for snapshotting
|
|
let out = format!("{buf:#?}");
|
|
insta::assert_snapshot!(out);
|
|
}
|