box_drawing_table/
lib.rs

1pub use ansi_term;
2
3#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
4pub enum Align {
5    Center,
6    CenterPadded { padl: usize, padr: usize },
7    Left,
8    LeftPadded { padl: usize },
9    Right,
10    RightPadded { padr: usize },
11}
12impl Align {
13    pub fn padding_size(&self) -> usize {
14        match self {
15            Self::Center => 0,
16            Self::Left => 0,
17            Self::Right => 0,
18            Self::CenterPadded { padl, padr } => padl + padr,
19            Self::LeftPadded { padl } => *padl,
20            Self::RightPadded { padr } => *padr,
21        }
22    }
23}
24impl std::default::Default for Align {
25    fn default() -> Self {
26        Align::Left
27    }
28}
29
30#[derive(Debug, Clone, PartialEq)]
31pub struct Cell {
32    pub value: String,
33    pub align: Align,
34    pub style: ansi_term::Style,
35}
36
37use ansi_term::Style;
38impl Cell {
39    pub fn left<T: std::fmt::Display>(val: T) -> Self {
40        Cell {
41            value: val.to_string(),
42            align: Align::Left,
43            style: Style::default(),
44        }
45    }
46    pub fn left_with_style<T: std::fmt::Display>(val: T, style: Style) -> Self {
47        Cell {
48            value: val.to_string(),
49            align: Align::Left,
50            style,
51        }
52    }
53
54    pub fn right<T: std::fmt::Display>(val: T) -> Self {
55        Cell {
56            value: val.to_string(),
57            align: Align::Right,
58            style: Style::default(),
59        }
60    }
61    pub fn right_with_style<T: std::fmt::Display>(val: T, style: Style) -> Self {
62        Cell {
63            value: val.to_string(),
64            align: Align::Right,
65            style,
66        }
67    }
68
69    pub fn center<T: std::fmt::Display>(val: T) -> Self {
70        Cell {
71            value: val.to_string(),
72            align: Align::Center,
73            style: Style::default(),
74        }
75    }
76    pub fn center_with_style<T: std::fmt::Display>(val: T, style: Style) -> Self {
77        Cell {
78            value: val.to_string(),
79            align: Align::Center,
80            style,
81        }
82    }
83}
84
85#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
86pub enum Border {
87    Single,
88    Double,
89}
90
91#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
92pub enum CellSize {
93    Flexible,
94    Fixed(usize),
95}
96
97#[derive(Debug, Clone, PartialEq)]
98pub enum Row {
99    HorizontalBorder(Border),
100    Cells { height: CellSize, cells: Vec<Cell> },
101}
102impl Row {
103    pub fn flexible_height(cells: Vec<Cell>) -> Self {
104        Self::Cells {
105            height: CellSize::Flexible,
106            cells,
107        }
108    }
109    pub fn fixed_height(height: usize, cells: Vec<Cell>) -> Self {
110        Self::Cells {
111            height: CellSize::Fixed(height),
112            cells,
113        }
114    }
115
116    pub fn border(&self) -> Option<Border> {
117        match self {
118            Row::HorizontalBorder(b) => Some(*b),
119            _ => None,
120        }
121    }
122    pub fn cells(&self) -> Option<&Vec<Cell>> {
123        match self {
124            Row::Cells { cells, .. } => Some(cells),
125            _ => None,
126        }
127    }
128}
129impl From<Border> for Row {
130    fn from(b: Border) -> Self {
131        Row::HorizontalBorder(b)
132    }
133}
134
135#[derive(Debug, Clone, Hash, PartialEq, Eq)]
136pub enum Column {
137    VerticalBorder(Border),
138    Cells { width: CellSize },
139}
140impl Column {
141    pub fn flexible_width() -> Self {
142        Self::Cells {
143            width: CellSize::Flexible,
144        }
145    }
146    pub fn fixed_width(width: usize) -> Self {
147        Self::Cells {
148            width: CellSize::Fixed(width),
149        }
150    }
151
152    pub fn border(&self) -> Option<Border> {
153        match self {
154            Column::VerticalBorder(b) => Some(*b),
155            _ => None,
156        }
157    }
158}
159impl From<Border> for Column {
160    fn from(b: Border) -> Self {
161        Column::VerticalBorder(b)
162    }
163}
164
165mod table;
166pub use table::*;
167
168#[cfg(test)]
169mod tests;