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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/// A `PreTable` struct used for table representation
#[derive(Debug)]
pub struct PreTable {
    items: Vec<Item>,
    body_length: usize,
    show_header: bool,
    is_body_split: bool,
    line_char: char,
    vertical_char: char,
    corner_char: char,
    default_alignment: Alignment,
}

impl PreTable {
    /// Creates a new instance of `PreTable`
    pub fn new() -> Self {
        Self {
            items: Vec::new(),
            body_length: 0,
            show_header: true,
            is_body_split: false,
            line_char: '-',
            vertical_char: '|',
            corner_char: '+',
            default_alignment: Alignment::Center,
        }
    }

    /// Adds a header to the table with default alignment
    pub fn add_header(&mut self, v: &str) {
        self.add_header_with_alignment(v, self.default_alignment);
    }

    /// Adds a header with a specified alignment
    ///
    /// # Examples
    ///
    /// ```
    /// # use pretable::{Alignment, PreTable};
    /// let mut table = PreTable::new();
    /// table.add_header_with_alignment("KEY", Alignment::Left);
    /// ```
    pub fn add_header_with_alignment(&mut self, v: &str, alignment: Alignment) {
        self.items.push(Item::new(v, alignment));
    }

    /// Sets the headers of the table with default alignment
    ///
    /// # Examples
    ///
    /// ```
    /// # use pretable::{Alignment, PreTable};
    /// let mut table = PreTable::new();
    /// table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
    /// ```
    pub fn set_header(&mut self, v: Vec<&str>) {
        self.items = Vec::new();
        for value in v {
            self.add_header_with_alignment(value, self.default_alignment);
        }
    }

    /// Sets the headers of the table with a specified alignment
    ///
    /// # Examples
    ///
    /// ```
    /// # use pretable::{Alignment, PreTable};
    /// let mut table = PreTable::new();
    /// table.set_header_with_alignment(vec![
    ///     ("KEY", Alignment::Left),
    ///     ("VALUE", Alignment::Center),
    ///     ("DESCRIPTION", Alignment::Right),
    /// ]);
    /// ```
    pub fn set_header_with_alignment(&mut self, v: Vec<(&str, Alignment)>) {
        self.items = Vec::new();
        for value in v {
            self.add_header_with_alignment(value.0, value.1);
        }
    }

    /// Adds a row to the body of the table
    ///
    /// # Examples
    ///
    /// ```
    /// # use pretable::{Alignment, PreTable};
    /// let mut table = PreTable::new();
    /// table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
    /// table.add_body(vec!["key1", "value1", "description1"]);
    /// ```
    pub fn add_body(&mut self, v: Vec<&str>) {
        self.body_length += 1;
        for n in 0..self.items.len() {
            let value = if v.len() > n { v[n] } else { "" };
            self.items[n].value.push(value.to_string());
            if self.items[n].max_value_len < value.len() {
                self.items[n].max_value_len = value.len();
            }
        }
    }

    /// Generates a line string based on the current state of table
    pub fn line(&self) -> String {
        let s: Vec<String> = self
            .items
            .iter()
            .map(|item| {
                let mut s = String::with_capacity(1 + item.max_value_len + 2);
                s.push(self.corner_char);
                s.extend(std::iter::repeat(self.line_char).take(item.max_value_len + 2));
                s
            })
            .collect();

        format!("{}{}", s.concat(), self.corner_char)
    }

    /// Formats and returns the header as a string
    fn header(&self) -> String {
        let s: Vec<String> = self
            .items
            .iter()
            .map(|item| {
                let mut s = self.vertical_char.to_string();
                Self::format_align(&item.key, &item.max_value_len + 2, item.alignment, &mut s);
                s
            })
            .collect();

        format!("{}{}", s.concat(), self.vertical_char)
    }

    /// Formats and returns the body as a vector of strings
    fn body(&self) -> Vec<String> {
        let mut v: Vec<_> = self.items.iter().map(|item| item.value.iter()).collect();
        let value_len_vec: Vec<_> = self.items.iter().map(|item| item.max_value_len).collect();

        let mut vec = Vec::with_capacity(self.body_length);
        for _ in 0..self.body_length {
            let r = v.next();
            let mut n = 0;
            let mut inc = || {
                n += 1;
                n - 1
            };

            let mut result = String::new();
            for ref value in r {
                result.push(self.vertical_char);

                let item_index = inc();

                Self::format_align(
                    match value {
                        &Some(vv) => vv,
                        &None => "",
                    },
                    value_len_vec[item_index] + 2,
                    self.items[item_index].alignment,
                    &mut result,
                );
            }
            result.push(self.vertical_char);
            vec.push(result);
        }

        vec
    }

    /// Returns the complete table as a string
    pub fn output(&self) -> String {
        let mut buf = vec![];
        let l = self.line();

        buf.push(l.clone());
        if self.show_header && !self.items.is_empty() {
            buf.push(self.header());
            buf.push(l.clone());
        }
        let body = self.body();
        if self.is_body_split {
            for b in body {
                buf.push(b);
                buf.push(l.clone());
            }
        } else {
            buf.push(body.join("\n"));
            buf.push(l.clone());
        }

        buf.join("\n")
    }

    /// Decides whether to show the header in output
    pub fn set_show_header(&mut self, b: bool) {
        self.show_header = b;
    }

    /// Decides whether to split the body in output
    pub fn set_body_split(&mut self, b: bool) {
        self.is_body_split = b;
    }

    /// Sets the character used for line separation
    pub fn set_line_char(&mut self, c: char) {
        self.line_char = c;
    }

    /// Sets the character used for vertical separation
    pub fn set_vertical_char(&mut self, c: char) {
        self.vertical_char = c;
    }

    /// Sets the character used for corners
    pub fn set_corner_char(&mut self, c: char) {
        self.corner_char = c;
    }

    /// Sets the alignment used for format align
    pub fn set_default_alignment(&mut self, a: Alignment) {
        self.default_alignment = a;
    }

    /// Helper function to format string according to alignment
    fn format_align(v: &str, count: usize, alignment: Alignment, buf: &mut String) {
        let padding = count - v.len();
        let start = match alignment {
            Alignment::Left => 1,
            Alignment::Center => padding / 2,
            Alignment::Right => padding - 1,
        };
        let end = padding - start;

        buf.extend(std::iter::repeat(' ').take(start));
        *buf += v;
        buf.extend(std::iter::repeat(' ').take(end));
    }
}

/// An `Item` struct represents a column in the table
#[derive(Debug)]
pub struct Item {
    key: String,
    value: Vec<String>,
    max_value_len: usize,
    alignment: Alignment,
}

impl Item {
    fn new(key: &str, alignment: Alignment) -> Self {
        Self {
            key: key.to_string(),
            value: Vec::new(),
            max_value_len: key.len(),
            alignment: alignment,
        }
    }
}

/// An enum for alignment options
#[derive(Debug, Clone, Copy)]
pub enum Alignment {
    Center,
    Left,
    Right,
}

trait SliceItarator {
    fn next(&mut self) -> Vec<Option<&String>>;
}

impl<'a> SliceItarator for Vec<std::slice::Iter<'a, String>> {
    fn next(&mut self) -> Vec<Option<&String>> {
        let mut values = Vec::with_capacity(self.len());
        for v in self {
            values.push(v.next());
        }
        values
    }
}

#[cfg(test)]
mod tests {
    use super::{Alignment, PreTable};

    fn generate_test_table() -> PreTable {
        let mut table = PreTable::new();
        table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
        table.add_body(vec!["key1", "value1", "description1"]);
        table.add_body(vec!["key2", "long value 2", "description2"]);
        table.add_body(vec!["key3", "value3", "description3"]);

        table
    }

    #[test]
    fn test_output() {
        let table = generate_test_table();

        let actual = table.output();
        let expected = "+------+--------------+--------------+
| KEY  |    VALUE     | DESCRIPTION  |
+------+--------------+--------------+
| key1 |    value1    | description1 |
| key2 | long value 2 | description2 |
| key3 |    value3    | description3 |
+------+--------------+--------------+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_skip_item() {
        let mut table = PreTable::new();
        table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
        table.add_body(vec!["key1", "value1"]);
        table.add_body(vec!["key2", "", "description2"]);

        let actual = table.output();
        let expected = "+------+--------+--------------+
| KEY  | VALUE  | DESCRIPTION  |
+------+--------+--------------+
| key1 | value1 |              |
| key2 |        | description2 |
+------+--------+--------------+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_set_line_char() {
        let mut table = generate_test_table();
        table.set_line_char('x');

        let actual = table.output();
        let expected = "+xxxxxx+xxxxxxxxxxxxxx+xxxxxxxxxxxxxx+
| KEY  |    VALUE     | DESCRIPTION  |
+xxxxxx+xxxxxxxxxxxxxx+xxxxxxxxxxxxxx+
| key1 |    value1    | description1 |
| key2 | long value 2 | description2 |
| key3 |    value3    | description3 |
+xxxxxx+xxxxxxxxxxxxxx+xxxxxxxxxxxxxx+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_set_vertical_char() {
        let mut table = generate_test_table();
        table.set_vertical_char('x');

        let actual = table.output();
        let expected = "+------+--------------+--------------+
x KEY  x    VALUE     x DESCRIPTION  x
+------+--------------+--------------+
x key1 x    value1    x description1 x
x key2 x long value 2 x description2 x
x key3 x    value3    x description3 x
+------+--------------+--------------+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_set_corner_char() {
        let mut table = generate_test_table();
        table.set_corner_char('x');

        let actual = table.output();
        let expected = "x------x--------------x--------------x
| KEY  |    VALUE     | DESCRIPTION  |
x------x--------------x--------------x
| key1 |    value1    | description1 |
| key2 | long value 2 | description2 |
| key3 |    value3    | description3 |
x------x--------------x--------------x";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_set_show_header() {
        let mut table = generate_test_table();
        table.set_show_header(false);

        let actual = table.output();
        let expected = "+------+--------------+--------------+
| key1 |    value1    | description1 |
| key2 | long value 2 | description2 |
| key3 |    value3    | description3 |
+------+--------------+--------------+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_set_body_split() {
        let mut table = generate_test_table();
        table.set_body_split(true);

        let actual = table.output();
        let expected = "+------+--------------+--------------+
| KEY  |    VALUE     | DESCRIPTION  |
+------+--------------+--------------+
| key1 |    value1    | description1 |
+------+--------------+--------------+
| key2 | long value 2 | description2 |
+------+--------------+--------------+
| key3 |    value3    | description3 |
+------+--------------+--------------+";
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_format_align_center() {
        let mut buf = String::new();
        PreTable::format_align("abcde", 15, Alignment::Center, &mut buf);
        assert_eq!(buf, "     abcde     ");
    }

    #[test]
    fn test_format_align_left() {
        let mut buf = String::new();
        PreTable::format_align("abcde", 15, Alignment::Left, &mut buf);
        assert_eq!(buf, " abcde         ");
    }

    #[test]
    fn test_format_align_right() {
        let mut buf = String::new();
        PreTable::format_align("abcde", 15, Alignment::Right, &mut buf);
        assert_eq!(buf, "         abcde ");
    }
}