use std::cmp::Ordering;
use std::fmt;
use crate::block::Block;
use crate::style::palette::{BLUSH, LINE, MUTED, ROSE, TEXT, TRACK};
use crate::style::{Rgb, bold, mix, on_bg, paint};
use crate::text;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Align {
Left,
Right,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Border {
#[default]
None,
Rounded,
Square,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Overflow {
Truncate,
Wrap,
}
#[derive(Clone, Debug, Default)]
struct Row {
cells: Vec<String>,
highlight: bool,
}
#[derive(Clone, Debug, Default)]
pub struct Table {
headers: Vec<String>,
rows: Vec<Row>,
footer: Option<Vec<String>>,
align: Vec<Option<Align>>,
limits: Vec<Option<(usize, Overflow)>>,
border: Border,
title: Option<String>,
striped: bool,
}
impl Table {
pub fn new<S: Into<String>>(headers: impl IntoIterator<Item = S>) -> Self {
Table {
headers: headers.into_iter().map(Into::into).collect(),
..Self::default()
}
}
pub fn row<S: Into<String>>(mut self, cells: impl IntoIterator<Item = S>) -> Self {
self.rows.push(Row {
cells: cells.into_iter().map(Into::into).collect(),
highlight: false,
});
self
}
pub fn footer<S: Into<String>>(mut self, cells: impl IntoIterator<Item = S>) -> Self {
self.footer = Some(cells.into_iter().map(Into::into).collect());
self
}
pub fn align(mut self, column: usize, align: Align) -> Self {
set(&mut self.align, column, Some(align));
self
}
pub fn max_width(mut self, column: usize, cols: usize) -> Self {
set(&mut self.limits, column, Some((cols.max(1), Overflow::Truncate)));
self
}
pub fn wrap(mut self, column: usize, cols: usize) -> Self {
set(&mut self.limits, column, Some((cols.max(1), Overflow::Wrap)));
self
}
pub fn border(mut self, border: Border) -> Self {
self.border = border;
self
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn striped(mut self) -> Self {
self.striped = true;
self
}
pub fn highlight(mut self, index: usize) -> Self {
if let Some(row) = self.rows.get_mut(index) {
row.highlight = true;
}
self
}
pub fn sort_by(mut self, column: usize) -> Self {
self.sort(column, false);
self
}
pub fn sort_by_desc(mut self, column: usize) -> Self {
self.sort(column, true);
self
}
fn sort(&mut self, column: usize, desc: bool) {
let numeric = self.column_align(column) == Align::Right;
let cell = |r: &Row| text::strip_ansi(r.cells.get(column).map_or("", String::as_str));
self.rows.sort_by(|a, b| {
let (a, b) = (cell(a), cell(b));
let order = if numeric {
match (parse_number(&a), parse_number(&b)) {
(Some(x), Some(y)) => x.total_cmp(&y),
(Some(_), None) => return Ordering::Less,
(None, Some(_)) => return Ordering::Greater,
(None, None) => Ordering::Equal,
}
} else {
a.to_lowercase().cmp(&b.to_lowercase())
};
if desc { order.reverse() } else { order }
});
}
fn column_align(&self, col: usize) -> Align {
if let Some(Some(a)) = self.align.get(col) {
return *a;
}
let numeric = self
.rows
.iter()
.filter_map(|r| r.cells.get(col))
.map(|c| text::strip_ansi(c))
.all(|c| {
let c = c.trim();
c.is_empty() || c.starts_with(|ch: char| ch.is_ascii_digit() || ch == '-' || ch == '$')
});
if numeric && !self.rows.is_empty() {
Align::Right
} else {
Align::Left
}
}
fn fit(&self, col: usize, raw: &str) -> Vec<String> {
match self.limits.get(col).copied().flatten() {
Some((cols, _)) if text::width(raw) <= cols => vec![raw.to_string()],
Some((cols, Overflow::Truncate)) => vec![text::truncate(&text::strip_ansi(raw), cols)],
Some((cols, Overflow::Wrap)) => text::wrap(&text::strip_ansi(raw), cols),
None => vec![raw.to_string()],
}
}
pub fn lines(&self) -> Block {
let layout = self.layout();
match self.border {
Border::Square => self.grid(&layout),
Border::None | Border::Rounded => self.soft(&layout),
}
.into()
}
fn layout(&self) -> Layout {
let cols = self
.rows
.iter()
.map(|r| r.cells.len())
.chain([self.headers.len()])
.chain(self.footer.as_ref().map(Vec::len))
.max()
.unwrap_or(0);
let fitted = |cells: &[String]| -> Vec<Vec<String>> {
(0..cols)
.map(|c| self.fit(c, cells.get(c).map_or("", String::as_str)))
.collect()
};
let header = fitted(&self.headers);
let rows: Vec<(Vec<Vec<String>>, bool)> = self.rows.iter().map(|r| (fitted(&r.cells), r.highlight)).collect();
let footer = self.footer.as_deref().map(fitted);
let widths: Vec<usize> = (0..cols)
.map(|c| {
let all = rows
.iter()
.map(|(r, _)| &r[c])
.chain([&header[c]])
.chain(footer.iter().map(|f| &f[c]));
all.flat_map(|lines| lines.iter().map(|l| text::width(l)))
.max()
.unwrap_or(0)
.max(1)
})
.collect();
let aligns = (0..cols).map(|c| self.column_align(c)).collect();
Layout {
cols,
header,
rows,
footer,
widths,
aligns,
}
}
fn band(&self, n: usize, marked: bool) -> Option<Rgb> {
if marked {
Some(mix(STRIPE, ROSE, 0.2))
} else if self.striped && n % 2 == 1 {
Some(STRIPE)
} else {
None
}
}
fn soft(&self, l: &Layout) -> Vec<String> {
let boxed = self.border == Border::Rounded;
let inner = l.widths.iter().sum::<usize>() + 3 * l.cols.saturating_sub(1);
let frame = |s: &str| paint(mix(LINE, ROSE, 0.35), s);
let dots = |s: &str| paint(mix(BLUSH, TRACK, 0.55), s);
let wrap = |lead: &str, inside: String, band: Option<Rgb>| {
if boxed {
let inside = format!("{lead}{} ", text::pad(&inside, inner));
let inside = band.map_or_else(|| inside.clone(), |b| on_bg(b, &inside));
format!(" {} {inside}{}", frame("│"), frame("│"))
} else if let Some(b) = band {
format!("{lead}{}", on_bg(b, &text::pad(&inside, inner)))
} else {
format!("{lead}{}", inside.trim_end())
}
};
let blank = || wrap(" ", String::new(), None);
let rule = || wrap(" ", dots(&"┈".repeat(inner)), None);
let mut out = Vec::new();
let title = self.title.as_deref().map(|t| bold(&paint(ROSE, &format!("✦ {t}"))));
if boxed {
let across = inner + 5;
out.push(match &title {
Some(t) => {
let t = text::truncate(&text::strip_ansi(t), across.saturating_sub(4));
let rest = across.saturating_sub(text::width(&t) + 3);
format!(
" {}{}{}",
frame("╭─ "),
bold(&paint(ROSE, &t)),
frame(&format!(" {}╮", "─".repeat(rest)))
)
}
None => format!(" {}", frame(&format!("╭{}╮", "─".repeat(across)))),
});
} else if let Some(t) = &title {
out.push(format!(" {t}"));
}
let heading = Look::heading();
for line in self.cells(l, &l.header, &heading, " ") {
out.push(wrap(" ", line, None));
}
out.push(rule());
let divided = l.rows.iter().any(|(r, _)| height(r) > 1);
for (n, (cells, marked)) in l.rows.iter().enumerate() {
if divided && n > 0 {
out.push(blank());
}
let look = Look::body(*marked);
let band = self.band(n, *marked);
for (i, line) in self.cells(l, cells, &look, " ").into_iter().enumerate() {
let lead = if *marked && i == 0 {
format!("{} ", paint(ROSE, "✦"))
} else {
" ".into()
};
out.push(wrap(&lead, line, band));
}
}
if let Some(footer) = &l.footer {
out.push(rule());
for line in self.cells(l, footer, &Look::footer(), " ") {
out.push(wrap(" ", line, None));
}
}
if boxed {
out.push(format!(" {}", frame(&format!("╰{}╯", "─".repeat(inner + 5)))));
}
out
}
fn grid(&self, l: &Layout) -> Vec<String> {
let frame = |s: &str| paint(LINE, s);
let inner = |s: &str| paint(mix(LINE, TRACK, 0.55), s);
let faint = |s: &str| paint(mix(LINE, TRACK, 0.8), s);
let sep = format!(" {} ", inner("│"));
let row = |line: String, band: Option<Rgb>| {
let inside = format!(" {line} ");
let inside = band.map_or_else(|| inside.clone(), |b| on_bg(b, &inside));
format!(" {}{inside}{}", frame("│"), frame("│"))
};
let rule = |left: &str, mid: &str, right: &str, line: &str, paint_it: &dyn Fn(&str) -> String| {
let segs: Vec<String> = l.widths.iter().map(|w| line.repeat(w + 2)).collect();
format!(" {}{}{}", frame(left), paint_it(&segs.join(mid)), frame(right))
};
let mut out = Vec::new();
if let Some(t) = &self.title {
out.push(format!(" {}", bold(&paint(ROSE, &format!("✦ {t}")))));
}
out.push(rule("┌", "┬", "┐", "─", &frame));
for line in self.cells(l, &l.header, &Look::heading(), &sep) {
out.push(row(line, None));
}
out.push(rule("├", "┼", "┤", "─", &frame));
let divided = l.rows.iter().any(|(r, _)| height(r) > 1);
for (n, (cells, marked)) in l.rows.iter().enumerate() {
if divided && n > 0 {
out.push(rule("├", &faint("┼"), "┤", "┄", &faint));
}
let band = self.band(n, *marked);
for line in self.cells(l, cells, &Look::body(*marked), &sep) {
out.push(row(line, band));
}
}
if let Some(footer) = &l.footer {
out.push(rule("├", "┼", "┤", "─", &inner));
for line in self.cells(l, footer, &Look::footer(), &sep) {
out.push(row(line, None));
}
}
out.push(rule("└", "┴", "┘", "─", &frame));
out
}
fn cells(&self, l: &Layout, cells: &[Vec<String>], look: &Look, sep: &str) -> Vec<String> {
(0..height(cells))
.map(|i| {
let parts: Vec<String> = (0..l.cols)
.map(|c| {
let raw = cells[c].get(i).map_or("", String::as_str);
let cell = if raw.contains('\x1b') {
raw.to_string()
} else if look.body && i == 0 && cells[c].iter().all(String::is_empty) {
paint(MUTED, "·")
} else if look.bold {
bold(&paint(look.color, raw))
} else {
paint(look.color, raw)
};
match l.aligns[c] {
Align::Left => text::pad(&cell, l.widths[c]),
Align::Right => text::pad_left(&cell, l.widths[c]),
}
})
.collect();
parts.join(sep)
})
.collect()
}
pub fn print(&self) {
self.lines().print();
}
}
const STRIPE: Rgb = Rgb(38, 32, 36);
struct Layout {
cols: usize,
header: Vec<Vec<String>>,
rows: Vec<(Vec<Vec<String>>, bool)>,
footer: Option<Vec<Vec<String>>>,
widths: Vec<usize>,
aligns: Vec<Align>,
}
struct Look {
color: Rgb,
bold: bool,
body: bool,
}
impl Look {
fn heading() -> Look {
Look {
color: BLUSH,
bold: true,
body: false,
}
}
fn body(marked: bool) -> Look {
Look {
color: if marked { ROSE } else { TEXT },
bold: false,
body: true,
}
}
fn footer() -> Look {
Look {
color: TEXT,
bold: true,
body: false,
}
}
}
fn height(cells: &[Vec<String>]) -> usize {
cells.iter().map(Vec::len).max().unwrap_or(1).max(1)
}
fn set<T: Clone + Default>(v: &mut Vec<T>, i: usize, value: T) {
if i >= v.len() {
v.resize(i + 1, T::default());
}
v[i] = value;
}
fn parse_number(cell: &str) -> Option<f64> {
let s: String = cell
.trim()
.trim_start_matches('$')
.chars()
.filter(|&c| c != ',')
.collect();
let end = s
.char_indices()
.find(|&(i, c)| !(c.is_ascii_digit() || c == '.' || (i == 0 && (c == '-' || c == '+'))))
.map_or(s.len(), |(i, _)| i);
let value: f64 = s[..end].parse().ok()?;
let scale = match s[end..].trim() {
"" | "%" | "B" | "s" | "x" | "×" => 1.0,
"k" | "K" | "KB" | "kB" => 1e3,
"M" | "MB" => 1e6,
"G" | "GB" => 1e9,
"T" | "TB" => 1e12,
"KiB" => 1024.0,
"MiB" => 1024f64.powi(2),
"GiB" => 1024f64.powi(3),
"TiB" => 1024f64.powi(4),
"ns" => 1e-9,
"µs" | "us" => 1e-6,
"ms" => 1e-3,
"m" | "min" => 60.0,
"h" => 3600.0,
"d" => 86400.0,
_ => 1.0,
};
Some(value * scale)
}
impl fmt::Display for Table {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.lines().fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::parse_number;
#[test]
fn numbers_understand_units() {
assert_eq!(parse_number("$1,200"), Some(1200.0));
assert_eq!(parse_number("1.5 MB"), Some(1.5e6));
assert!(parse_number("900 KB") < parse_number("1.2 MB"));
assert!(parse_number("45s") < parse_number("2m"));
assert_eq!(parse_number("84ms"), Some(0.084));
assert_eq!(parse_number("-3"), Some(-3.0));
assert_eq!(parse_number("n/a"), None);
}
}