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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use std::iter::FromIterator;

use crate::{grid::records::vec_records::CellInfo, Table};

use super::IndexBuilder;

/// Builder creates a [`Table`] from dynamic data set.
///
/// It useful when the amount of columns or rows is not known statically.
///
/// ```rust
/// use tabled::builder::Builder;
///
/// let mut builder = Builder::default();
/// builder.push_record(["index", "measure", "value"]);
/// builder.push_record(["0", "weight", "0.443"]);
///
/// let table = builder.build();
///
/// println!("{}", table);
/// ```
///
/// It may be useful to use [`FromIterator`] for building.
///
/// ```rust
/// use tabled::builder::Builder;
/// use std::iter::FromIterator;
///
/// let data = vec![
///     ["column1", "column2"],
///     ["data1", "data2"],
///     ["data3", "data4"],
/// ];
///
/// let table = Builder::from_iter(data).build();
///
/// println!("{}", table);
/// ```
#[derive(Debug, Default, Clone)]
pub struct Builder {
    /// A list of rows.
    data: Vec<Vec<CellInfo<String>>>,
    /// A number of columns.
    count_columns: usize,
    /// A content of cells which are created in case rows has different length.
    empty_text: CellInfo<String>,
}

impl Builder {
    /// Creates a [`Builder`] instance.
    ///
    /// ```
    /// use tabled::builder::Builder;
    ///
    /// let builder = Builder::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a [`Builder`] instance with a given row capacity.
    ///
    /// ```
    /// use tabled::builder::Builder;
    ///
    /// let mut builder = Builder::with_capacity(2, 3);
    /// builder.push_record((0..3).map(|i| i.to_string()));
    /// builder.push_record(["i", "surname", "lastname"]);
    /// ```
    pub fn with_capacity(count_records: usize, count_columns: usize) -> Self {
        let mut builder = Self::new();
        builder.data = Vec::with_capacity(count_records);
        builder.count_columns = count_columns;

        builder
    }

    /// Creates a [`Builder`] instance.
    ///
    /// # Safety
    ///
    /// It's marked unsafe to emphasize that you shall make sure that all rows bound to have the same length.
    ///
    /// ```
    /// use tabled::builder::Builder;
    ///
    /// let data = vec![];
    /// let builder = Builder::from_vec(data);
    /// ```
    pub fn from_vec(data: Vec<Vec<CellInfo<String>>>) -> Self {
        let count_columns = if data.is_empty() { 0 } else { data[0].len() };

        Self {
            data,
            count_columns,
            empty_text: CellInfo::default(),
        }
    }

    /// Sets a content of cells which are created in case rows has different length.
    ///
    ///
    /// ```rust
    /// use tabled::builder::Builder;
    ///
    /// let mut builder = Builder::default();
    /// builder.set_empty("undefined");
    /// builder.push_record((0..3).map(|i| i.to_string()));
    /// builder.push_record(["i"]);
    /// ```
    pub fn set_empty<T>(&mut self, text: T)
    where
        T: Into<String>,
    {
        self.empty_text = CellInfo::new(text.into());
    }

    /// Build creates a [`Table`] instance.
    ///
    /// ```rust
    /// use tabled::builder::Builder;
    ///
    /// let mut builder = Builder::default();
    /// builder.push_record(["i", "column1", "column2"]);
    /// builder.push_record(["0", "value1", "value2"]);
    /// ```
    pub fn build(self) -> Table {
        Table::from(self)
    }

    /// Add an index to the [`Table`].
    ///
    /// Default index is a range 0-N where N is amount of records.
    ///
    /// # Example
    ///
    /// ```
    /// use tabled::Table;
    ///
    /// let table = Table::builder(&["Hello", "World", "!"]).index().build();
    ///
    /// assert_eq!(
    ///     table.to_string(),
    ///     "+---+-------+\n\
    ///      |   | &str  |\n\
    ///      +---+-------+\n\
    ///      | 0 | Hello |\n\
    ///      +---+-------+\n\
    ///      | 1 | World |\n\
    ///      +---+-------+\n\
    ///      | 2 | !     |\n\
    ///      +---+-------+"
    /// )
    /// ```
    pub fn index(self) -> IndexBuilder {
        IndexBuilder::from(self)
    }

    /// Adds a row to a [`Table`].
    ///
    /// ```
    /// use tabled::builder::Builder;
    ///
    /// let mut builder = Builder::default();
    /// builder.push_record((0..3).map(|i| i.to_string()));
    /// builder.push_record(["i", "surname", "lastname"]);
    /// ```
    pub fn push_record<R>(&mut self, record: R)
    where
        R: IntoIterator,
        R::Item: Into<String>,
    {
        let list = create_row(record, self.count_columns, &self.empty_text);
        let list_length = list.len();

        if !is_size_eq(self.count_columns, list_length) {
            let size = list_length - self.count_columns;
            resize_rows(&mut self.data, size, &self.empty_text)
        }

        self.count_columns = list_length;
        self.data.push(list);
    }

    /// Insert a row into a specific position.
    ///
    /// # Panics
    ///
    /// Panics if `index > count_rows`.
    pub fn insert_record<R>(&mut self, index: usize, record: R)
    where
        R: IntoIterator,
        R::Item: Into<String>,
    {
        let list = create_row(record, self.count_columns, &self.empty_text);
        let list_length = list.len();

        if !is_size_eq(self.count_columns, list_length) {
            let size = list_length - self.count_columns;
            resize_rows(&mut self.data, size, &self.empty_text)
        }

        self.count_columns = list_length;
        self.data.insert(index, list);
    }

    /// Clean removes empty columns and rows.
    ///
    /// # Example
    ///
    /// ```
    /// use tabled::Table;
    ///
    /// let mut builder = Table::builder(&["Hello", "World", ""]);
    /// builder.clean();
    ///
    /// let table = builder.build();
    ///
    /// assert_eq!(
    ///     table.to_string(),
    ///     "+-------+\n\
    ///      | &str  |\n\
    ///      +-------+\n\
    ///      | Hello |\n\
    ///      +-------+\n\
    ///      | World |\n\
    ///      +-------+"
    /// )
    /// ```
    pub fn clean(&mut self) {
        self.count_columns -= remove_empty_columns(&mut self.data, self.count_columns);
        remove_empty_rows(&mut self.data, self.count_columns);
    }

    /// Removes a row with a specific position.
    ///
    /// Index expected to be in range.
    /// `Builder::count_records() < x >= 0`
    ///
    /// # Panics
    ///
    /// Panics if `row_index > count_rows`.
    pub fn remove_record(&mut self, index: usize) {
        let _ = self.data.remove(index);
    }

    /// Removes a column with a specific position.
    ///
    /// Index expected to be in range.
    /// `Builder::count_columns() < x >= 0`
    ///
    /// # Panics
    ///
    /// Panics if `index > count_columns`.
    pub fn remove_column(&mut self, index: usize) {
        for row in &mut self.data {
            let _ = row.remove(index);
        }

        self.count_columns -= 1;
    }

    /// Push a column.
    pub fn push_column<I>(&mut self, column: I)
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        let mut iter = column.into_iter();

        for row in self.data.iter_mut() {
            let text = iter
                .next()
                .map(Into::into)
                .map(CellInfo::new)
                .unwrap_or(self.empty_text.clone());

            row.push(text);
        }

        for text in iter {
            let text = CellInfo::new(text.into());

            let mut row = Vec::with_capacity(self.count_columns + 1);
            for _ in 0..self.count_columns {
                row.push(self.empty_text.clone());
            }

            row.push(text);
        }

        self.count_columns += 1;
    }

    /// Insert a column with a specific position.
    ///
    /// In case a column is bigger then the total amount of rows it will be truncated.
    ///
    /// # Panics
    ///
    /// Panics if `index > count_columns`.
    pub fn insert_column<I>(&mut self, index: usize, column: I)
    where
        I: IntoIterator,
        I::Item: Into<String>,
    {
        let mut iter = column.into_iter();

        for row in self.data.iter_mut() {
            let text = iter
                .next()
                .map(Into::into)
                .map(CellInfo::new)
                .unwrap_or(self.empty_text.clone());

            row.insert(index, text);
        }

        for text in iter {
            let text = CellInfo::new(text.into());

            let mut row = Vec::with_capacity(self.count_columns + 1);
            for _ in 0..index {
                row.push(self.empty_text.clone());
            }

            row.push(text);

            for _ in index..self.count_columns {
                row.push(self.empty_text.clone());
            }
        }

        self.count_columns += 1;
    }

    /// Remove all records.
    pub fn clear(&mut self) {
        self.data.clear();
        self.count_columns = 0;
    }

    /// Returns an amount of columns which would be present in a built table.
    pub fn count_columns(&self) -> usize {
        self.count_columns
    }

    /// Returns an amount of rows which would be present in a built table.
    ///
    /// Notice that it does not include header if present;
    /// It returns only amount of records.
    pub fn count_records(&self) -> usize {
        self.data.len()
    }
}

impl From<Builder> for Vec<Vec<String>> {
    fn from(builder: Builder) -> Self {
        builder
            .data
            .into_iter()
            .map(|row| row.into_iter().map(CellInfo::into_inner).collect())
            .collect()
    }
}

impl From<Builder> for Vec<Vec<CellInfo<String>>> {
    fn from(builder: Builder) -> Self {
        builder.data
    }
}

impl<R> FromIterator<R> for Builder
where
    R: IntoIterator,
    R::Item: Into<String>,
{
    fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self {
        let mut builder = Self::new();
        for row in iter {
            builder.push_record(row);
        }

        builder
    }
}

impl<D> Extend<D> for Builder
where
    D: Into<String>,
{
    fn extend<T: IntoIterator<Item = D>>(&mut self, iter: T) {
        self.push_record(iter);
    }
}

impl From<Vec<Vec<String>>> for Builder {
    fn from(data: Vec<Vec<String>>) -> Self {
        let mut data = data
            .into_iter()
            .map(|row| row.into_iter().map(CellInfo::new).collect())
            .collect();

        let count_columns = equalize_row_length(&mut data);

        Self {
            data,
            count_columns,
            empty_text: CellInfo::default(),
        }
    }
}

impl From<Vec<Vec<CellInfo<String>>>> for Builder {
    fn from(mut data: Vec<Vec<CellInfo<String>>>) -> Self {
        let count_columns = equalize_row_length(&mut data);

        Self {
            data,
            count_columns,
            empty_text: CellInfo::default(),
        }
    }
}

fn create_row<R>(row: R, size: usize, default: &CellInfo<String>) -> Vec<CellInfo<String>>
where
    R: IntoIterator,
    R::Item: Into<String>,
{
    let mut list = Vec::with_capacity(size);
    for text in row {
        let text = text.into();
        let text = CellInfo::new(text);
        list.push(text);
    }

    if list.len() < size {
        for _ in 0..size - list.len() {
            let text = default.clone();
            list.push(text);
        }
    }

    list
}

fn remove_empty_columns(data: &mut [Vec<CellInfo<String>>], count_columns: usize) -> usize {
    let mut deleted = 0;
    for col in 0..count_columns {
        let col = col - deleted;

        let mut is_empty_column = true;
        for row in data.iter() {
            let text = &row[col];
            if !text.as_ref().is_empty() {
                is_empty_column = false;
                break;
            }
        }

        if is_empty_column {
            for row in data.iter_mut() {
                let _ = row.remove(col);
            }

            deleted += 1;
        }
    }

    deleted
}

fn remove_empty_rows(data: &mut Vec<Vec<CellInfo<String>>>, count_columns: usize) {
    let mut deleted = 0;

    for row in 0..data.len() {
        let row = row - deleted;

        let mut is_empty_row = true;
        for col in 0..count_columns {
            let cell = &data[row][col];
            if !cell.as_ref().is_empty() {
                is_empty_row = false;
                break;
            }
        }

        if is_empty_row {
            let _ = data.remove(row);
            deleted += 1;
        }
    }
}

fn resize_rows(data: &mut Vec<Vec<CellInfo<String>>>, size: usize, empty_text: &CellInfo<String>) {
    for row in data {
        append_vec(row, empty_text.clone(), size);
    }
}

fn append_vec<T>(v: &mut Vec<T>, value: T, n: usize)
where
    T: Clone,
{
    for _ in 0..n {
        v.push(value.clone());
    }
}

fn is_size_eq(expected: usize, new: usize) -> bool {
    use std::cmp::Ordering;

    match new.cmp(&expected) {
        Ordering::Less => {
            unreachable!("must be impossible due to the assumptions/checks we do");
        }
        Ordering::Greater => false,
        Ordering::Equal => true,
    }
}

fn equalize_row_length(data: &mut Vec<Vec<CellInfo<String>>>) -> usize {
    if data.is_empty() {
        return 0;
    }

    let first_row_length = data[0].len();
    let init = (first_row_length, true);
    let (count_columns, is_consistent) = data.iter().fold(init, |mut acc, cur| {
        let length = cur.len();
        acc.1 = acc.1 && acc.0 == length;
        acc.0 = std::cmp::max(acc.0, length);
        acc
    });

    if !is_consistent {
        let empty_text = CellInfo::default();
        for row in data {
            let size = count_columns - row.len();
            append_vec(row, empty_text.clone(), size);
        }
    }

    count_columns
}