comfy_table/utils/
mod.rs

1pub mod arrangement;
2pub mod formatting;
3
4use crate::style::{CellAlignment, ColumnConstraint};
5use crate::{Column, Table};
6
7use arrangement::arrange_content;
8use formatting::borders::draw_borders;
9use formatting::content_format::format_content;
10
11/// This struct is ONLY used when table.to_string() is called.
12/// It's purpose is to store intermediate results, information on how to
13/// arrange the table and other convenience variables.
14///
15/// The idea is to have a place for all this intermediate stuff, without
16/// actually touching the Column struct.
17#[derive(Debug)]
18pub struct ColumnDisplayInfo {
19    pub padding: (u16, u16),
20    pub delimiter: Option<char>,
21    /// The actual allowed content width after arrangement
22    pub content_width: u16,
23    /// The content alignment of cells in this column
24    pub cell_alignment: Option<CellAlignment>,
25    is_hidden: bool,
26}
27
28impl ColumnDisplayInfo {
29    pub fn new(column: &Column, mut content_width: u16) -> Self {
30        // The min contend width may only be 1
31        if content_width == 0 {
32            content_width = 1;
33        }
34        Self {
35            padding: column.padding,
36            delimiter: column.delimiter,
37            content_width,
38            cell_alignment: column.cell_alignment,
39            is_hidden: matches!(column.constraint, Some(ColumnConstraint::Hidden)),
40        }
41    }
42
43    pub fn width(&self) -> u16 {
44        self.content_width
45            .saturating_add(self.padding.0)
46            .saturating_add(self.padding.1)
47    }
48}
49
50pub fn build_table(table: &Table) -> impl Iterator<Item = String> {
51    let display_info = arrange_content(table);
52    let content = format_content(table, &display_info);
53    draw_borders(table, &content, &display_info).into_iter()
54}