Lines
100 %
Functions
Branches
//! The scrollbar parts a frame records are the cells the bar is drawn in.
use crate::app::App;
use crate::hits::{Hits, Surface, Target};
use crate::modal::{Draft, Modal};
use crate::tabs::fetch::Fetch;
use crate::test_support::draw_frame;
use crate::view::Tab;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use tvui::BarPart;
fn symbol(part: BarPart) -> &'static str {
match part {
BarPart::Up => "\u{25B2}",
BarPart::Down => "\u{25BC}",
BarPart::Thumb => "\u{25A0}",
BarPart::PageUp | BarPart::PageDown => "\u{2592}",
}
fn parts(hits: &Hits, surface: Surface) -> Vec<(Rect, BarPart)> {
hits.entries()
.iter()
.filter_map(|(rect, target)| match target {
Target::Scroll(found, part) if *found == surface => Some((*rect, *part)),
_ => None,
})
.collect()
/// Each recorded part covers cells drawn with its symbol; together they are one column.
fn assert_on_screen(buffer: &Buffer, parts: &[(Rect, BarPart)]) {
assert!(!parts.is_empty());
let column = parts[0].0.x;
for (rect, part) in parts {
assert_eq!(rect.x, column);
for y in rect.top()..rect.bottom() {
assert_eq!(buffer[(rect.x, y)].symbol(), symbol(*part), "{part:?} {y}");
let ends = |first: bool| {
let end = if first { parts.first() } else { parts.last() };
end.map(|(_, part)| *part)
};
assert_eq!(ends(true), Some(BarPart::Up));
assert_eq!(ends(false), Some(BarPart::Down));
fn accounts(count: usize) -> App {
let mut app = crate::test_support::app();
app.switch_tab(Tab::Accounts);
let row = |i: usize| format!(r#"(:account :id "u{i}" :name "a{i}" :parent "")"#);
let rows: Vec<String> = (0..count).map(row).collect();
app.accounts_mut()
.on_reply(&format!("(:id 1 :value ({}))", rows.join(" ")));
app
#[test]
fn a_list_with_hidden_rows_records_its_bar_and_the_whole_panel_as_a_body() {
let app = accounts(60);
let (buffer, hits) = draw_frame(&app, 80, 24);
assert_on_screen(&buffer, &parts(&hits, Surface::List));
assert!(
.any(|(_, target)| *target == Target::Body(Surface::List))
);
fn a_list_that_fits_records_no_bar() {
let (_, hits) = draw_frame(&accounts(3), 80, 24);
assert!(parts(&hits, Surface::List).is_empty());
fn the_thumb_part_follows_the_scrolled_list() {
let mut app = accounts(60);
let (_, hits) = draw_frame(&app, 80, 24);
let thumb = |hits: &Hits| {
let found = parts(hits, Surface::List);
found.into_iter().find(|(_, part)| *part == BarPart::Thumb)
let before = thumb(&hits).map(|(rect, _)| rect.y);
app.accounts_mut().selected = 59;
assert!(thumb(&hits).map(|(rect, _)| rect.y) > before);
fn a_report_table_records_its_bar_and_body() {
app.switch_tab(Tab::Reports);
app.reports_mut().state = Fetch::Loaded((0..60).map(|n| n.to_string()).collect());
assert_on_screen(&buffer, &parts(&hits, Surface::Pane));
.any(|(_, target)| *target == Target::Body(Surface::Pane))
fn detail(lines: usize) -> App {
let lines: Vec<String> = (0..lines).map(|n| n.to_string()).collect();
app.overlays.push(Modal::Draft(Draft::new(lines)));
fn a_scrolled_modal_records_its_body_above_the_block_and_its_bar() {
let app = detail(60);
let (buffer, hits) = draw_frame(&app, 80, 14);
assert_on_screen(&buffer, &parts(&hits, Surface::Modal));
let entries = hits.entries();
let body = entries
.find(|(_, target)| *target == Target::Body(Surface::Modal));
let (rect, _) = body.expect("the body is recorded");
let inside = rect.as_position();
assert_eq!(
hits.hit_at(inside).map(|(_, target, _)| target),
Some(Target::Body(Surface::Modal))
hits.hit_at((0, 0).into()).map(|(_, target, _)| target),
Some(Target::Block)
fn a_modal_that_fits_records_a_body_and_no_bar() {
let (_, hits) = draw_frame(&detail(2), 80, 24);
assert!(parts(&hits, Surface::Modal).is_empty());
.any(|(_, target)| *target == Target::Body(Surface::Modal))