use crate::buffer::Buffer;
use crate::layout::{Constraint, Direction, Layout, Rect};
use crate::style::Style;
use crate::text::Text;
use crate::widgets::{Block, StatefulWidget, Widget};
#[derive(Debug, Clone)]
pub struct Cell<'a> {
content: Text<'a>,
style: Style,
}
impl<'a> Cell<'a> {
pub fn new<T>(content: T) -> Cell<'a>
where
T: Into<Text<'a>>,
{
Cell {
content: content.into(),
style: Style::default(),
}
}
pub fn style(mut self, style: Style) -> Cell<'a> {
self.style = style;
self
}
}
#[derive(Debug, Clone, Default)]
pub struct Row<'a> {
cells: Vec<Cell<'a>>,
style: Style,
height: u16,
}
impl<'a> Row<'a> {
pub fn new<T>(cells: T) -> Row<'a>
where
T: IntoIterator,
T::Item: Into<Cell<'a>>,
{
Row {
cells: cells.into_iter().map(Into::into).collect(),
style: Style::default(),
height: 1,
}
}
pub fn style(mut self, style: Style) -> Row<'a> {
self.style = style;
self
}
pub fn height(mut self, height: u16) -> Row<'a> {
self.height = height;
self
}
}
impl<'a> From<&'a str> for Cell<'a> {
fn from(s: &'a str) -> Cell<'a> {
Cell::new(s)
}
}
impl<'a> From<String> for Cell<'a> {
fn from(s: String) -> Cell<'a> {
Cell::new(s)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct TableState {
offset: usize,
selected: Option<usize>,
}
impl TableState {
pub fn selected(&self) -> Option<usize> {
self.selected
}
pub fn select(&mut self, index: Option<usize>) {
self.selected = index;
}
}
#[derive(Debug, Default, Clone)]
pub struct Table<'a> {
header: Option<Row<'a>>,
rows: Vec<Row<'a>>,
widths: Vec<Constraint>,
block: Option<Block<'a>>,
style: Style,
highlight_style: Style,
column_spacing: u16,
}
impl<'a> Table<'a> {
pub fn new<T>(rows: T, widths: Vec<Constraint>) -> Table<'a>
where
T: IntoIterator<Item = Row<'a>>,
{
Table {
header: None,
rows: rows.into_iter().collect(),
widths,
block: None,
style: Style::default(),
highlight_style: Style::default(),
column_spacing: 1,
}
}
pub fn header(mut self, header: Row<'a>) -> Table<'a> {
self.header = Some(header);
self
}
pub fn block(mut self, block: Block<'a>) -> Table<'a> {
self.block = Some(block);
self
}
pub fn style(mut self, style: Style) -> Table<'a> {
self.style = style;
self
}
pub fn highlight_style(mut self, style: Style) -> Table<'a> {
self.highlight_style = style;
self
}
pub fn column_spacing(mut self, spacing: u16) -> Table<'a> {
self.column_spacing = spacing;
self
}
}
fn render_row(row: &Row<'_>, cols: &[Rect], y: u16, buf: &mut Buffer, extra_style: Style) {
for (cell, &area) in row.cells.iter().zip(cols) {
let area = Rect::new(area.x, y, area.width, row.height.max(1));
buf.set_style(area, row.style.patch(extra_style));
for (i, line) in cell.content.lines.iter().enumerate() {
if i as u16 >= area.height {
break;
}
let mut cx = area.x;
for span in &line.spans {
let merged = span.style.patch(cell.style).patch(extra_style);
cx = buf.set_stringn(
cx,
area.y + i as u16,
&span.content,
area.width.saturating_sub(cx - area.x) as usize,
merged,
);
}
}
}
}
impl<'a> StatefulWidget for Table<'a> {
type State = TableState;
fn render(mut self, area: Rect, buf: &mut Buffer, state: &mut TableState) {
let table_area = if let Some(block) = self.block.take() {
let inner = block.inner(area);
block.render(area, buf);
inner
} else {
area
};
if table_area.area() == 0 {
return;
}
buf.set_style(table_area, self.style);
let columns = Layout::default()
.direction(Direction::Horizontal)
.spacing(self.column_spacing)
.constraints(self.widths.clone())
.split(Rect::new(table_area.x, 0, table_area.width, 1));
let mut y = table_area.y;
if let Some(header) = &self.header {
let cols: Vec<Rect> = columns
.iter()
.map(|c| Rect::new(c.x, y, c.width, 1))
.collect();
render_row(header, &cols, y, buf, Style::default());
y += header.height.max(1);
}
for (i, row) in self.rows.iter().enumerate().skip(state.offset) {
if y >= table_area.bottom() {
break;
}
let cols: Vec<Rect> = columns
.iter()
.map(|c| Rect::new(c.x, y, c.width, row.height.max(1)))
.collect();
let extra = if state.selected == Some(i) {
self.highlight_style
} else {
Style::default()
};
render_row(row, &cols, y, buf, extra);
y += row.height.max(1);
}
}
}
impl<'a> Widget for Table<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let mut state = TableState::default();
StatefulWidget::render(self, area, buf, &mut state);
}
}