Lines
100 %
Functions
Branches
use ratatui::style::Color;
use super::*;
fn theme() -> Theme {
Theme::preset("dn").expect("the dn preset parses")
}
const AREA: Rect = Rect::new(2, 3, 4, 3);
fn view(lines: &[&str], cursor: (usize, usize), previous: (usize, usize)) -> (usize, usize) {
let previous = Viewport {
top: previous.0,
left: previous.1,
};
let at = follow_cursor(AREA, lines, cursor, previous);
(at.top, at.left)
fn render(lines: &[&str], cursor: (usize, usize), state: &mut Viewport, focused: bool) -> Buffer {
let theme = theme();
let mut buf = Buffer::empty(Rect::new(0, 0, 4, 3));
TextView::new(&theme, lines, cursor, focused).render(buf.area, &mut buf, state);
buf
/// The lines drawn with the view starting at `viewport` and the cursor inside it.
fn drawn(lines: &[&str], viewport: (usize, usize), focused: bool) -> Buffer {
let mut state = Viewport {
top: viewport.0,
left: viewport.1,
let cursor = (
viewport.0,
lines.get(viewport.0).map_or(0, |line| line.chars().count()),
);
let cursor = (cursor.0, cursor.1.min(viewport.1));
render(lines, cursor, &mut state, focused)
fn row(buf: &Buffer, y: u16) -> String {
(0..buf.area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
const TEN: [&str; 10] = ["a"; 10];
#[test]
fn a_cursor_inside_the_view_leaves_it_where_it_was() {
assert_eq!(view(&TEN, (4, 0), (2, 0)), (2, 0));
assert_eq!(view(&["abcdefgh"], (0, 5), (0, 3)), (0, 3));
fn a_cursor_below_or_above_scrolls_the_least() {
assert_eq!(view(&TEN, (3, 0), (0, 0)), (1, 0));
assert_eq!(view(&TEN, (9, 0), (0, 0)), (7, 0));
assert_eq!(view(&TEN, (1, 0), (4, 0)), (1, 0));
fn a_cursor_right_or_left_of_the_view_scrolls_the_least() {
let lines = ["abcdefgh"];
assert_eq!(view(&lines, (0, 4), (0, 0)), (0, 1));
assert_eq!(view(&lines, (0, 8), (0, 0)), (0, 5));
assert_eq!(view(&lines, (0, 1), (0, 5)), (0, 1));
fn wide_characters_count_two_columns() {
assert_eq!(view(&["日本語"], (0, 3), (0, 0)), (0, 3));
let at = TextView::cursor(AREA, &["日本語"], (0, 2), Viewport::default());
assert_eq!((at.x, at.y), (6, 3));
fn empty_text_and_a_row_past_the_lines_stay_at_the_origin() {
assert_eq!(view(&[], (0, 0), (0, 0)), (0, 0));
assert_eq!(view(&[""], (0, 0), (0, 0)), (0, 0));
fn a_tiny_area_keeps_the_cursor_in_it() {
let area = Rect::new(0, 0, 0, 0);
let at = follow_cursor(area, &["abc", "d"], (1, 1), Viewport::default());
assert_eq!(at, Viewport { top: 1, left: 1 });
fn the_cursor_cell_is_relative_to_the_view() {
let lines = ["ab", "cdefgh", "ij"];
let viewport = Viewport { top: 1, left: 2 };
let at = TextView::cursor(AREA, &lines, (1, 5), viewport);
assert_eq!((at.x, at.y), (5, 3));
let at = TextView::cursor(AREA, &lines, (2, 0), Viewport::default());
assert_eq!((at.x, at.y), (2, 5));
fn the_lines_show_through_the_view() {
let lines = ["abcdef", "gh", "ij", "kl"];
let buf = drawn(&lines, (1, 0), false);
assert_eq!([row(&buf, 0), row(&buf, 1)], ["gh ", "ij "]);
let buf = drawn(&lines, (0, 2), false);
assert_eq!(row(&buf, 0), "cdef");
assert_eq!(row(&buf, 1), " ");
fn a_wide_character_cut_at_the_left_leaves_no_half_of_it() {
let buf = drawn(&["日本x"], (0, 1), false);
assert!(!row(&buf, 0).contains('日'), "{:?}", row(&buf, 0));
assert!(row(&buf, 0).contains('本'), "{:?}", row(&buf, 0));
fn colours(buf: &Buffer, x: u16, y: u16) -> (Option<Color>, Option<Color>) {
let style = buf[(x, y)].style();
(style.fg, style.bg)
fn text_and_empty_cells_use_the_input_roles() {
let want = |role| (theme.style(role).fg, theme.style(role).bg);
let buf = drawn(&["a"], (0, 0), false);
assert_eq!(colours(&buf, 0, 0), want(Role::Input));
assert_eq!(colours(&buf, 3, 2), want(Role::Input));
let buf = drawn(&["a"], (0, 0), true);
assert_eq!(colours(&buf, 0, 0), want(Role::InputFocused));
assert_eq!(colours(&buf, 3, 2), want(Role::InputFocused));
fn rendering_follows_the_cursor_and_keeps_the_state() {
let lines = ["abcdefgh", "x", "y", "z"];
let mut state = Viewport::default();
let buf = render(&lines, (0, 7), &mut state, true);
assert_eq!(state, Viewport { top: 0, left: 4 });
assert_eq!(row(&buf, 0), "efgh");
let buf = render(&lines, (3, 0), &mut state, true);
assert_eq!(state, Viewport { top: 1, left: 0 });
assert_eq!(row(&buf, 0), "x ");
fn a_wide_character_under_the_cursor_is_kept_whole_in_view() {
assert_eq!(view(&["abc日"], (0, 3), (0, 0)), (0, 1));
assert_eq!(view(&["abc日"], (0, 3), (0, 3)), (0, 3));
let buf = render(&["abc日"], (0, 3), &mut state, true);
assert_eq!(row(&buf, 0), "bc日 ");
fn a_character_wider_than_the_box_starts_at_its_edge() {
let tiny = Rect::new(0, 0, 1, 1);
assert_eq!(
follow_cursor(tiny, &["a日"], (0, 1), Viewport::default()).left,
1
fn the_top_is_pulled_back_when_the_text_got_shorter() {
assert_eq!(view(&["a", "b", "c", "d"], (3, 0), (7, 0)), (1, 0));
assert_eq!(view(&["a"], (0, 0), (5, 0)), (0, 0));
fn control_characters_are_drawn_as_blanks() {
let buf = render(&["a\tb\u{7f}c"], (0, 0), &mut state, false);
assert_eq!(row(&buf, 0), "a b ");
let at = TextView::cursor(AREA, &["a\tb"], (0, 3), Viewport::default());
assert_eq!(at.x, AREA.x + 3);
fn at(lines: &[&str], viewport: (usize, usize), x: u16, y: u16) -> (usize, usize) {
let viewport = Viewport {
TextView::position_at(AREA, lines, viewport, Position::new(AREA.x + x, AREA.y + y))
fn a_click_lands_on_its_line_and_character() {
let lines = ["abcd", "efgh", "ij"];
assert_eq!(at(&lines, (0, 0), 2, 1), (1, 2));
assert_eq!(at(&lines, (0, 0), 0, 0), (0, 0));
fn a_click_past_a_line_end_lands_at_its_end() {
let lines = ["abcd", "ij"];
assert_eq!(at(&lines, (0, 0), 3, 1), (1, 2));
fn a_click_below_the_text_lands_on_the_last_line() {
assert_eq!(at(&lines, (0, 0), 1, 2), (1, 1));
assert_eq!(at(&[], (0, 0), 1, 2), (0, 0));
fn a_click_in_a_scrolled_view_counts_from_the_viewport() {
assert_eq!(at(&TEN, (4, 0), 0, 2), (6, 0));
assert_eq!(at(&["abcdefgh"], (0, 3), 1, 0), (0, 4));
fn a_wide_character_takes_a_click_on_either_of_its_cells() {
let lines = ["a\u{4e2d}b"];
assert_eq!(at(&lines, (0, 0), 2, 0), (0, 1));
assert_eq!(at(&lines, (0, 1), 1, 0), (0, 1));
fn position_at_is_the_inverse_of_cursor() {
let lines = [
"abcdefgh",
"\u{4e2d}\u{4e2d}\u{4e2d}x",
"",
"ij",
"klmn",
"o",
];
for row in 0..lines.len() {
for col in 0..=lines[row].chars().count() {
let mut viewport = Viewport::default();
let mut buf = Buffer::empty(AREA);
TextView::new(&theme, &lines, (row, col), true).render(AREA, &mut buf, &mut viewport);
let point = TextView::cursor(AREA, &lines, (row, col), viewport);
let found = TextView::position_at(AREA, &lines, viewport, point);
assert_eq!(found, (row, col), "{viewport:?}");