cli_grid/grid.rs
1#![allow(dead_code)]
2
3use crate::{options::Options, row::Row, HAlign, VAlign};
4
5/// Builder for the [`Grid`] type.
6///
7/// [`Grid`]: struct.Grid.html
8pub struct GridBuilder {
9 inner: Grid,
10}
11
12/// Data type for crating a [`Grid`].
13///
14/// [`Grid`]: struct.Grid.html
15pub struct Grid {
16 /// These options will be used if the equivalent is not provided
17 /// by the underlying [`Row`] type.
18 ///
19 /// [`Row`]: struct.Row.html
20 pub default_options: Options,
21
22 /// Width in chars for each column of the [`Grid`].
23 ///
24 /// [`Grid`]: struct.Grid.html
25 pub column_width: Option<usize>,
26
27 /// Number of char spaces for each padding space between grid columns.
28 pub padding_size: Option<usize>,
29
30 /// Collection of rows that this [`Grid`] contains.
31 ///
32 /// [`Grid`]: struct.Grid.html
33 pub rows: Vec<Row>,
34}
35
36impl Grid {
37 /// Creates a [`Grid`] bye specifying its rows.
38 ///
39 /// [`Grid`]: struct.Grid.html
40 pub fn new(rows: Vec<Row>) -> Self {
41 let default_options = Options {
42 col_span: None,
43 h_align: None,
44 v_align: None,
45 blank_char: None,
46 };
47 let grid = Self {
48 default_options,
49 column_width: None,
50 padding_size: None,
51 rows,
52 };
53 grid
54 }
55
56 /// Creates a [`GridBuilder`] initiated with rows.
57 ///
58 /// [`GridBuilder`]: struct.GridBuilder.html
59 pub fn builder(rows: Vec<Row>) -> GridBuilder {
60 GridBuilder {
61 inner: Grid::new(rows),
62 }
63 }
64
65 /// Format the grid into a string.
66 fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 for row in &self.rows {
68 row.render(
69 f,
70 &self.default_options,
71 self.column_width,
72 self.padding_size,
73 )?;
74 }
75 Ok(())
76 }
77}
78
79impl std::fmt::Display for Grid {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 self.render(f)
82 }
83}
84
85impl GridBuilder {
86 /// Builds a [`Grid`] from a [`GridBuilder`].
87 ///
88 /// [`GridBuilder`]: struct.GridBuilder.html
89 /// [`Grid`]: struct.Grid.html
90 pub fn build(self) -> Grid {
91 self.inner
92 }
93
94 /// Sets the default column span for all the cells of the grid. If a cell specifies
95 /// a column span it will be used instead of the grids default value.
96 pub fn default_colspan(mut self, default_colspan: usize) -> Self {
97 if default_colspan == 0 {
98 panic!("Column span cannot be 0!");
99 }
100 self.inner.default_options.col_span = Some(default_colspan);
101 self
102 }
103
104 /// Sets the default horizontal alignment for all the cells of the grid. If a cell specifies
105 /// a horizontal alignment it will be used instead of the grids default value.
106 pub fn default_h_align(mut self, default_h_align: HAlign) -> Self {
107 self.inner.default_options.h_align = Some(default_h_align);
108 self
109 }
110
111 /// Sets the default vertical alignment for all the cells of the grid. If a cell specifies
112 /// a vertical alignment it will be used instead of the grids default value.
113 pub fn default_v_align(mut self, default_v_align: VAlign) -> Self {
114 self.inner.default_options.v_align = Some(default_v_align);
115 self
116 }
117
118 /// Sets the default blank char for all the cells of the grid. If a cell specifies
119 /// a blank char it will be used instead of the grids default value.
120 pub fn default_blank_char(mut self, default_blank_char: char) -> Self {
121 self.inner.default_options.blank_char = Some(default_blank_char);
122 self
123 }
124
125 /// Width of each column in the grid in number of chars.
126 pub fn column_width(mut self, column_width: usize) -> Self {
127 self.inner.column_width = Some(column_width);
128 self
129 }
130
131 /// Width of each padding space in the grid in number of chars.
132 pub fn padding_size(mut self, padding_size: usize) -> Self {
133 self.inner.padding_size = Some(padding_size);
134 self
135 }
136}