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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use crate::util::StringBuffer;

use std::fmt;
use derive_builder::Builder;

// https://www.unicode.org/charts/PDF/U2500.pdf
const VERTICAL: &str = "\u{2502}"; // │

const HORIZONTAL: &str = "\u{2500}"; // ─
const HORIZONTAL_HEADER: &str = "\u{2550}"; // ═

const TOP_BRACE: &str = "\u{252C}"; // ┬
const TOP_BRACE_HEADER: &str = "\u{2564}"; // ╤

const BOTTOM_BRACE: &str = "\u{2534}"; // ┴
const LEFT_BRACE: &str = "\u{251c}"; // ├
const LEFT_BRACE_HEADER: &str = "\u{255e}"; // ╞
const RIGHT_BRACE: &str = "\u{2524}"; // ┤
const RIGHT_BRACE_HEADER: &str = "\u{2561}"; // ╡
const MIDDLE_BRACE: &str = "\u{253C}"; // ┼
const MIDDLE_BRACE_HEADER: &str = "\u{256a}"; // ╪

const TOP_LEFT_CORNER: &str = "\u{250c}"; // ┌
const TOP_RIGHT_CORNER: &str = "\u{2510}"; // ┐

const TOP_LEFT_CORNER_HEADER: &str = "\u{2552}"; // ╒
const TOP_RIGHT_CORNER_HEADER: &str = "\u{2555}"; // ╕

const BOTTOM_RIGHT_CORNER: &str = "\u{2518}"; // ┘
const BOTTOM_LEFT_CORNER: &str = "\u{2514}"; // └

#[derive(Builder, Clone, Default)]
pub struct Header {
    /// Whether to use double bar Unicode characters surrounding the header
    double_bar: bool,
    /// Whether to center the header text within each column
    centered_text: bool,
}

#[derive(Builder)]
pub struct Table<'a, T>
where
    T: AsRef<str>,
    &'a T: AsRef<str>,
{
    /// If you provide header settings, the first row will be treated as headers
    #[builder(default)]
    header: Header,
    /// Rows holding the data
    rows: &'a Vec<Vec<T>>,
}

impl <'a, T> Table<'a, T>
where
    T: AsRef<str>,
    &'a T: AsRef<str>
{
    /// Creates a list of byte vectors corresponding to each string of horizontal separators
    fn generate_horizontal_separators(column_widths: &Vec<usize>, horizontal_char: &str) -> Vec<Vec<u8>> {
        column_widths
            .iter()
            .map(|&length| horizontal_char.repeat(length).into_bytes())
            .collect::<Vec<_>>()
    }

    /// Given a 2D input, returns the minimum width of each column in a vector
    fn get_column_widths(&self) -> Vec<usize>
    where
        T: AsRef<str>,
        &'a T: AsRef<str>
    {
        let header_widths: Vec<usize> = {
            if let Some(row) = self.rows.get(0) {
                let header_padding = {
                    if self.header.centered_text {
                        2
                    }
                    else {
                        0
                    }
                };

                row.iter().map(|entry| entry.as_ref().len() + header_padding).collect()
            }
            else {
                return Vec::new();
            }
        };

        let mut widths = self.rows
            .iter()
            .map(|row| row
                .into_iter()
                .map(|entry| entry.as_ref().len())
            )
            .skip(1)
            .fold(header_widths.clone(), |mut column_widths: Vec<usize>, row| {
                for (a, b) in column_widths.iter_mut().zip(row) {
                    // if b is bigger than the current column width, set the new column width to b
                    *a = b.max(*a);
                }

                column_widths
            });

        if self.header.centered_text {
            // make sure that header has an even number of spaces on either side
            for (col_width, header_width) in widths.iter_mut().zip(header_widths.into_iter()) {
                let num_spaces = *col_width - header_width;

                // let there be a space on either side
                if num_spaces == 0 {
                    *col_width += 2;
                }
                // make it an even number of spaces
                else if num_spaces % 2 == 1 {
                    *col_width += 1;
                }
            }
        }

        widths
    }
}

impl <'a, T> fmt::Display for Table<'a, T>
where
    T: AsRef<str>,
    &'a T: AsRef<str>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let column_widths = self.get_column_widths();

        let total_width_per_row = {
            // one separator to the left of each one, as well as one separator on the very right
            let num_separators_per_row = column_widths.len() + 1;

            let base_width_per_row: usize = column_widths.iter().sum();

            base_width_per_row + num_separators_per_row
        };

        let string_length = {
            let total_lines = {
                let num_rows = self.rows.len();

                // separator above each row PLUS one under the last row
                let num_separator_lines = num_rows + 1;

                num_separator_lines + num_rows
            };

            // no newline after the last line
            let num_newlines = total_lines - 1;

            (total_width_per_row * total_lines) + num_newlines
        };

        // fill string with spaces
        let mut buffer = StringBuffer::with_capacity_fill(string_length, ' ');

        let standard_horizontal_separators = Self::generate_horizontal_separators(&column_widths, HORIZONTAL);
        let header_horizontal_separators = Self::generate_horizontal_separators(&column_widths, HORIZONTAL_HEADER);

        let create_sep_row = |horizontal_separators: &Vec<Vec<u8>>, left_char, middle_char, right_char, newline| {
            let mut sep_buffer = {
                // allocate one extra byte if there is a newline at the end
                let newline_increment = if newline { 1 } else { 0 };
                let capacity = total_width_per_row + newline_increment;
                StringBuffer::with_capacity(capacity)
            };

            sep_buffer.push_chars(left_char);

            let (last_sep, seps) = horizontal_separators.split_last().unwrap();

            for sep in seps {
                sep_buffer.push_bytes(sep);
                sep_buffer.push_chars(middle_char);
            }

            sep_buffer.push_bytes(&last_sep);
            sep_buffer.push_chars(right_char);

            if newline {
                sep_buffer.push_chars("\n")
            }

            sep_buffer.into_buffer()
        };

        let mut input_iterator = self.rows.iter().peekable();

        macro_rules! push_data_row {
            ($col_formatter:expr) => {
                if let Some(row) = input_iterator.next() {
                    buffer.push_chars(VERTICAL);
                    for (col_index, col) in row.into_iter().enumerate() {
                        let base = col.as_ref();

                        buffer.push_chars_fixed_width($col_formatter(base, column_widths[col_index]).as_ref(), column_widths[col_index]);
                        buffer.push_chars(VERTICAL);
                    }

                    buffer.push_chars("\n");
                }
            }
        }

        let standard_separator = create_sep_row(&standard_horizontal_separators, LEFT_BRACE, MIDDLE_BRACE, RIGHT_BRACE, true);

        macro_rules! push_header_data_row {
            () => (
                if self.header.centered_text {
                    // align header text in the center of each column
                    push_data_row!(|base_str, width| format!("{:^width$}", base_str, width = width));
                }
                else {
                    push_data_row!(|base_str, _| base_str);
                }
            )
        }

        if self.header.double_bar {
            let header_top = create_sep_row(&header_horizontal_separators, TOP_LEFT_CORNER_HEADER, TOP_BRACE_HEADER, TOP_RIGHT_CORNER_HEADER, true);
            buffer.push_bytes(&header_top);

            push_header_data_row!();

            let header_bottom = create_sep_row(&header_horizontal_separators, LEFT_BRACE_HEADER, MIDDLE_BRACE_HEADER, RIGHT_BRACE_HEADER, true);
            buffer.push_bytes(&header_bottom);

            // TODO
            // handle case where there are no remaining rows
            // currently, it will output an empty body
        }
        else {
            let header = create_sep_row(&standard_horizontal_separators, TOP_LEFT_CORNER, TOP_BRACE, TOP_RIGHT_CORNER, true);
            // add header
            buffer.push_bytes(&header);
            push_header_data_row!();

            // only put the separator if there are rows under
            if input_iterator.peek().is_some() {
                buffer.push_bytes(&standard_separator);
            }
        }

        let footer = create_sep_row(&standard_horizontal_separators, BOTTOM_LEFT_CORNER, BOTTOM_BRACE, BOTTOM_RIGHT_CORNER, false);

        loop {
            push_data_row!(|base_str, _| base_str);

            // only create a standard separator row if there are more data rows
            if input_iterator.peek().is_some() {
                buffer.push_bytes(&standard_separator);
            }
            else {
                // break out if there are no more data rows
                break;
            }
        }

        // add footer
        buffer.push_bytes(&footer);

        write!(f, "{}", buffer.to_string())
    }
}