feat(tui): scroll long result names and page through results
Closes #55. While [2] Results is focused the Info pane describes the highlighted result, so a truncated path can be read in full. h/l (or the arrow keys) scroll the filename column by display cells, 0 and $ jump to either end of the highlighted name, and Home/End, g/G, PgUp/PgDn and Ctrl-f/b/d/u move through the list; the same keys work while a filter is being typed. Ctrl chords are no longer read as their bare letter: outside [2] they do nothing, where before Ctrl-d deleted a search and Ctrl-c opened rooms. Space after a filter that matches nothing no longer indexes an empty list.
This commit is contained in:
@@ -6,14 +6,25 @@ use crate::ui::{
|
||||
};
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::Rect,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
text::{Line, Span},
|
||||
widgets::{Cell, HighlightSpacing, Paragraph, Row, Table, TableState},
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use unicode_truncate::UnicodeTruncateStr;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
const WIDTHS: [Constraint; 7] = [
|
||||
Constraint::Length(3),
|
||||
Constraint::Fill(3),
|
||||
Constraint::Length(12),
|
||||
Constraint::Length(15),
|
||||
Constraint::Length(10),
|
||||
Constraint::Length(12),
|
||||
Constraint::Length(6),
|
||||
];
|
||||
|
||||
pub struct ResultsPaneParams<'a> {
|
||||
pub items: &'a [FileDisplayData],
|
||||
pub table_state: &'a mut TableState,
|
||||
@@ -25,6 +36,38 @@ pub struct ResultsPaneParams<'a> {
|
||||
pub is_filtering: bool,
|
||||
pub focused: bool,
|
||||
pub active_search_query: Option<&'a str>,
|
||||
pub name_offset: usize,
|
||||
}
|
||||
|
||||
fn name_column_width(area: Rect) -> usize {
|
||||
let inner = pane_block(false).inner(area);
|
||||
let symbol = Line::from(HIGHLIGHT_SYMBOL).width() as u16;
|
||||
let [_, columns] =
|
||||
Layout::horizontal([Constraint::Length(symbol), Constraint::Fill(0)])
|
||||
.areas(inner);
|
||||
usize::from(Layout::horizontal(WIDTHS).spacing(1).split(columns)[1].width)
|
||||
}
|
||||
|
||||
const fn overflow(cells: usize, width: usize) -> usize {
|
||||
if cells <= width { 0 } else { cells + 1 - width }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn name_end_offset(name: &str, area: Rect) -> usize {
|
||||
overflow(Span::raw(name).width(), name_column_width(area))
|
||||
}
|
||||
|
||||
fn scroll_name(name: &str, offset: usize, width: usize) -> Line<'_> {
|
||||
if offset == 0 {
|
||||
return Line::from(name);
|
||||
}
|
||||
let cells = Span::raw(name).width();
|
||||
let skip = offset.min(overflow(cells, width));
|
||||
if skip == 0 {
|
||||
return Line::from(name);
|
||||
}
|
||||
let (tail, _) = name.unicode_truncate_start(cells - skip);
|
||||
Line::from(vec![Span::raw("…"), Span::raw(tail)])
|
||||
}
|
||||
|
||||
/// Whether the rendered row `display_idx` is selected. `selected_indices` holds
|
||||
@@ -59,6 +102,7 @@ pub fn render_results_pane(
|
||||
is_filtering,
|
||||
focused,
|
||||
active_search_query,
|
||||
name_offset,
|
||||
} = params;
|
||||
if items.is_empty() {
|
||||
let title = match active_search_query {
|
||||
@@ -105,6 +149,7 @@ pub fn render_results_pane(
|
||||
])
|
||||
.height(1);
|
||||
|
||||
let name_width = name_column_width(area);
|
||||
let rows: Vec<Row> = items
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -137,7 +182,12 @@ pub fn render_results_pane(
|
||||
|
||||
Row::new(vec![
|
||||
Cell::from(checkbox).style(checkbox_style),
|
||||
Cell::from(file.filename.clone()).style(body_style()),
|
||||
Cell::from(scroll_name(
|
||||
&file.filename,
|
||||
name_offset,
|
||||
name_width,
|
||||
))
|
||||
.style(body_style()),
|
||||
Cell::from(format_bytes(file.size)).style(warning_style()),
|
||||
Cell::from(file.username.clone()).style(info_style()),
|
||||
Cell::from(bitrate_str).style(dimmed_style()),
|
||||
@@ -147,16 +197,6 @@ pub fn render_results_pane(
|
||||
})
|
||||
.collect();
|
||||
|
||||
let widths = [
|
||||
ratatui::layout::Constraint::Length(3),
|
||||
ratatui::layout::Constraint::Fill(3),
|
||||
ratatui::layout::Constraint::Length(12),
|
||||
ratatui::layout::Constraint::Length(15),
|
||||
ratatui::layout::Constraint::Length(10),
|
||||
ratatui::layout::Constraint::Length(12),
|
||||
ratatui::layout::Constraint::Length(6),
|
||||
];
|
||||
|
||||
let title = if is_filtering {
|
||||
format!("Results · filter: '{filter_query}'")
|
||||
} else if let Some(query) = active_search_query {
|
||||
@@ -165,7 +205,7 @@ pub fn render_results_pane(
|
||||
"Results".to_string()
|
||||
};
|
||||
|
||||
let table = Table::new(rows, widths)
|
||||
let table = Table::new(rows, WIDTHS)
|
||||
.header(header)
|
||||
.row_highlight_style(row_highlight_style())
|
||||
.highlight_symbol(HIGHLIGHT_SYMBOL)
|
||||
@@ -177,9 +217,78 @@ pub fn render_results_pane(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::row_is_selected;
|
||||
use super::{ResultsPaneParams, render_results_pane, row_is_selected};
|
||||
use crate::models::FileDisplayData;
|
||||
use ratatui::{Terminal, backend::TestBackend, widgets::TableState};
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn render_rows(items: &[FileDisplayData], name_offset: usize) -> String {
|
||||
let mut state = TableState::default();
|
||||
state.select(Some(0));
|
||||
let mut terminal =
|
||||
Terminal::new(TestBackend::new(80, 6)).expect("backend");
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
render_results_pane(
|
||||
frame,
|
||||
frame.area(),
|
||||
ResultsPaneParams {
|
||||
items,
|
||||
table_state: &mut state,
|
||||
selected_indices: &HashSet::new(),
|
||||
original_indices: None,
|
||||
filter_query: "",
|
||||
is_filtering: false,
|
||||
focused: true,
|
||||
active_search_query: None,
|
||||
name_offset,
|
||||
},
|
||||
);
|
||||
})
|
||||
.expect("draw");
|
||||
terminal.backend().to_string()
|
||||
}
|
||||
|
||||
fn file(filename: &str) -> FileDisplayData {
|
||||
FileDisplayData {
|
||||
filename: filename.to_string(),
|
||||
username: "bob".to_string(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scrolled_name_shows_its_tail_up_to_the_column_edge() {
|
||||
let items = [
|
||||
file("abcdefghijklmnopqrstuvwxyz0123456789ABCD"),
|
||||
file("short.mp3"),
|
||||
];
|
||||
let screen = render_rows(&items, usize::MAX);
|
||||
assert!(screen.contains("…456789ABCD"), "{screen}");
|
||||
assert!(!screen.contains("3456789ABCD"), "{screen}");
|
||||
assert!(screen.contains(" short.mp3 "), "{screen}");
|
||||
|
||||
let screen = render_rows(&items, 8);
|
||||
assert!(screen.contains("…ijklmnopqr"), "{screen}");
|
||||
assert!(!screen.contains("hijklmnopqr"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wide_glyph_name_scrolls_by_cells_not_chars() {
|
||||
let items = [file("零一二三四五六七八九十百千万亿東西南北中")];
|
||||
let screen = render_rows(&items, usize::MAX);
|
||||
assert!(screen.contains("…東西南北中"), "{screen}");
|
||||
assert!(!screen.contains("亿東"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unscrolled_name_starts_at_its_beginning() {
|
||||
let items = [file("abcdefghijklmnopqrstuvwxyz0123456789ABCD")];
|
||||
let screen = render_rows(&items, 0);
|
||||
assert!(screen.contains("abcdefghijk"), "{screen}");
|
||||
assert!(!screen.contains("…"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_mapping_used_when_no_filter() {
|
||||
let selected: HashSet<usize> = std::iter::once(1).collect();
|
||||
|
||||
Reference in New Issue
Block a user