Lines
100 %
Functions
Branches
use ratatui::style::Color;
use ratatui::widgets::StatefulWidget;
use super::*;
const PRESETS: [&str; 2] = ["dn", "turbo"];
fn theme(preset: &str) -> Theme {
Theme::preset(preset).expect("the preset parses")
}
fn render(line: InputLine<'_>, width: u16, from: Viewport) -> (Buffer, Viewport) {
let mut buf = Buffer::empty(Rect::new(0, 0, width, 1));
let mut viewport = from;
line.render(buf.area, &mut buf, &mut viewport);
(buf, viewport)
fn drawn_from(
text: &str,
cursor: usize,
width: u16,
focused: bool,
from: Viewport,
) -> (Buffer, Viewport) {
let theme = theme("dn");
render(InputLine::new(&theme, text, cursor, focused), width, from)
fn drawn(text: &str, cursor: usize, width: u16, focused: bool) -> Buffer {
drawn_from(text, cursor, width, focused, Viewport::default()).0
fn scrolled(text: &str, cursor: usize, width: u16) -> Viewport {
drawn_from(text, cursor, width, false, Viewport::default()).1
fn row(buf: &Buffer) -> String {
(0..buf.area.width)
.map(|x| buf[(x, 0)].symbol().to_string())
.collect()
fn col(text: &str, cursor: usize, width: u16) -> u16 {
let viewport = scrolled(text, cursor, width);
InputLine::cursor(Rect::new(3, 5, width, 1), text, cursor, viewport).x - 3
#[test]
fn short_text_starts_one_cell_in_and_the_cursor_follows_it() {
assert_eq!(row(&drawn("abc", 3, 8, false)), " abc ");
assert_eq!(col("abc", 3, 8), 4);
assert_eq!(col("abc", 1, 8), 2);
fn empty_text_puts_the_cursor_after_the_margin() {
assert_eq!(row(&drawn("", 0, 4, true)), " ");
assert_eq!(col("", 0, 4), 1);
fn text_exactly_filling_the_row_leaves_the_cursor_on_the_last_cell_unscrolled() {
assert_eq!(row(&drawn("abcd", 4, 6, true)), " abcd ");
assert_eq!(col("abcd", 4, 6), 5);
assert_eq!(row(&drawn("abcde", 5, 6, true)), "\u{25c4}bcde ");
assert_eq!(col("abcde", 5, 6), 5);
fn wide_characters_count_two_columns() {
assert_eq!(col("日本語", 2, 10), 5);
assert_eq!(col("日本語", 3, 6), 5);
let buf = drawn("日本語", 3, 6, true);
assert_eq!(buf[(1, 0)].symbol(), "本");
assert_eq!(buf[(3, 0)].symbol(), "語");
fn a_wide_character_that_does_not_fit_is_left_out() {
assert_eq!(row(&drawn("a日", 0, 2, false)), "a ");
assert_eq!(row(&drawn("ab日", 0, 5, false)), " ab \u{25ba}");
fn a_one_cell_area_keeps_the_cursor_in_it() {
assert_eq!(col("abc", 3, 1), 0);
assert_eq!(row(&drawn("abc", 3, 1, true)), " ");
assert_eq!(row(&drawn("abc", 3, 2, true)), "c ");
fn an_empty_area_draws_nothing_and_keeps_the_cursor_at_its_edge() {
assert_eq!(col("abc", 3, 0), 0);
let mut buf = Buffer::empty(Rect::new(0, 0, 3, 1));
let mut viewport = Viewport::default();
InputLine::new(&theme, "abc", 3, true).render(Rect::new(0, 0, 0, 1), &mut buf, &mut viewport);
assert_eq!(row(&buf), " ");
fn the_cursor_is_placed_inside_the_area_rows_and_columns() {
let at = InputLine::cursor(Rect::new(10, 4, 6, 1), "ab", 2, Viewport::default());
assert_eq!((at.x, at.y), (13, 4));
fn a_combining_mark_joins_the_cell_before_it() {
let buf = drawn("e\u{301}x", 0, 6, false);
assert_eq!(buf[(1, 0)].symbol(), "e\u{301}");
assert_eq!(buf[(2, 0)].symbol(), "x");
assert_eq!(col("e\u{301}x", 2, 6), 2);
fn a_huge_text_does_not_overflow_the_column_sum() {
let text = "日".repeat(40_000);
assert!(col(&text, 40_000, 10) < 10);
fn a_control_character_is_drawn_as_a_blank_one_cell_wide() {
assert_eq!(row(&drawn("a\tb", 0, 8, false)), " a b ");
assert_eq!(col("a\tb", 3, 8), 4);
fn at(text: &str, cursor: usize, width: u16, x: u16) -> usize {
let area = Rect::new(3, 5, width, 1);
InputLine::position_at(area, text, viewport, Position::new(3 + x, 5))
fn a_click_lands_on_the_character_under_it() {
assert_eq!(at("abcdef", 0, 8, 1), 0);
assert_eq!(at("abcdef", 0, 8, 4), 3);
fn a_click_past_the_end_lands_at_the_end() {
assert_eq!(at("abc", 3, 8, 7), 3);
assert_eq!(at("", 0, 8, 5), 0);
fn a_click_left_of_the_area_lands_on_the_first_visible_character() {
let area = Rect::new(3, 5, 8, 1);
assert_eq!(
InputLine::position_at(area, "abc", Viewport::default(), Position::new(0, 5)),
0
);
fn a_click_in_a_scrolled_field_counts_from_the_scroll() {
let text = "abcdefghij";
assert_eq!(at(text, 10, 6, 1), 6);
assert_eq!(at(text, 10, 6, 3), 8);
assert_eq!(at(text, 10, 6, 5), 10);
fn a_wide_character_takes_a_click_on_either_of_its_cells() {
let text = "a\u{4e2d}b";
assert_eq!(at(text, 0, 8, 2), 1);
assert_eq!(at(text, 0, 8, 3), 1);
assert_eq!(at(text, 0, 8, 4), 2);
fn position_at_is_the_inverse_of_cursor() {
for text in [
"",
"abc",
"abcdefghijkl",
"a\u{4e2d}b\u{4e2d}cdefg",
"\u{4e2d}\u{4e2d}\u{4e2d}\u{4e2d}",
] {
for width in [1, 2, 3, 5, 8] {
for cursor in 0..=text.chars().count() {
let point = InputLine::cursor(area, text, cursor, viewport);
let found = InputLine::position_at(area, text, viewport, point);
assert_eq!(found, cursor, "{text:?} width {width} cursor {cursor}");
fn the_scroll_stays_while_the_cursor_moves_inside_the_view() {
let text = "abcdefghijklmnop";
let (_, scrolled) = drawn_from(text, 16, 8, true, Viewport::default());
assert_eq!(scrolled.left, 10);
let (buf, kept) = drawn_from(text, 13, 8, true, scrolled);
assert_eq!(kept, scrolled);
assert_eq!(row(&buf), "\u{25c4}klmnop ");
fn the_scroll_moves_the_least_to_bring_the_cursor_back() {
let from = Viewport { top: 0, left: 11 };
let (buf, moved) = drawn_from(text, 8, 8, true, from);
assert_eq!(moved.left, 8);
assert_eq!(row(&buf), "\u{25c4}ijklmn\u{25ba}");
fn a_click_in_a_scrolled_field_keeps_the_view_when_drawn_again() {
let (_, viewport) = drawn_from(text, 16, 8, true, Viewport::default());
let clicked = InputLine::position_at(area, text, viewport, Position::new(3 + 3, 5));
assert_eq!(clicked, 12);
let (_, again) = drawn_from(text, clicked, 8, true, viewport);
assert_eq!(again, viewport);
assert_eq!(InputLine::cursor(area, text, clicked, again).x, 3 + 3);
fn style_of(buf: &Buffer, x: u16) -> (Option<Color>, Option<Color>) {
let style = buf[(x, 0)].style();
(style.fg, style.bg)
fn want(theme: &Theme, role: Role) -> (Option<Color>, Option<Color>) {
let style = theme.style(role);
fn long(cursor: usize, left: usize, width: u16, preset: &str) -> (Buffer, Theme) {
let theme = theme(preset);
let line = InputLine::new(&theme, "abcdefghij", cursor, true);
let (buf, _) = render(line, width, Viewport { top: 0, left });
(buf, theme)
fn an_empty_field_is_all_field_colour_in_both_presets() {
for preset in PRESETS {
let line = InputLine::new(&theme, "", 0, true);
let (buf, _) = render(line, 6, Viewport::default());
assert_eq!(row(&buf), " ", "{preset}");
for x in 0..6 {
style_of(&buf, x),
want(&theme, Role::InputFocused),
"{preset} {x}"
fn arrow_style(theme: &Theme) -> (Option<Color>, Option<Color>) {
(
theme.style(Role::InputArrow).fg,
theme.style(Role::InputFocused).bg,
)
fn the_left_arrow_marks_text_scrolled_off_the_left() {
let (buf, theme) = long(10, 7, 6, preset);
assert_eq!(row(&buf), "\u{25c4}ghij ", "{preset}");
assert_eq!(style_of(&buf, 0), arrow_style(&theme), "{preset}");
assert_eq!(style_of(&buf, 1), want(&theme, Role::InputFocused));
fn the_right_arrow_marks_text_running_off_the_right() {
let (buf, theme) = long(0, 0, 6, preset);
assert_eq!(row(&buf), " abcd\u{25ba}", "{preset}");
assert_eq!(style_of(&buf, 5), arrow_style(&theme), "{preset}");
fn both_arrows_show_while_text_runs_off_both_sides() {
let (buf, _) = long(5, 0, 6, "turbo");
assert_eq!(row(&buf), "\u{25c4}bcde\u{25ba}");
fn text_that_just_fits_shows_no_arrow() {
let line = InputLine::new(&theme, "abcd", 0, true);
assert_eq!(row(&buf), " abcd ");
fn the_cursor_stays_between_the_margin_and_the_last_cell_at_both_ends_of_the_scroll() {
let area = Rect::new(0, 0, 6, 1);
let (_, right) = drawn_from(text, 10, 6, true, Viewport::default());
assert_eq!(InputLine::cursor(area, text, 10, right).x, 5);
let (_, left) = drawn_from(text, 6, 6, true, right);
assert_eq!(left.left, 6);
assert_eq!(InputLine::cursor(area, text, 6, left).x, 1);
let (_, back) = drawn_from(text, 5, 6, true, left);
assert_eq!(InputLine::cursor(area, text, 5, back).x, 1);
fn the_cursor_never_leaves_the_text_cells() {
for cursor in 0..=text.len() {
let area = Rect::new(0, 0, 7, 1);
let viewport = scrolled(text, cursor, 7);
let x = InputLine::cursor(area, text, cursor, viewport).x;
assert!((1..=6).contains(&x), "cursor {cursor} at {x}");
fn a_field_too_narrow_for_two_arrows_draws_none() {
assert_eq!(row(&drawn("abcdef", 6, 2, true)), "f ");
assert_eq!(row(&drawn("abcdef", 0, 2, true)), "ab");
fn typed_then_erased() -> (Buffer, Viewport) {
let full = "abcdefghijklmnopqrstuvwxyz0123456789ABCD";
let (_, scrolled) = drawn_from(full, 40, 20, true, Viewport::default());
let kept = &full[..22];
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 1));
let mut viewport = scrolled;
InputLine::new(&theme, kept, 22, true).render(buf.area, &mut buf, &mut viewport);
fn erasing_scrolls_the_text_back_so_it_fills_the_field() {
let (buf, viewport) = typed_then_erased();
assert_eq!(viewport.left, 4);
assert_eq!(row(&buf), "\u{25c4}efghijklmnopqrstuv ");
fn a_wide_character_under_the_cursor_at_the_right_edge_is_drawn_whole() {
let (buf, viewport) = drawn_from("abcde\u{4e2d}", 5, 8, true, Viewport::default());
assert_eq!(viewport.left, 1);
assert_eq!(buf[(5, 0)].symbol(), "\u{4e2d}");
let (buf, viewport) = drawn_from("abcdef\u{4e2d}", 6, 8, true, Viewport::default());
assert_eq!(viewport.left, 2);