Lines
100 %
Functions
Branches
use ratatui::layout::{Position, Rect};
use super::*;
use crate::widgets::SelectOption;
fn two_lines() -> Grid<'static> {
let mut grid = Grid::default();
grid.push_cells(0, vec![Item::label("ab"), Item::text("x", 1, false)]);
grid.push_cells(
0,
vec![Item::label("abcd"), Item::text("hello world", 0, true)],
);
grid
}
fn select(labels: &[&str]) -> SelectWidget {
let mut select = SelectWidget::new();
select.set_options(
labels
.iter()
.map(|label| SelectOption {
id: label.to_lowercase(),
label: (*label).to_string(),
})
.collect(),
select
#[test]
fn columns_take_the_widest_cell_and_a_gap_apart() {
let rects = two_lines().cell_rects(Rect::new(2, 3, 40, 5), 0);
assert_eq!(rects[0], [Rect::new(2, 3, 4, 1), Rect::new(7, 3, 13, 1)]);
assert_eq!(rects[1], [Rect::new(2, 4, 4, 1), Rect::new(7, 4, 13, 1)]);
fn a_text_cell_reserves_twelve_and_a_select_has_room_for_its_mark() {
assert_eq!(Item::text("", 0, false).width(), 12);
assert_eq!(Item::select(&select(&["Apple"]), false).width(), 9);
assert_eq!(Item::select(&select(&["Banana"]), false).width(), 9);
fn an_empty_label_takes_no_column_and_no_gap() {
grid.push_cells(0, vec![Item::label(""), Item::label("a"), Item::label("")]);
let rects = grid.cell_rects(Rect::new(0, 0, 10, 1), 0);
assert_eq!(rects[0][1], Rect::new(0, 0, 1, 1));
assert_eq!(rects[0][2].width, 0);
assert_eq!(grid.width(), 1);
fn indents_align_their_own_columns() {
grid.push_cells(0, vec![Item::label("wide label")]);
grid.push_cells(4, vec![Item::label("a"), Item::label("b")]);
let rects = grid.cell_rects(Rect::new(0, 0, 40, 2), 0);
assert_eq!(rects[1], [Rect::new(4, 1, 1, 1), Rect::new(6, 1, 1, 1)]);
fn scrolling_moves_the_lines_up_and_hides_those_out_of_view() {
let rects = two_lines().cell_rects(Rect::new(0, 0, 40, 1), 1);
assert!(rects[0].iter().all(|rect| rect.width == 0));
assert_eq!(rects[1][0], Rect::new(0, 0, 4, 1));
let rects = two_lines().cell_rects(Rect::new(0, 0, 40, 1), 0);
assert!(rects[1].iter().all(|rect| rect.height == 0));
fn a_narrow_area_clips_the_cells_instead_of_panicking() {
let area = Rect::new(1, 1, 7, 2);
let rects = two_lines().cell_rects(area, 0);
for rect in rects.iter().flatten() {
assert_eq!(rect.intersection(area), *rect, "{rect:?}");
assert_eq!(rects[0][1].x, rects[1][1].x, "the group shifts together");
let nothing = two_lines().cell_rects(Rect::new(0, 0, 0, 0), 0);
assert!(nothing.iter().flatten().all(|rect| rect.area() == 0));
fn a_text_line_spans_from_its_indent_to_the_edge() {
grid.push_note(4, "(no tags)");
let rects = grid.cell_rects(Rect::new(0, 0, 30, 1), 0);
assert_eq!(rects[0], [Rect::new(4, 0, 26, 1)]);
assert_eq!(grid.width(), 13);
fn a_title_goes_first_and_pushes_the_lines_in_and_the_focus_down() {
let mut grid = two_lines();
grid.focus_line = 1;
let grid = grid.titled(" Splits:", true);
assert_eq!(grid.focus_line, 2);
let rects = grid.cell_rects(Rect::new(0, 0, 40, 3), 0);
assert_eq!(rects[0], [Rect::new(0, 0, 40, 1)]);
assert_eq!(rects[1][0].x, GRID_INDENT);
fn the_focused_text_cell_has_the_caret_at_its_cursor() {
let grid = two_lines();
assert_eq!(grid.caret(&rects), Some(Position::new(6, 1)));
assert_eq!(grid.anchor(&rects), None);
fn a_select_has_a_caret_only_while_open_and_anchors_its_list_at_its_cell() {
let mut open = select(&["Apple", "Banana"]);
grid.push_cells(0, vec![Item::label("1."), Item::select(&open, true)]);
let rects = grid.cell_rects(Rect::new(0, 0, 40, 1), 0);
assert_eq!(grid.caret(&rects), None);
assert_eq!(grid.anchor(&rects), Some(Rect::new(3, 0, 9, 1)));
open.open_list();
open.filter_push('b');
assert_eq!(grid.caret(&rects), Some(Position::new(5, 0)));
fn a_select_shows_its_filter_and_keeps_the_width_of_its_label() {
let mut select = select(&["Banana"]);
select.open_list();
select.filter_push('b');
let item = Item::select(&select, true);
assert_eq!(item.text, "b");
assert_eq!(item.width(), 9);
assert_eq!(Item::select(&SelectWidget::new(), false).text, "?");
fn the_caret_is_gone_when_the_focused_cell_has_no_room() {
let rects = grid.cell_rects(Rect::new(0, 0, 0, 2), 0);
fn a_select_keeps_the_width_of_its_widest_option_whatever_is_typed() {
let mut pick = select(&["Cash", "Cafe", "Expenses:Food"]);
let closed = Item::select(&pick, false).width();
pick.open_list();
pick.filter_push('c');
pick.filter_push('a');
assert_eq!(Item::select(&pick, true).width(), closed);
assert_eq!(closed, 16);
fn a_text_cell_has_a_column_for_its_end_cursor() {
assert_eq!(Item::text("1234567", 7, true).width(), 12);
assert_eq!(Item::text("123456789012", 12, true).width(), 14);
fn long_row(focus: Option<usize>) -> Grid<'static> {
let item = |text: &str, at: usize| Item::text(text, 0, focus == Some(at));
vec![
Item::label("1."),
item("aaaaaaaaaaaaaaaaaaaa", 1),
item("bbbbbbbbbbbbbbbbbbbb", 2),
item("cccccccccccccccccccc", 3),
],
grid.push_cells(2, vec![Item::label("2."), item("x", 9)]);
fn a_focused_cell_past_the_edge_shifts_its_line_just_enough_and_its_caret_follows() {
let area = Rect::new(0, 0, 40, 2);
let grid = long_row(Some(3));
let rects = grid.cell_rects(area, 0);
assert_eq!(rects[0][3].right(), 40);
assert_eq!(rects[0][3].width, 22);
assert_eq!(grid.caret(&rects), Some(Position::new(19, 0)));
assert_eq!(
rects[1],
long_row(None).cell_rects(area, 0)[1],
"other lines stay"
fn a_cell_cut_at_the_left_edge_is_hidden_and_one_cut_at_the_right_is_clipped() {
let rects = long_row(Some(3)).cell_rects(Rect::new(5, 0, 40, 2), 0);
assert_eq!(rects[0][0].width, 0);
assert_eq!(rects[0][2].width, 0, "the cell cut on the left");
assert_eq!(rects[0][3].right(), 45);
let rects = long_row(None).cell_rects(Rect::new(5, 0, 40, 2), 0);
assert!(
rects[0][2].width > 0 && rects[0][2].width < 21,
"clipped on the right"
fn focus_back_on_the_first_cell_returns_the_shift_to_zero() {
long_row(Some(1)).cell_rects(area, 0),
long_row(None).cell_rects(area, 0)
fn a_cell_wider_than_the_area_starts_at_its_edge() {
let rects = long_row(Some(1)).cell_rects(Rect::new(0, 0, 10, 2), 0);
assert_eq!(rects[0][1], Rect::new(0, 0, 10, 1));
fn the_list_anchor_follows_the_shift() {
let mut pick = select(&["Apple"]);
let wide = || Item::text("w".repeat(40), 0, false);
grid.push_cells(0, vec![wide(), Item::select(&pick, true)]);
let anchor = grid.anchor(&rects).expect("an anchor");
assert_eq!(anchor.right(), 40);
assert!(anchor.x < 42);
fn a_shifted_group_moves_whole_and_other_groups_stay() {
let text = |text: &str, focused: bool| Item::text(text, 0, focused);
let wide = "w".repeat(30);
grid.push_cells(0, vec![Item::label("split"), text("s", false)]);
grid.push_cells(4, vec![Item::label("Name"), Item::label("Value")]);
grid.push_cells(4, vec![text(&wide, false), text("v", true)]);
grid.push_cells(4, vec![text("a", false), text("b", false)]);
let rects = grid.cell_rects(Rect::new(0, 0, 40, 4), 0);
let flat = grid.clone();
let area_far = flat.cell_rects(Rect::new(0, 0, 400, 4), 0);
assert_eq!(rects[0], area_far[0], "the split row above stays");
let shifts: Vec<u16> = (1..4)
.map(|line| area_far[line][1].x - rects[line][1].x)
.collect();
shifts.iter().all(|&by| by == shifts[0] && by > 0),
"{shifts:?}"
assert_eq!(rects[1][1].x, rects[2][1].x);
assert_eq!(rects[3][1].x, rects[2][1].x);
fn a_clipped_tail_item_shows_the_end_after_an_ellipsis_until_focused() {
let rect = Rect::new(0, 0, 12, 1);
let tail = Item::text("Expenses:Food:Groceries", 0, false).tail();
assert_eq!(tail.shown(rect), "…Groceries");
let mut focused = Item::text("Expenses:Food:Groceries", 0, true).tail();
assert_eq!(focused.shown(rect), "Expenses:Food:Groceries");
focused.focused = false;
assert_eq!(focused.shown(rect), "…Groceries");
tail.shown(Rect::new(0, 0, 40, 1)),
"Expenses:Food:Groceries"
fn an_item_without_the_tail_rule_keeps_its_text() {
let item = Item::text("Expenses:Food:Groceries", 0, false);
item.shown(Rect::new(0, 0, 12, 1)),
fn a_combining_accent_at_the_cut_is_dropped_not_put_on_the_ellipsis() {
let item = Item::text("Expenses:Cafe\u{301}s", 0, false).tail();
let shown = item.shown(Rect::new(0, 0, 4, 1));
assert_eq!(shown, "…s");
fn a_wide_text_never_exceeds_the_room() {
let item = Item::text("資産:銀行:普通預金口座", 0, false).tail();
for width in 3..30 {
let shown = item.shown(Rect::new(0, 0, width, 1));
let cells = Span::raw(shown.as_ref()).width();
assert!(cells <= usize::from(width - 2), "{width}: {shown}");