use crate::node::{ColorTheme, Node, RenderOptions, TableAlign, TableAlignKind, TableCell, render_cell_values};
use unicode_width::UnicodeWidthStr;
pub(super) struct TableLayout {
widths: Vec<usize>,
align: Vec<TableAlignKind>,
header_columns: usize,
cell_widths: rustc_hash::FxHashMap<(usize, usize), usize>,
pub(super) end: usize,
}
impl TableLayout {
pub(super) fn compute(nodes: &[Node], options: &RenderOptions, start: usize) -> Self {
let mut widths: Vec<usize> = Vec::new();
let mut align: Vec<TableAlignKind> = Vec::new();
let mut cell_widths = rustc_hash::FxHashMap::default();
let mut header_columns = 0;
let mut i = start;
while let Some(node) = nodes.get(i) {
match node {
Node::TableCell(TableCell {
row, column, values, ..
}) => {
if *row == 0 && *column == 0 && !align.is_empty() {
break;
}
if *row == 0 {
header_columns = header_columns.max(*column + 1);
}
let width = cell_display_width(values, options);
cell_widths.insert((*row, *column), width);
if *column >= widths.len() {
widths.resize(*column + 1, 0);
}
widths[*column] = widths[*column].max(width);
i += 1;
}
Node::TableAlign(TableAlign { align: a, .. }) => {
align = a.clone();
i += 1;
}
_ => break,
}
}
if widths.len() < align.len() {
widths.resize(align.len(), 0);
}
for (idx, width) in widths.iter_mut().enumerate() {
let a = align.get(idx).unwrap_or(&TableAlignKind::None);
*width = (*width).max(column_min_width(a));
}
Self {
widths,
align,
header_columns,
cell_widths,
end: i,
}
}
pub(super) fn align_for(&self, column: usize) -> TableAlignKind {
self.align.get(column).cloned().unwrap_or(TableAlignKind::None)
}
pub(super) fn width_for(&self, column: usize) -> usize {
self.widths
.get(column)
.copied()
.unwrap_or(column_min_width(&self.align_for(column)))
}
pub(super) fn plain_width_at(&self, row: usize, column: usize) -> usize {
self.cell_widths.get(&(row, column)).copied().unwrap_or(0)
}
pub(super) fn render_separator(&self) -> String {
let columns = if self.header_columns == 0 {
self.widths.len()
} else {
self.header_columns
};
let segments = self
.widths
.iter()
.take(columns)
.enumerate()
.map(|(idx, &width)| {
let align = self.align_for(idx);
match align {
TableAlignKind::None => "-".repeat(width),
TableAlignKind::Left => format!(":{}", "-".repeat(width - 1)),
TableAlignKind::Right => format!("{}:", "-".repeat(width - 1)),
TableAlignKind::Center => format!(":{}:", "-".repeat(width - 2)),
}
})
.collect::<Vec<_>>()
.join(" | ");
format!("| {} |", segments)
}
}
fn column_min_width(align: &TableAlignKind) -> usize {
match align {
TableAlignKind::None => 1,
TableAlignKind::Left | TableAlignKind::Right => 2,
TableAlignKind::Center => 3,
}
}
fn cell_display_width(values: &[Node], options: &RenderOptions) -> usize {
UnicodeWidthStr::width(render_cell_values(values, options, &ColorTheme::PLAIN).as_str())
}
pub(super) fn write_padded_cell(
output: &mut String,
content: &str,
plain_width: usize,
width: usize,
align: &TableAlignKind,
) {
let pad = width.saturating_sub(plain_width);
match align {
TableAlignKind::Right => {
for _ in 0..pad {
output.push(' ');
}
output.push_str(content);
}
TableAlignKind::Center => {
let left = pad / 2;
let right = pad - left;
for _ in 0..left {
output.push(' ');
}
output.push_str(content);
for _ in 0..right {
output.push(' ');
}
}
TableAlignKind::Left | TableAlignKind::None => {
output.push_str(content);
for _ in 0..pad {
output.push(' ');
}
}
}
}