1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use super::border::Border;
use super::cell::Cell;
use crate::content::{CellWidth};

pub struct CellIterator<'a> {
    cells: &'a Vec<Cell>,
    current_cell_ix: usize
}

impl<'a> Iterator for CellIterator<'a> {
    type Item = &'a Cell;

    fn next(&mut self) -> Option<&'a Cell> {
        if self.current_cell_ix < self.cells.len() {
            let cell: &Cell = &self.cells[self.current_cell_ix];
            self.current_cell_ix += 1;
            Some(cell)
        } else {
            None
        }
    }
}

#[allow(unused_macros)]
#[macro_export]
macro_rules! row {
    ( $($style:expr => $content:expr),* ) => {
        {
            let mut r: Row = Row::new();
            $( r.add_cell(crate::cell!($style, $content)); )*
            r
        }
    };
    ( $style:expr, $($content:expr),* ) => {
        {
            let mut r: Row = Row::new();
            $( r.add_cell(crate::cell!($style, $content)); )*
            r
        }
    };
}

/// Table rows represent horizontal breakpoints.
#[derive(Debug)]
pub struct Row {
    cells: Vec<Cell>
}

impl Default for Row {
    fn default() -> Self {
        Self::new()
    }
}

impl Row {
    #[must_use]
    pub fn new() -> Row {
        Row {
            cells: Vec::new()
        }
    }

    #[must_use]
    pub fn from(
        cells: Vec<Cell>
    ) -> Row {
        Row { cells }
    }

    pub fn add_cell(&mut self, cell: Cell) {
        self.cells.push(cell);
    }

    #[must_use]
    pub fn iter(
        self: &Row,
    ) -> CellIterator {
        CellIterator {
            cells: &self.cells,
            current_cell_ix: 0
        }
    }

    #[must_use]
    pub fn len(self: &Row) -> usize {
        self.cells.len()
    }

    #[must_use]
    pub fn is_empty(self: &Row) -> bool {
        self.len() == 0
    }

    /// Formats a table row.
    ///
    /// # Arguments
    ///
    /// * `self` - The table row to format.
    /// * `border` - The table border.
    /// * `column_breaks` - The breakpoints at which to wrap or truncate.
    #[must_use]
    #[allow(clippy::option_if_let_else)]
    pub fn format(
        self: &Row,
        border: &Border,
        column_breaks: &[CellWidth]
    ) -> String {
        let mut result: String = String::from("");

        let row_height = self.measure_height(column_breaks);

        // Get content iterators for each cell
        let mut content_iterators = Vec::new();
        for (cell_ix, cell) in self.cells.iter().enumerate() {
            let column_break = &column_breaks[cell_ix];
            content_iterators.push(cell.get_iterator(&column_break));
        }

        // Iterate the number of lines
        let content_break = CellWidth::default();
        for _line_ix in 0..row_height {
            // Left border
            result.push_str(&border.format_left());
            // Write the contents for the current line of the cell
            for cell_ix in 0..self.cells.len() {
                let cell = &self.cells[cell_ix];
                let column_break: &CellWidth =
                    if cell_ix < column_breaks.len() {
                        &column_breaks[cell_ix]
                    } else {
                        &content_break
                    };
                result.push_str(
                    &if let Some(content) = content_iterators[cell_ix].next() {
                        content.to_string()
                    } else {
                        // No more lines so fill height with empty space
                        let cell_width = cell.measure_width(column_break);
                        (0..cell_width)
                            .map(|_| " ")
                            .collect::<String>()
                    }
                );
                // Vertical split (except for final column)
                if cell_ix < column_breaks.len() - 1 {
                    result.push_str(&border.format_vertical_split());
                }
            }
            // Right border
            result.push_str(&border.format_right());
            result.push('\n');
        }

        result
    }

    /// Measures the height of a table row.
    ///
    /// # Arguments
    ///
    /// * `self` - The table row being measured.
    /// * `columns` - The columns used to format the cells for this row.
    #[must_use]
    pub fn measure_height(
        self: &Row,
        column_breaks: &[CellWidth],
    ) -> usize {
        let mut tallest_height = 0;

        // Iterate the row cells and measure based upon supplied column breaks
        let column_break_ix = 0;
        let content_break = CellWidth::Content;
        for cell in &self.cells {
            // Get the next column break (if one is available)
            let column_break: &CellWidth = 
                if column_break_ix < column_breaks.len() {
                    &column_breaks[column_break_ix]
                } else {
                    // Use content-width break for additional columns
                    &content_break
                };
            let cell_height = cell.measure_height(column_break);
            if cell_height > tallest_height {
                tallest_height = cell_height;
            }
        }

        tallest_height
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_row_macro_style_per_cell() {
        assert_eq!(
            format!("{:?}", row!("{c^}" => "Head 1", "{G-r>}" => "Head 2")),
            format!("{:?}", Row::from(
                vec!(
                    crate::cell!("{c^}", "Head 1"),
                    crate::cell!("{G-r>}", "Head 2")
                )
            ))
        );
    }

    #[test]
    fn test_row_macro_common_style() {
        assert_eq!(
            format!("{:?}", row!("{c^}", "Text 1", "Text 2", "Text 3")),
            format!("{:?}", Row::from(
                vec!(
                    crate::cell!("{c^}", "Text 1"),
                    crate::cell!("{c^}", "Text 2"),
                    crate::cell!("{c^}", "Text 3")
                )
            ))
        );
    }
}