use ratatui::layout::Constraint;
use crate::component::cell::{Cell, RowStatus};
pub trait TableRow: Clone {
fn cells(&self) -> Vec<Cell>;
fn status(&self) -> RowStatus {
RowStatus::None
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "serialization",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct Column {
header: String,
#[cfg_attr(feature = "serialization", serde(skip))]
width: Constraint,
sortable: bool,
editable: bool,
visible: bool,
default_sort: SortDirection,
}
impl Column {
pub fn new(header: impl Into<String>, width: Constraint) -> Self {
Self {
header: header.into(),
width,
sortable: false,
editable: true,
visible: true,
default_sort: SortDirection::Ascending,
}
}
pub fn sortable(mut self) -> Self {
self.sortable = true;
self
}
pub fn header(&self) -> &str {
&self.header
}
pub fn width(&self) -> Constraint {
self.width
}
pub fn fixed(header: impl Into<String>, width: u16) -> Self {
Self::new(header, Constraint::Length(width))
}
pub fn min(header: impl Into<String>, width: u16) -> Self {
Self::new(header, Constraint::Min(width))
}
pub fn percent(header: impl Into<String>, percent: u16) -> Self {
Self::new(header, Constraint::Percentage(percent))
}
pub fn is_sortable(&self) -> bool {
self.sortable
}
pub fn with_editable(mut self, editable: bool) -> Self {
self.editable = editable;
self
}
pub fn is_editable(&self) -> bool {
self.editable
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn is_visible(&self) -> bool {
self.visible
}
pub fn set_visible(&mut self, visible: bool) {
self.visible = visible;
}
pub fn set_editable(&mut self, editable: bool) {
self.editable = editable;
}
pub fn with_default_sort(mut self, dir: SortDirection) -> Self {
self.default_sort = dir;
self
}
pub fn default_sort(&self) -> SortDirection {
self.default_sort
}
pub fn set_width(&mut self, width: Constraint) {
self.width = width;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "serialization",
derive(serde::Serialize, serde::Deserialize)
)]
pub enum SortDirection {
#[default]
Ascending,
Descending,
}
impl SortDirection {
pub fn toggle(self) -> Self {
match self {
SortDirection::Ascending => SortDirection::Descending,
SortDirection::Descending => SortDirection::Ascending,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "serialization",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct InitialSort {
pub column: usize,
pub direction: SortDirection,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TableMessage {
Up,
Down,
First,
Last,
PageUp(usize),
PageDown(usize),
Select,
#[allow(dead_code)] SortAsc(usize),
#[allow(dead_code)]
SortDesc(usize),
#[allow(dead_code)]
SortToggle(usize),
#[allow(dead_code)]
SortClear,
#[allow(dead_code)]
RemoveSort(usize),
#[allow(dead_code)]
AddSortAsc(usize),
#[allow(dead_code)]
AddSortDesc(usize),
#[allow(dead_code)]
AddSortToggle(usize),
IncreaseColumnWidth(usize),
DecreaseColumnWidth(usize),
SetFilter(String),
ClearFilter,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TableOutput<T: Clone> {
Selected(T),
SelectionChanged(usize),
Sorted {
column: usize,
direction: SortDirection,
},
SortCleared,
FilterChanged(String),
ColumnResized {
column: usize,
width: u16,
},
}