cli_grid/
cell.rs

1#![allow(dead_code)]
2
3pub const DEFAULT_COLSPAN: usize = 1;
4pub const DEFAULT_H_ALIGN: HAlign = HAlign::Left;
5pub const DEFAULT_V_ALIGN: VAlign = VAlign::Top;
6pub const DEFAULT_BLANK_CHAR: char = '\x20';
7
8/// Horizontal alignments for a cell.
9#[derive(Clone, Copy)]
10pub enum HAlign {
11    /// Left align contents of the cell. (default)
12    Left,
13
14    /// Right align contents of the cell
15    Right,
16
17    /// Center align contents of the cell
18    Center,
19
20    /// Fills the entire width of the cell with repeating the content.
21    Fill,
22}
23
24/// Vertical alignments for a cell.
25#[derive(Clone, Copy)]
26pub enum VAlign {
27    /// Top align contents of the cell. (default)
28    Top,
29
30    /// Bottom align contents of the cell
31    Bottom,
32
33    /// Middle align contents of the cell
34    Middle,
35}
36
37/// Data type that represents options for a cell and its content.
38pub struct Cell {
39    /// The content of the cell. It can be a multi line string or even a nested [`Grid`].
40    ///
41    /// [`Grid`]: struct.Grid.html
42    pub content: String,
43
44    /// Number of columns that this cell will be spreads out into.
45    /// If `None` specified, the value [`col_span`] of the grid will
46    /// be used. If [`col_span`] of the grid also is `None`, value 1
47    /// will be used.
48    ///
49    /// [`col_span`]: struct.Options.html#structfield.col_span
50    ///
51    /// # Panics
52    ///
53    /// Panics if `0` is specified.
54    pub col_span: Option<usize>,
55
56    /// Align content of the cell horizontally. If `None` specified,
57    /// the value [`h_align`] of the grid will be used. If [`h_align`]
58    /// of the grid also is `None`, [`HAlign::Left`] will be used.
59    ///
60    /// [`h_align`]: struct.Options.html#structfield.h_align
61    /// [`HAlign::Left`]: enum.HAlign.html#variant.Left
62    pub h_align: Option<HAlign>,
63
64    /// Align content of the cell vertically. If `None` specified,
65    /// the value [`v_align`] of the grid will be used. If [`v_align`]
66    /// of the grid also is `None`, [`VAlign::Top`] will be used.
67    ///
68    /// [`v_align`]: struct.Options.html#structfield.v_align
69    /// [`VAlign::Top`]: enum.VAlign.html#variant.Top
70    pub v_align: Option<VAlign>,
71
72    /// Fill cell padding spaces by this char value. If `None` specified,
73    /// the value [`blank_char`] of the grid will be used. If [`blank_char`]
74    /// of the grid also is `None`, white space char (`'\x20'`) will be used.
75    ///
76    /// [`blank_char`]: struct.Options.html#structfield.blank_char
77    pub blank_char: Option<char>,
78}
79
80impl Cell {
81    /// Create a new [`Cell`] by its `content` and [`col_span`] properties.
82    /// To specify other properties, use the [`builder`] method instead.
83    ///
84    /// [`Cell`]: struct.Cell.html
85    /// [`col_span`]: struct.Cell.html#structfield.col_span
86    /// [`builder`]: struct.Cell.html#method.builder
87    pub fn new(content: String, col_span: usize) -> Self {
88        if col_span == 0 {
89            panic!("Column span cannot be 0");
90        }
91        Self {
92            content,
93            col_span: Some(col_span),
94            h_align: None,
95            v_align: None,
96            blank_char: None,
97        }
98    }
99
100    /// Creates a [`Cell`] with the specified [`col_span`].
101    /// The entire width of the cell will be filled by repeating the content.
102    ///
103    /// [`Cell`]: struct.Cell.html
104    /// [`col_span`]: struct.Cell.html#structfield.col_span
105    pub fn new_fill(content: String, col_span: usize) -> Self {
106        Cell::builder(content, col_span).h_align(HAlign::Fill).build()
107    }
108
109    /// Creates an empty [`Cell`] with the specified [`col_span`].
110    /// To specify other properties, use the [`builder`] method instead.
111    ///
112    /// [`Cell`]: struct.Cell.html
113    /// [`col_span`]: struct.Cell.html#structfield.col_span
114    /// [`builder`]: struct.Cell.html#method.builder
115    pub fn new_empty(col_span: usize) -> Self {
116        Cell::new("".into(), col_span)
117    }
118
119    /// Creates a [`CellBuilder`] initiated with `content` and [`col_span`] properties.
120    /// To build the final [`CellBuilder`] call the [`build`] method.
121    ///
122    /// [`CellBuilder`]: struct.CellBuilder.html
123    /// [`col_span`]: struct.Cell.html#structfield.col_span
124    /// [`build`]: struct.CellBuilder.html#method.build
125    pub fn builder(content: String, col_span: usize) -> CellBuilder {
126        CellBuilder {
127            inner: Cell::new(content, col_span),
128        }
129    }
130}
131
132/// Builder for the [`Cell`] type.
133///
134/// [`Cell`]: struct.CEll.html
135pub struct CellBuilder {
136    inner: Cell,
137}
138
139impl CellBuilder {
140    /// Builds a [`Cell`] from a [`CellBuilder`].
141    ///
142    /// [`Cell`]: struct.Cell.html
143    /// [`CellBuilder`]: struct.CellBuilder.html
144    pub fn build(self) -> Cell {
145        self.inner
146    }
147
148    /// Sets the content of the cell. It can be a multi line string or even a nested [`Grid`].
149    /// To build the final [`Cell`] type, [`build`] method must be called.
150    ///
151    /// [`Grid`]: struct.Grid.html
152    /// [`Cell`]: struct.Cell.html
153    /// [`build`]: struct.CellBuilder.html#method.build
154    pub fn content(mut self, content: String) -> Self {
155        self.inner.content = content;
156        self
157    }
158
159    /// Sets the column span of the cell
160    /// To build the final [`Cell`] type, [`build`] method must be called.
161    ///
162    /// [`Cell`]: struct.Cell.html
163    /// [`build`]: struct.CellBuilder.html#method.build
164    pub fn col_span(mut self, col_span: usize) -> Self {
165        self.inner.col_span = Some(col_span);
166        self
167    }
168
169    /// Sets the horizontal alignment of the cell.
170    /// To build the final [`Cell`] type, [`build`] method must be called.
171    ///
172    /// [`Cell`]: struct.Cell.html
173    /// [`build`]: struct.CellBuilder.html#method.build
174    pub fn h_align(mut self, h_align: HAlign) -> Self {
175        self.inner.h_align = Some(h_align);
176        self
177    }
178
179    /// Sets the vertical alignment of the cell.
180    /// To build the final [`Cell`] type, [`build`] method must be called.
181    ///
182    /// [`Cell`]: struct.Cell.html
183    /// [`build`]: struct.CellBuilder.html#method.build
184    pub fn v_align(mut self, v_align: VAlign) -> Self {
185        self.inner.v_align = Some(v_align);
186        self
187    }
188
189    /// Sets the blank char as the cell padding space char.
190    /// To build the final [`Cell`] type, [`build`] method must be called.
191    ///
192    /// [`Cell`]: struct.Cell.html
193    /// [`build`]: struct.CellBuilder.html#method.build
194    pub fn blank_char(mut self, blank_char: char) -> Self {
195        self.inner.blank_char = Some(blank_char);
196        self
197    }
198}