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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use crate::{
    cell::{Cell, HAlign, VAlign, DEFAULT_BLANK_CHAR, DEFAULT_H_ALIGN, DEFAULT_V_ALIGN},
    options::Options,
};

use std::borrow::Cow;

/// Data type for creating a [`Row`] for the grid.
///
/// [`Row`]: struct.Row.html
pub struct Row {
    /// These options will be used if the equivalent is not provided
    /// by the underlying [`Cell`] type.
    ///
    /// [`Cell`]: struct.Cell.html
    pub default_options: Options,

    /// Width in chars for each column of the [`Row`].
    ///
    /// [`Row`]: struct.Row.html
    pub column_width: Option<usize>,

    /// Number of char spaces for each padding space between row columns.
    pub padding_size: Option<usize>,

    /// Collection of cells that this [`Row`] contains.
    ///
    /// [`Row`]: struct.Row.html
    pub cells: Vec<Cell>,
}

impl Row {
    /// Creates a new [`Row`] by its cells.
    ///
    /// [`Row`]: struct.Row.html
    pub fn new(cells: Vec<Cell>) -> Self {
        Self {
            default_options: Options {
                col_span: None,
                h_align: None,
                v_align: None,
                blank_char: None,
            },
            column_width: None,
            padding_size: None,
            cells,
        }
    }

    /// Create a new empty row with a specific column span.
    pub fn new_empty(col_span: usize) -> Self {
        Row::new(vec![Cell::new_empty(col_span)])
    }

    /// Create a new row with a specified column span filled by the repeated content.
    pub fn new_fill(content: String, col_span: usize) -> Self {
        Row::new(vec![Cell::new_fill(content, col_span)])
    }

    /// Creates a [`RowBuilder`] initiated with cells.
    /// To build the final [`RowBuilder`] call the [`build`] method.
    ///
    /// [`RowBuilder`]: struct.RowBuilder.html
    /// [`build`]: struct.RowBuilder.html#method.build
    pub fn builder(cells: Vec<Cell>) -> RowBuilder {
        RowBuilder {
            inner: Self::new(cells),
        }
    }

    /// Formats the [`Row`] into a string.
    ///
    /// [`Row`]: struct.Row.html
    pub fn render(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        default_options: &Options,
        column_width: Option<usize>,
        padding_size: Option<usize>,
    ) -> std::fmt::Result {
        let column_width = column_width.or(self.column_width).unwrap_or(1);
        let padding_size = padding_size.or(self.padding_size).unwrap_or(1);
        let mut cols_lines = self
            .cells
            .iter()
            .map(|c| {
                let mut lines = c.content.lines().map(|l| l.to_owned()).collect::<Vec<_>>();
                if lines.is_empty() {
                    lines.push(String::new());
                }
                lines
            })
            .collect::<Vec<_>>();
        let max_lines = cols_lines
            .iter()
            .map(|col_lines| col_lines.len())
            .max()
            .unwrap_or(0);
        for line_index in 0..max_lines {
            for (col_index, col) in self.cells.iter().enumerate() {
                let col_lines = &mut cols_lines[col_index];
                let col_span = col
                    .col_span
                    .or(self.default_options.col_span)
                    .or(default_options.col_span)
                    .unwrap_or(1);
                let col_width = col_span * column_width + padding_size * (col_span - 1);
                let h_align = col
                    .h_align
                    .or(self.default_options.h_align)
                    .or(default_options.h_align)
                    .unwrap_or(DEFAULT_H_ALIGN);
                let v_align = col
                    .v_align
                    .or(self.default_options.v_align)
                    .or(default_options.v_align)
                    .unwrap_or(DEFAULT_V_ALIGN);
                let blank_char = col
                    .blank_char
                    .or(self.default_options.blank_char)
                    .or(default_options.blank_char)
                    .unwrap_or(DEFAULT_BLANK_CHAR);
                if col_index != 0 {
                    write!(f, "{s:<0$}", padding_size, s = "")?;
                }
                write!(
                    f,
                    "{}",
                    col_line(
                        h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
                    )
                )?;
            }
            writeln!(f)?;
        }
        Ok(())
    }
}

impl std::fmt::Display for Row {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.render(
            f,
            &self.default_options,
            self.column_width,
            self.padding_size,
        )
    }
}

fn col_line(
    h_align: HAlign,
    v_align: VAlign,
    col_width: usize,
    col_lines: &mut [String],
    max_lines: usize,
    line_index: usize,
    blank_char: char,
) -> Cow<str> {
    let index = match v_align {
        VAlign::Top => {
            let start_line_index = 0;
            let end_line_index = col_lines.len() - 1;
            if line_index >= start_line_index && line_index <= end_line_index {
                Some(line_index)
            } else {
                None
            }
        }
        VAlign::Bottom => {
            let start_line_index = max_lines - col_lines.len();
            let end_line_index = max_lines - 1;
            if line_index >= start_line_index && line_index <= end_line_index {
                Some(line_index - start_line_index)
            } else {
                None
            }
        }
        VAlign::Middle => {
            let start_line_index = (max_lines - col_lines.len()) / 2;
            let end_line_index = start_line_index + col_lines.len() - 1;
            if line_index >= start_line_index && line_index <= end_line_index {
                Some(line_index - start_line_index)
            } else {
                None
            }
        }
    };
    if let Some(i) = index {
        return pad(h_align, &mut col_lines[i], col_width, blank_char);
    }
    Cow::Owned(blank_char.to_string().repeat(col_width))
}

fn pad(h_align: HAlign, s: &mut String, width: usize, blank_char: char) -> Cow<str> {
    let s_chars_len = s.chars().count();
    if s_chars_len >= width {
        let bytes_index = byte_index(s, width);
        return s[..bytes_index].into();
    }
    let blanks = width - s_chars_len;
    match h_align {
        HAlign::Left => {
            s.extend(std::iter::repeat(blank_char).take(blanks));
            s.as_str().into()
        }
        HAlign::Right => {
            let mut new_str = std::iter::repeat(blank_char)
                .take(blanks)
                .collect::<String>();
            new_str.push_str(s);
            new_str.into()
        }
        HAlign::Center => {
            let left_blanks = blanks / 2;
            let right_blanks = blanks - left_blanks;
            let mut new_str = std::iter::repeat(blank_char)
                .take(left_blanks)
                .collect::<String>();
            new_str.push_str(s);
            new_str.extend(std::iter::repeat(blank_char).take(right_blanks));
            new_str.into()
        }
        HAlign::Fill => {
            let repeats = width / s_chars_len + 1;
            let s = s.repeat(repeats);
            let bytes_index = byte_index(&s, width);
            s[..bytes_index].to_owned().into()
        }
    }
}

fn byte_index(s: &str, char_index: usize) -> usize {
    s.char_indices()
        .take(char_index)
        .last()
        .map_or(0, |(i, ch)| i + ch.len_utf8())
}

/// Builder for the [`Row`] type.
///
/// [`Row`]: struct.Row.html
pub struct RowBuilder {
    inner: Row,
}

impl RowBuilder {
    /// Builds a [`Row`] from a [`RowBuilder`].
    ///
    /// [`Row`]: struct.Row.html
    /// [`RowBuilder`]: struct.RowBuilder.html
    pub fn build(self) -> Row {
        self.inner
    }

    /// Sets the default column span for all the cells of the grid. If a cell specifies
    /// a column span it will be used instead of the grids default value.
    pub fn default_colspan(mut self, default_col_span: usize) -> Self {
        self.inner.default_options.col_span = Some(default_col_span);
        self
    }

    /// Sets the default horizontal alignment for all the cells of the grid. If a cell specifies
    /// a horizontal alignment it will be used instead of the grids default value.
    pub fn default_h_align(mut self, default_h_align: HAlign) -> Self {
        self.inner.default_options.h_align = Some(default_h_align);
        self
    }

    /// Sets the default vertical alignment for all the cells of the grid. If a cell specifies
    /// a vertical alignment it will be used instead of the grids default value.
    pub fn default_v_align(mut self, default_v_align: VAlign) -> Self {
        self.inner.default_options.v_align = Some(default_v_align);
        self
    }

    /// Sets the default blank char for all the cells of the grid. If a cell specifies
    /// a blank char it will be used instead of the grids default value.
    pub fn default_blank_char(mut self, default_blank_char: char) -> Self {
        self.inner.default_options.blank_char = Some(default_blank_char);
        self
    }

    /// Sets the width of each column in the [`Row`].
    ///
    /// [`Row`]: struct.Row.html
    pub fn column_width(mut self, column_width: usize) -> Self {
        self.inner.column_width = Some(column_width);
        self
    }

    /// Sets the padding size between each column in the [`Row`].
    ///
    /// [`Row`]: struct.Row.html
    pub fn padding_size(mut self, padding_size: usize) -> Self {
        self.inner.padding_size = Some(padding_size);
        self
    }

    /// Sets the cells collection of the [`Row`].
    ///
    /// [`Row`]: struct.Row.html
    pub fn cells(mut self, cells: Vec<Cell>) -> Self {
        self.inner.cells = cells;
        self
    }
}

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

    #[test]
    fn test_byte_index_ascii() {
        let s = "abc";
        let result = byte_index(s, 2);
        let expected = 2;
        assert_eq!(result, expected);
    }

    #[test]
    fn test_byte_index_unicode1() {
        let s = "aµc";
        let result = byte_index(s, 2);
        let expected = 3;
        assert_eq!(result, expected);
    }

    #[test]
    fn test_byte_index_unicode2() {
        let s = "µ∆c";
        let result = byte_index(s, 2);
        let expected = 5;
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_left_empty() {
        let s = &mut "".into();
        let result = pad(HAlign::Right, s, 3, '.');
        let expected = String::from("...");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_right_empty() {
        let s = &mut "".into();
        let result = pad(HAlign::Left, s, 3, '.');
        let expected = String::from("...");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_left() {
        let s = &mut "a".into();
        let result = pad(HAlign::Right, s, 3, '.');
        let expected = String::from("..a");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_right() {
        let s = &mut "a".into();
        let result = pad(HAlign::Left, s, 3, '.');
        let expected = String::from("a..");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_center() {
        let s = &mut "a".into();
        let result = pad(HAlign::Center, s, 3, '.');
        let expected = String::from(".a.");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_fill1() {
        let s = &mut "a".into();
        let result = pad(HAlign::Fill, s, 3, '.');
        let expected = String::from("aaa");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_fill2() {
        let s = &mut "ab".into();
        let result = pad(HAlign::Fill, s, 3, '.');
        let expected = String::from("aba");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_left_unicode() {
        let s = &mut "∆".into();
        let result = pad(HAlign::Right, s, 3, '.');
        let expected = String::from("..∆");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_right_unicode() {
        let s = &mut "∆".into();
        let result = pad(HAlign::Left, s, 3, '.');
        let expected = String::from("∆..");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_center_unicode() {
        let s = &mut "∆".into();
        let result = pad(HAlign::Center, s, 3, '.');
        let expected = String::from(".∆.");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_pad_fill_unicode() {
        let s = &mut "∆".into();
        let result = pad(HAlign::Fill, s, 3, '.');
        let expected = String::from("∆∆∆");
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_1_row_in_a_3_lines_column_left_top() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Top;
        let col_width = 3;
        let col_lines = &mut [String::from("a")];
        let max_lines = 3;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_1_row_in_a_3_lines_column_left_middle() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Middle;
        let col_width = 3;
        let col_lines = &mut [String::from("a")];
        let max_lines = 3;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_1_row_in_a_3_lines_column_left_bottom() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Bottom;
        let col_width = 3;
        let col_lines = &mut [String::from("a")];
        let max_lines = 3;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_2_row_in_a_4_lines_column_left_top() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Top;
        let col_width = 3;
        let col_lines = &mut [String::from("a"), String::from("b")];
        let max_lines = 4;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "b..";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 3;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_2_row_in_a_4_lines_column_left_middle() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Middle;
        let col_width = 3;
        let col_lines = &mut [String::from("a"), String::from("b")];
        let max_lines = 4;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "b..";
        assert_eq!(result, expected);

        let line_index = 3;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_col_line_for_a_content_with_2_row_in_a_4_lines_column_left_bottom() {
        let h_align = HAlign::Left;
        let v_align = VAlign::Bottom;
        let col_width = 3;
        let col_lines = &mut [String::from("a"), String::from("b")];
        let max_lines = 4;
        let blank_char = '.';

        let line_index = 0;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 1;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "...";
        assert_eq!(result, expected);

        let line_index = 2;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "a..";
        assert_eq!(result, expected);

        let line_index = 3;
        let result = col_line(
            h_align, v_align, col_width, col_lines, max_lines, line_index, blank_char,
        );
        let expected = "b..";
        assert_eq!(result, expected);
    }
}