mod scroll;
use super::help::{self, KeyMap as HelpKeyMap};
use super::key::Binding;
use super::viewport;
use crate::style::{
Color, Position, Style, height, join_horizontal, join_vertical, truncate_with, width,
};
use crate::{Cmd, Msg};
pub type Row = Vec<String>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Column {
pub title: String,
pub width: usize,
}
impl Column {
#[must_use]
pub fn new(title: &str, width: usize) -> Self {
Self {
title: title.to_string(),
width,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyMap {
pub line_up: Binding,
pub line_down: Binding,
pub page_up: Binding,
pub page_down: Binding,
pub half_page_up: Binding,
pub half_page_down: Binding,
pub goto_top: Binding,
pub goto_bottom: Binding,
}
impl Default for KeyMap {
fn default() -> Self {
let b = |keys: &[&str], key: &str, desc: &str| Binding::new(keys).with_help(key, desc);
Self {
line_up: b(&["up", "k"], "↑/k", "up"),
line_down: b(&["down", "j"], "↓/j", "down"),
page_up: b(&["b", "pgup"], "b/pgup", "page up"),
page_down: b(&["f", "pgdown", "space"], "f/pgdn", "page down"),
half_page_up: b(&["u", "ctrl+u"], "u", "½ page up"),
half_page_down: b(&["d", "ctrl+d"], "d", "½ page down"),
goto_top: b(&["home", "g"], "g/home", "go to start"),
goto_bottom: b(&["end", "G"], "G/end", "go to end"),
}
}
}
impl HelpKeyMap for KeyMap {
fn short_help(&self) -> Vec<Binding> {
vec![self.line_up.clone(), self.line_down.clone()]
}
fn full_help(&self) -> Vec<Vec<Binding>> {
vec![
vec![
self.line_up.clone(),
self.line_down.clone(),
self.goto_top.clone(),
self.goto_bottom.clone(),
],
vec![
self.page_up.clone(),
self.page_down.clone(),
self.half_page_up.clone(),
self.half_page_down.clone(),
],
]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Styles {
pub header: Style,
pub cell: Style,
pub selected: Style,
}
impl Default for Styles {
fn default() -> Self {
Self {
selected: Style::new().bold().fg(Color::Palette(212)),
header: Style::new().bold().padding(0, 1),
cell: Style::new().padding(0, 1),
}
}
}
#[derive(Debug, Clone)]
pub struct Model {
pub key_map: KeyMap,
pub help: help::Model,
cols: Vec<Column>,
rows: Vec<Row>,
cursor: usize,
focus: bool,
styles: Styles,
viewport: viewport::Model,
start: usize,
end: usize,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
let mut m = Self {
key_map: KeyMap::default(),
help: help::Model::new(),
cols: Vec::new(),
rows: Vec::new(),
cursor: 0,
focus: false,
styles: Styles::default(),
viewport: viewport::Model::new(0, 20),
start: 0,
end: 0,
};
m.update_viewport();
m
}
#[must_use]
pub fn with_columns(mut self, cols: Vec<Column>) -> Self {
self.set_columns(cols);
self
}
#[must_use]
pub fn with_rows(mut self, rows: Vec<Row>) -> Self {
self.set_rows(rows);
self
}
#[must_use]
pub fn with_height(mut self, rows: usize) -> Self {
self.set_height(rows);
self
}
#[must_use]
pub fn with_width(mut self, columns: usize) -> Self {
self.set_width(columns);
self
}
#[must_use]
pub fn with_focused(mut self, focused: bool) -> Self {
self.focus = focused;
self.update_viewport();
self
}
#[must_use]
pub fn with_styles(mut self, styles: Styles) -> Self {
self.set_styles(styles);
self
}
#[must_use]
pub fn with_key_map(mut self, key_map: KeyMap) -> Self {
self.key_map = key_map;
self
}
pub fn set_styles(&mut self, styles: Styles) {
self.styles = styles;
self.update_viewport();
}
pub fn update<M>(&mut self, msg: &Msg<M>) -> Cmd<M> {
if let (true, Msg::KeyPress(key)) = (self.focus, msg) {
let k = &self.key_map;
let page = self.viewport.height();
if k.line_up.matches(key) {
self.move_up(1);
} else if k.line_down.matches(key) {
self.move_down(1);
} else if k.page_up.matches(key) {
self.move_up(page);
} else if k.page_down.matches(key) {
self.move_down(page);
} else if k.half_page_up.matches(key) {
self.move_up(page / 2);
} else if k.half_page_down.matches(key) {
self.move_down(page / 2);
} else if k.goto_top.matches(key) {
self.goto_top();
} else if k.goto_bottom.matches(key) {
self.goto_bottom();
}
}
Cmd::none()
}
#[must_use]
pub const fn focused(&self) -> bool {
self.focus
}
pub fn focus(&mut self) {
self.focus = true;
self.update_viewport();
}
pub fn blur(&mut self) {
self.focus = false;
self.update_viewport();
}
#[must_use]
pub fn view(&self) -> String {
self.headers_view() + "\n" + &self.viewport.view()
}
#[must_use]
pub fn help_view(&self) -> String {
self.help.view(&self.key_map)
}
pub fn update_viewport(&mut self) {
let h = self.viewport.height();
self.start = self.cursor.saturating_sub(h);
self.end = (self.cursor + h).min(self.rows.len());
let rendered: Vec<String> = (self.start..self.end).map(|r| self.render_row(r)).collect();
let blocks: Vec<&str> = rendered.iter().map(String::as_str).collect();
self.viewport
.set_content(&join_vertical(Position::LEFT, &blocks));
}
#[must_use]
pub fn selected_row(&self) -> Option<&Row> {
self.rows.get(self.cursor)
}
#[must_use]
pub fn rows(&self) -> &[Row] {
&self.rows
}
#[must_use]
pub fn columns(&self) -> &[Column] {
&self.cols
}
pub fn set_rows(&mut self, rows: Vec<Row>) {
self.rows = rows;
self.cursor = self.cursor.min(self.rows.len().saturating_sub(1));
self.update_viewport();
}
pub fn set_columns(&mut self, cols: Vec<Column>) {
self.cols = cols;
self.update_viewport();
}
pub fn set_width(&mut self, columns: usize) {
self.viewport.set_width(columns);
self.update_viewport();
}
pub fn set_height(&mut self, rows: usize) {
self.viewport
.set_height(rows.saturating_sub(height(&self.headers_view())));
self.update_viewport();
}
#[must_use]
pub const fn height(&self) -> usize {
self.viewport.height()
}
#[must_use]
pub const fn width(&self) -> usize {
self.viewport.width()
}
#[must_use]
pub const fn cursor(&self) -> usize {
self.cursor
}
pub fn from_values(&mut self, value: &str, separator: &str) {
let rows = value
.split('\n')
.map(|line| line.split(separator).map(String::from).collect())
.collect();
self.set_rows(rows);
}
fn headers_view(&self) -> String {
let cells: Vec<String> = self
.cols
.iter()
.filter(|c| c.width > 0)
.map(|c| self.styles.header.render(&fit(&c.title, c.width)))
.collect();
let blocks: Vec<&str> = cells.iter().map(String::as_str).collect();
join_horizontal(Position::TOP, &blocks)
}
fn render_row(&self, r: usize) -> String {
let selected = r == self.cursor;
let cells: Vec<String> = self.rows[r]
.iter()
.zip(&self.cols)
.filter(|(_, c)| c.width > 0)
.map(|(value, c)| self.render_cell(&fit(value, c.width), selected))
.collect();
cells.concat()
}
fn render_cell(&self, text: &str, selected: bool) -> String {
let cell = self.styles.cell;
if !selected {
return cell.render(text);
}
let (left, right) = (cell.get_padding_left(), cell.get_padding_right());
let padded = format!("{}{text}{}", " ".repeat(left), " ".repeat(right));
self.styles
.selected
.plain()
.inherit(cell.plain())
.paint(&padded)
}
}
fn fit(text: &str, columns: usize) -> String {
let text = truncate_with(text, columns, "…");
let gap = columns.saturating_sub(width(&text));
text + &" ".repeat(gap)
}
#[cfg(test)]
mod tests;