Lines
99.37 %
Functions
100 %
Branches
//! The `▐↓▌` history mark on form select fields, in both presets, and `▼` kept in grid cells.
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use ratatui::style::Color;
use tvui::{HISTORY_CELLS, Place, Role, Theme};
use super::body::FormBody;
use super::render_field;
use super::select_tests::{finds, options, select_form, select_mut};
use crate::app::App;
use crate::form::Form;
use crate::hits::{Hits, Surface, Target};
use crate::modal::Modal;
use crate::overlay::Placement;
use crate::widgets::Widget;
const PRESETS: [&str; 2] = ["dn", "turbo"];
const SIZE: (u16, u16) = (100, 30);
fn app_with(preset: &str, form: Form) -> (App, Theme) {
let mut app = crate::test_support::app();
let Ok(theme) = Theme::preset(preset) else {
panic!("the {preset} preset parses");
};
app.theme = theme.clone();
app.overlays.push(Modal::Form(form));
(app, theme)
}
fn drawn(app: &App) -> (Buffer, Hits) {
crate::test_support::draw_frame(app, SIZE.0, SIZE.1)
fn look(buf: &Buffer, at: Position) -> (Option<Color>, Option<Color>) {
let style = buf[at].style();
(style.fg, style.bg)
fn want(theme: &Theme, role: Role) -> (Option<Color>, Option<Color>) {
let style = theme.style(role);
fn mark_rect(hits: &Hits) -> Rect {
let found = hits
.entries()
.into_iter()
.find(|(_, target)| matches!(target, Target::Mark { .. }));
found.map(|(rect, _)| rect).expect("a mark was drawn")
fn long_filter() -> Form {
let mut form = select_form(options(&["Apple", "Banana"]));
for _ in 0..60 {
select_mut(&mut form).filter_push('x');
form
#[test]
fn a_select_field_ends_in_the_history_mark_cell_by_cell() {
for preset in PRESETS {
let (app, theme) = app_with(preset, select_form(options(&["Apple", "Banana"])));
let (buf, hits) = drawn(&app);
let start = finds(&buf, "▐")[0];
for (dx, glyph) in ["▐", "↓", "▌"].into_iter().enumerate() {
let at = Position::new(start.x + u16::try_from(dx).unwrap_or(0), start.y);
assert_eq!(buf[at].symbol(), glyph, "{preset} {dx}");
assert_eq!(
mark_rect(&hits),
Rect::new(start.x, start.y, HISTORY_CELLS, 1),
"{preset}"
);
let before = Position::new(start.x - 1, start.y);
assert_eq!(look(&buf, before), want(&theme, Role::InputFocused));
assert!(finds(&buf, "▼").is_empty(), "{preset}");
fn the_input_line_of_a_select_is_shorter_by_the_mark_and_keeps_its_arrow() {
let (app, theme) = app_with(preset, long_filter());
let mark = mark_rect(&hits);
let field = hits
.find_map(|(rect, target)| (target == Target::Field(0)).then_some(rect));
let field = field.expect("the field was drawn");
assert_eq!(mark.right() + 1, field.right(), "{preset}");
let left = finds(&buf, "◄");
assert_eq!(left.len(), 1, "{preset}");
assert_eq!(left[0].y, mark.y);
assert!(left[0].x < mark.x, "{preset}");
buf[left[0]].style().fg,
theme.style(Role::InputArrow).fg,
assert!(finds(&buf, "►").is_empty(), "{preset}");
fn a_grid_cell_keeps_its_triangle_and_has_no_history_mark() {
let form = Form::transaction_create();
let selects = form
.fields
.iter()
.filter(|field| matches!(field.widget, Widget::Select(_)))
.count();
let (app, _) = app_with(preset, form);
let (buf, _) = drawn(&app);
assert!(!finds(&buf, "▼").is_empty(), "{preset}");
assert_eq!(finds(&buf, "▐").len(), selects, "{preset}");
fn moved_by(app: &mut App, dx: i32, dy: i32) {
let (_, hits) = drawn(app);
let auto = hits.dialog().expect("a dialog was drawn").auto;
let place = Place {
x: i32::from(auto.x) + dx,
y: i32::from(auto.y) + dy,
..Place::from(auto)
app.overlays.set_placement(Placement::of(place, auto));
fn the_mark_moves_with_a_moved_dialog() {
let (mut app, _) = app_with(preset, select_form(options(&["Apple", "Banana"])));
let before = mark_rect(&drawn(&app).1);
moved_by(&mut app, -6, 3);
let after = mark_rect(&hits);
assert_eq!((after.x + 6, after.y), (before.x, before.y + 3), "{preset}");
assert_eq!(buf[(after.x, after.y)].symbol(), "▐", "{preset}");
fn a_list_opened_in_a_dialog_moved_to_the_edge_stays_on_the_screen_below_the_mark() {
let wide = "W".repeat(60);
let mut form = select_form(options(&["Apple", &wide, "Cherry"]));
select_mut(&mut form).open_list();
let (mut app, _) = app_with(preset, form);
let auto = drawn(&app).1.dialog().expect("a dialog was drawn").auto;
moved_by(&mut app, i32::from(SIZE.0 - auto.right()) + 2, 4);
let (_, hits) = drawn(&app);
let list = hits
.find_map(|(rect, target)| (target == Target::Body(Surface::Options)).then_some(rect));
let list = list.expect("the list was drawn");
assert_eq!(list.y, mark.y + 1, "{preset}");
assert!(list.right() <= SIZE.0, "{preset} {list:?}");
assert!(list.x < mark.right(), "{preset} {list:?}");
fn the_cursor_of_an_open_select_stays_left_of_the_mark() {
let mut form = long_filter();
let mut terminal =
ratatui::Terminal::new(ratatui::backend::TestBackend::new(SIZE.0, SIZE.1))
.expect("test terminal");
let mut hits = Hits::default();
terminal
.draw(|frame| crate::draw::draw(frame, &app, &mut hits))
.expect("the frame draws");
let cursor = terminal.backend().cursor_position();
assert!(cursor.x < mark_rect(&hits).x, "{preset} {cursor:?}");
fn widest_of(field: &crate::form::Field) -> usize {
let body = FormBody {
title: "",
notices: Vec::new(),
blocks: vec![render_field(0, field, 0, 4, None, None)],
tail: Vec::new(),
focus: 0,
button: None,
pressed: None,
dragging: false,
active: true,
popup: None,
body.widest()
fn a_select_is_wider_than_a_text_field_by_its_mark() {
let mut form = select_form(options(&["Apple"]));
let select = form.fields.remove(0);
let text = crate::form::Field {
key: None,
label: "pick",
widget: Widget::Text(crate::widgets::Editor::with_buffer("")),
widest_of(&select),
widest_of(&text) + usize::from(HISTORY_CELLS)