mod-cli 0.6.4

A fully customizable, feature-rich CLI framework for Rust. Define commands, prefixes, styled output, and more—built for flexibility and speed.
Documentation
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use console::measure_text_width;
use crossterm::style::{Color, Stylize};
use terminal_size::{terminal_size, Width};
use unicode_segmentation::UnicodeSegmentation;

#[derive(Clone, Copy)]
pub enum Align {
    Left,
    Center,
    Right,
}

#[cfg(feature = "table-presets")]
/// Preset sugar: heavy borders, cyan header, separators on by default.
pub fn render_table_preset_heavy_cyan_separators(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
    row_separators: bool,
) -> String {
    render_table_with_opts_styled(
        headers,
        rows,
        mode,
        TableStyle::Heavy,
        alignments,
        trunc_modes,
        true,
        row_separators,
        Some(crate::output::CYAN),
        Some(crate::output::DARK_BLUE),
    )
}

#[cfg(feature = "table-presets")]
/// Preset sugar: minimal ASCII borders, magenta header, light grey zebra rows.
pub fn render_table_preset_minimal_magenta_grey_zebra(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
    row_separators: bool,
) -> String {
    render_table_with_opts_styled(
        headers,
        rows,
        mode,
        TableStyle::Ascii,
        alignments,
        trunc_modes,
        true,
        row_separators,
        Some(crate::output::MAGENTA),
        Some(crate::output::LIGHT_GREY),
    )
}

/// Write Markdown table to a file path. Returns Result for error handling.
pub fn write_table_markdown(
    path: &str,
    headers: &[&str],
    rows: &[Vec<&str>],
) -> std::io::Result<()> {
    std::fs::write(path, render_table_markdown(headers, rows))
}

/// Write CSV table to a file path. Returns Result for error handling.
pub fn write_table_csv(path: &str, headers: &[&str], rows: &[Vec<&str>]) -> std::io::Result<()> {
    std::fs::write(path, render_table_csv(headers, rows))
}

/// Styled variant: allow optional header foreground color and zebra row background color.
#[allow(clippy::too_many_arguments)]
pub fn render_table_with_opts_styled(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    style: TableStyle,
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
    zebra: bool,
    row_separators: bool,
    header_fg: Option<Color>,
    zebra_bg: Option<Color>,
) -> String {
    let term_width = terminal_size()
        .map(|(Width(w), _)| w as usize)
        .unwrap_or(80);
    let col_count = headers.len().max(1);
    let padding: usize = 1;
    let total_padding = (col_count - 1) * padding;

    let col_width = match mode {
        TableMode::Fixed(width) => width,
        TableMode::Full => {
            let border_space = col_count + 1;
            let usable = term_width.saturating_sub(border_space);
            usable / col_count
        }
        TableMode::Flex => {
            let content_max = headers
                .iter()
                .map(|h| measure_text_width(h))
                .chain(
                    rows.iter()
                        .flat_map(|r| r.iter().map(|c| measure_text_width(c))),
                )
                .max()
                .unwrap_or(10);
            content_max.min((term_width.saturating_sub(total_padding)) / col_count)
        }
    };

    let border = match style {
        TableStyle::Ascii => BorderSet::ascii(),
        TableStyle::Rounded => BorderSet::rounded(),
        TableStyle::Heavy => BorderSet::heavy(),
    };

    let mut out = String::with_capacity(128);

    // Top Border
    out.push(border.top_left);
    for i in 0..col_count {
        out.push_str(&border.horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.top_cross);
        }
    }
    out.push(border.top_right);
    out.push('\n');

    // Header Row (with optional color)
    out.push(border.vertical);
    for h in headers.iter() {
        let a = pick_align(0, alignments);
        let t = pick_trunc(0, trunc_modes);
        let mut cell = pad_cell_with(h, col_width, a, t);
        if let Some(color) = header_fg {
            cell = cell.with(color).bold().to_string();
        }
        out.push_str(&cell);
        out.push(border.vertical);
    }
    out.push('\n');

    // Mid Border
    out.push(border.mid_left);
    for i in 0..col_count {
        out.push_str(&border.inner_horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.mid_cross);
        }
    }
    out.push(border.mid_right);
    out.push('\n');

    // Body Rows (optional zebra bg)
    for (ri, row) in rows.iter().enumerate() {
        out.push(border.vertical);
        for (ci, cell) in row.iter().enumerate() {
            let a = pick_align(ci, alignments);
            let t = pick_trunc(ci, trunc_modes);
            let base = pad_cell_with(cell, col_width, a, t);
            let styled = if zebra && (ri % 2 == 1) {
                if let Some(bg) = zebra_bg {
                    base.on(bg).to_string()
                } else {
                    base
                }
            } else {
                base
            };
            out.push_str(&styled);
            out.push(border.vertical);
        }
        out.push('\n');

        if row_separators && ri < rows.len() - 1 {
            out.push(border.mid_left);
            for i in 0..col_count {
                out.push_str(&border.inner_horizontal.to_string().repeat(col_width));
                if i < col_count - 1 {
                    out.push(border.mid_cross);
                }
            }
            out.push(border.mid_right);
            out.push('\n');
        }
    }

    // Bottom Border
    out.push(border.bottom_left);
    for i in 0..col_count {
        out.push_str(&border.horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.bottom_cross);
        }
    }
    out.push(border.bottom_right);
    out.push('\n');

    out
}

// --- Export adapters ---

/// Render as GitHub-flavored Markdown table.
pub fn render_table_markdown(headers: &[&str], rows: &[Vec<&str>]) -> String {
    let mut out = String::new();
    // header
    out.push('|');
    for h in headers {
        out.push(' ');
        out.push_str(&escape_md(h));
        out.push(' ');
        out.push('|');
    }
    out.push('\n');
    // separator
    out.push('|');
    for _ in headers {
        out.push_str(" --- |");
    }
    out.push('\n');
    // rows
    for row in rows {
        out.push('|');
        for cell in row {
            out.push(' ');
            out.push_str(&escape_md(cell));
            out.push(' ');
            out.push('|');
        }
        out.push('\n');
    }
    out
}

/// Render as CSV. Minimal escaping for quotes and commas/newlines.
pub fn render_table_csv(headers: &[&str], rows: &[Vec<&str>]) -> String {
    let mut out = String::new();
    out.push_str(&join_csv(headers.iter().copied()));
    out.push('\n');
    for row in rows {
        out.push_str(&join_csv(row.iter().copied()));
        out.push('\n');
    }
    out
}

/// Render as JSON array of objects mapping header->value. Not streaming, small tables only.
pub fn render_table_json(headers: &[&str], rows: &[Vec<&str>]) -> String {
    use std::fmt::Write as _;
    let mut out = String::from("[");
    for (ri, row) in rows.iter().enumerate() {
        if ri > 0 {
            out.push(',');
        }
        out.push('{');
        for (ci, h) in headers.iter().enumerate() {
            if ci > 0 {
                out.push(',');
            }
            let _ = write!(
                out,
                "\"{}\":{}",
                escape_json(h),
                json_string(row.get(ci).copied().unwrap_or(""))
            );
        }
        out.push('}');
    }
    out.push(']');
    out
}

fn escape_md(s: &str) -> String {
    s.replace('|', "\\|")
}

fn join_csv<'a, I: IntoIterator<Item = &'a str>>(iter: I) -> String {
    let mut first = true;
    let mut s = String::new();
    for field in iter {
        if !first {
            s.push(',');
        } else {
            first = false;
        }
        s.push_str(&csv_field(field));
    }
    s
}

fn csv_field(s: &str) -> String {
    let need_quotes = s.contains(',') || s.contains('"') || s.contains('\n');
    if need_quotes {
        let escaped = s.replace('"', "\"\"");
        format!("\"{escaped}\"")
    } else {
        s.to_string()
    }
}

fn escape_json(s: &str) -> String {
    s.replace('"', "\\\"")
}
fn json_string(s: &str) -> String {
    format!("\"{}\"", escape_json(s))
}

#[derive(Clone, Copy)]
pub enum TruncateMode {
    End,
    Middle,
    Start,
}

pub enum TableMode {
    Flex,
    Fixed(usize),
    Full,
}

pub enum TableStyle {
    Ascii,
    Rounded,
    Heavy,
}

#[cfg(feature = "table-presets")]
impl TableStyle {
    #[inline]
    pub fn ascii_preset() -> Self {
        TableStyle::Ascii
    }
    #[inline]
    pub fn rounded_preset() -> Self {
        TableStyle::Rounded
    }
    #[inline]
    pub fn heavy_preset() -> Self {
        TableStyle::Heavy
    }
}

pub fn render_table(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    style: TableStyle,
) -> String {
    render_table_with(headers, rows, mode, style, None, None)
}

/// Advanced renderer allowing per-column alignment and truncation modes.
pub fn render_table_with(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    style: TableStyle,
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
) -> String {
    render_table_with_opts(
        headers,
        rows,
        mode,
        style,
        alignments,
        trunc_modes,
        false,
        false,
    )
}

/// Advanced renderer with options: per-column alignment/truncation, zebra stripes, and row separators.
#[allow(clippy::too_many_arguments)]
pub fn render_table_with_opts(
    headers: &[&str],
    rows: &[Vec<&str>],
    mode: TableMode,
    style: TableStyle,
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
    zebra: bool,
    row_separators: bool,
) -> String {
    let term_width = terminal_size()
        .map(|(Width(w), _)| w as usize)
        .unwrap_or(80);
    let col_count = headers.len().max(1);
    let padding: usize = 1;
    let total_padding = (col_count - 1) * padding;

    let col_width = match mode {
        TableMode::Fixed(width) => width,
        TableMode::Full => {
            let border_space = col_count + 1; // ┏┃┃┃┓ = 4 columns + 2 sides = 5 chars
            let usable = term_width.saturating_sub(border_space);
            usable / col_count
        }
        TableMode::Flex => {
            let content_max = headers
                .iter()
                .map(|h| measure_text_width(h))
                .chain(
                    rows.iter()
                        .flat_map(|r| r.iter().map(|c| measure_text_width(c))),
                )
                .max()
                .unwrap_or(10);
            content_max.min((term_width.saturating_sub(total_padding)) / col_count)
        }
    };

    let border = match style {
        TableStyle::Ascii => BorderSet::ascii(),
        TableStyle::Rounded => BorderSet::rounded(),
        TableStyle::Heavy => BorderSet::heavy(),
    };

    let mut out = String::with_capacity(128);

    // Top Border
    out.push(border.top_left);
    for i in 0..col_count {
        out.push_str(&border.horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.top_cross);
        }
    }
    out.push(border.top_right);
    out.push('\n');

    // Header Row
    out.push(border.vertical);
    for h in headers.iter() {
        let a = pick_align(0, alignments);
        let t = pick_trunc(0, trunc_modes);
        out.push_str(&pad_cell_with(h, col_width, a, t));
        out.push(border.vertical);
    }
    out.push('\n');

    // Mid Border
    out.push(border.mid_left);
    for i in 0..col_count {
        out.push_str(&border.inner_horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.mid_cross);
        }
    }
    out.push(border.mid_right);
    out.push('\n');

    // Body Rows
    for (ri, row) in rows.iter().enumerate() {
        out.push(border.vertical);
        for (ci, cell) in row.iter().enumerate() {
            let a = pick_align(ci, alignments);
            let t = pick_trunc(ci, trunc_modes);
            let mut cell_s = pad_cell_with(cell, col_width, a, t);
            if zebra && (ri % 2 == 1) {
                // lightweight zebra: replace spaces in padding with middle dot for visibility
                // keeps width identical
                cell_s = cell_s.replace(' ', "·");
            }
            out.push_str(&cell_s);
            out.push(border.vertical);
        }
        out.push('\n');

        if row_separators && ri < rows.len() - 1 {
            // Inner separator line between rows
            out.push(border.mid_left);
            for i in 0..col_count {
                out.push_str(&border.inner_horizontal.to_string().repeat(col_width));
                if i < col_count - 1 {
                    out.push(border.mid_cross);
                }
            }
            out.push(border.mid_right);
            out.push('\n');
        }
    }

    // Bottom Border
    out.push(border.bottom_left);
    for i in 0..col_count {
        out.push_str(&border.horizontal.to_string().repeat(col_width));
        if i < col_count - 1 {
            out.push(border.bottom_cross);
        }
    }
    out.push(border.bottom_right);
    out.push('\n');

    out
}

/// Column-specific width specification.
#[derive(Clone, Copy)]
pub enum ColWidth {
    Fixed(usize),
    Percent(u16),
    Auto,
}

/// Render with explicit per-column widths.
#[allow(clippy::too_many_arguments)]
pub fn render_table_with_columns(
    headers: &[&str],
    rows: &[Vec<&str>],
    style: TableStyle,
    columns: &[ColWidth],
    alignments: Option<&[Align]>,
    trunc_modes: Option<&[TruncateMode]>,
    zebra: bool,
    row_separators: bool,
) -> String {
    let term_width = terminal_size()
        .map(|(Width(w), _)| w as usize)
        .unwrap_or(80);
    let col_count = headers.len().max(1);
    let padding: usize = 1;
    let gaps_total = padding.saturating_mul(col_count.saturating_sub(1));

    // Compute column widths
    let mut widths = vec![0usize; col_count];
    let mut fixed_total = 0usize;
    let mut pct_total = 0u16;
    let mut auto_count = 0usize;
    for (i, spec) in columns.iter().enumerate().take(col_count) {
        match spec {
            ColWidth::Fixed(w) => {
                widths[i] = *w;
                fixed_total = fixed_total.saturating_add(*w);
            }
            ColWidth::Percent(p) => {
                pct_total = pct_total.saturating_add(*p);
            }
            ColWidth::Auto => {
                auto_count += 1;
            }
        }
    }

    let base_rem = term_width.saturating_sub(fixed_total + gaps_total);
    // Assign percent columns proportional to base_rem
    for (i, spec) in columns.iter().enumerate().take(col_count) {
        if let ColWidth::Percent(p) = spec {
            let w = ((base_rem as u128) * (*p as u128) / 100u128) as usize;
            widths[i] = w;
        }
    }
    // Remaining space goes to autos evenly
    let used_except_auto: usize = widths.iter().sum();
    let remaining = term_width.saturating_sub(used_except_auto + gaps_total);
    let auto_share = if auto_count > 0 {
        remaining / auto_count
    } else {
        0
    };
    for (i, spec) in columns.iter().enumerate().take(col_count) {
        if matches!(spec, ColWidth::Auto) {
            widths[i] = auto_share;
        }
    }

    let border = match style {
        TableStyle::Ascii => BorderSet::ascii(),
        TableStyle::Rounded => BorderSet::rounded(),
        TableStyle::Heavy => BorderSet::heavy(),
    };
    let mut out = String::with_capacity(128);

    // Top
    out.push(border.top_left);
    for (i, w) in widths.iter().enumerate() {
        out.push_str(&border.horizontal.to_string().repeat(*w));
        if i < widths.len() - 1 {
            out.push(border.top_cross);
        }
    }
    out.push(border.top_right);
    out.push('\n');

    // Header
    out.push(border.vertical);
    for (ci, h) in headers.iter().enumerate() {
        let a = pick_align(ci, alignments);
        let t = pick_trunc(ci, trunc_modes);
        out.push_str(&pad_cell_with(h, widths[ci].max(1), a, t));
        out.push(border.vertical);
    }
    out.push('\n');

    // Mid
    out.push(border.mid_left);
    for (i, w) in widths.iter().enumerate() {
        out.push_str(&border.inner_horizontal.to_string().repeat(*w));
        if i < widths.len() - 1 {
            out.push(border.mid_cross);
        }
    }
    out.push(border.mid_right);
    out.push('\n');

    // Rows
    for (ri, row) in rows.iter().enumerate() {
        out.push(border.vertical);
        for (ci, cell) in row.iter().enumerate() {
            let a = pick_align(ci, alignments);
            let t = pick_trunc(ci, trunc_modes);
            let mut cell_s = pad_cell_with(cell, widths[ci].max(1), a, t);
            if zebra && (ri % 2 == 1) {
                cell_s = cell_s.replace(' ', "·");
            }
            out.push_str(&cell_s);
            out.push(border.vertical);
        }
        out.push('\n');

        if row_separators && ri < rows.len() - 1 {
            out.push(border.mid_left);
            for (i, w) in widths.iter().enumerate() {
                out.push_str(&border.inner_horizontal.to_string().repeat(*w));
                if i < widths.len() - 1 {
                    out.push(border.mid_cross);
                }
            }
            out.push(border.mid_right);
            out.push('\n');
        }
    }

    // Bottom
    out.push(border.bottom_left);
    for (i, w) in widths.iter().enumerate() {
        out.push_str(&border.horizontal.to_string().repeat(*w));
        if i < widths.len() - 1 {
            out.push(border.bottom_cross);
        }
    }
    out.push(border.bottom_right);
    out.push('\n');

    out
}

/// Helper to pick alignment for a given column index with fallback.
fn pick_align(idx: usize, aligns: Option<&[Align]>) -> Align {
    aligns
        .and_then(|arr| arr.get(idx).copied())
        .unwrap_or(Align::Left)
}

/// Helper to pick truncate mode for a given column index with fallback.
fn pick_trunc(idx: usize, truncs: Option<&[TruncateMode]>) -> TruncateMode {
    truncs
        .and_then(|arr| arr.get(idx).copied())
        .unwrap_or(TruncateMode::End)
}

/// Truncates the cell to fit `width` characters visually, then pads according to `align`.
fn pad_cell_with(cell: &str, width: usize, align: Align, trunc: TruncateMode) -> String {
    let truncated = truncate_to_width_mode(cell, width, trunc);
    let visual = measure_text_width(&truncated);
    let pad = width.saturating_sub(visual);
    match align {
        Align::Left => format!("{truncated}{}", " ".repeat(pad)),
        Align::Right => format!("{}{truncated}", " ".repeat(pad)),
        Align::Center => {
            let left = pad / 2;
            let right = pad - left;
            format!("{}{}{}", " ".repeat(left), truncated, " ".repeat(right))
        }
    }
}

/// Best-effort truncate that respects visual width using `console::measure_text_width`.
/// If the content exceeds `width`, it trims to `width-1` and appends '…'.
fn truncate_to_width_mode(cell: &str, width: usize, mode: TruncateMode) -> String {
    if width == 0 {
        return String::new();
    }
    let visual = measure_text_width(cell);
    if visual <= width {
        return cell.to_string();
    }

    // Reserve room for ellipsis
    let target = width.saturating_sub(1);
    // Work with grapheme clusters to avoid splitting emojis or accents
    let g = UnicodeSegmentation::graphemes(cell, true).collect::<Vec<&str>>();
    match mode {
        TruncateMode::End => {
            let mut out = String::new();
            for gr in &g {
                let next = format!("{out}{gr}");
                if measure_text_width(&next) > target {
                    break;
                }
                out.push_str(gr);
            }
            out.push('');
            out
        }
        TruncateMode::Start => {
            let mut tail_rev: Vec<&str> = Vec::new();
            for gr in g.iter().rev() {
                let candidate = tail_rev
                    .iter()
                    .cloned()
                    .rev()
                    .chain(std::iter::once(*gr))
                    .collect::<String>();
                if measure_text_width(&candidate) > target {
                    break;
                }
                tail_rev.push(gr);
            }
            let tail: String = tail_rev.into_iter().rev().collect();
            format!("{tail}")
        }
        TruncateMode::Middle => {
            let mut head = String::new();
            let mut tail_rev: Vec<&str> = Vec::new();
            let mut left_i = 0usize;
            let mut right_i = g.len();
            loop {
                let current = format!(
                    "{head}{}",
                    tail_rev.iter().rev().cloned().collect::<String>()
                );
                if measure_text_width(&current) > width {
                    break;
                }
                // Try extend head first
                if left_i < right_i {
                    let next = format!("{head}{}", g[left_i]);
                    let cur2 = format!(
                        "{next}{}",
                        tail_rev.iter().rev().cloned().collect::<String>()
                    );
                    if measure_text_width(&cur2) <= width {
                        head.push_str(g[left_i]);
                        left_i += 1;
                        continue;
                    }
                }
                // Then try extend tail
                if right_i > left_i {
                    let cand_tail = {
                        let mut tmp = tail_rev.clone();
                        if right_i > 0 {
                            tmp.push(g[right_i - 1]);
                        }
                        tmp
                    };
                    let cur2 = format!(
                        "{head}{}",
                        cand_tail.iter().rev().cloned().collect::<String>()
                    );
                    if measure_text_width(&cur2) <= width {
                        tail_rev.push(g[right_i - 1]);
                        right_i -= 1;
                        continue;
                    }
                }
                break;
            }
            format!(
                "{head}{}",
                tail_rev.iter().rev().cloned().collect::<String>()
            )
        }
    }
}

struct BorderSet {
    top_left: char,
    top_right: char,
    bottom_left: char,
    bottom_right: char,
    top_cross: char,
    bottom_cross: char,
    mid_cross: char,
    mid_left: char,
    mid_right: char,
    horizontal: char,
    inner_horizontal: char,
    vertical: char,
}

impl BorderSet {
    fn ascii() -> Self {
        Self {
            top_left: '+',
            top_right: '+',
            bottom_left: '+',
            bottom_right: '+',
            top_cross: '+',
            bottom_cross: '+',
            mid_cross: '+',
            mid_left: '+',
            mid_right: '+',
            horizontal: '-',
            inner_horizontal: '-',
            vertical: '|',
        }
    }

    fn rounded() -> Self {
        Self {
            top_left: '',
            top_right: '',
            bottom_left: '',
            bottom_right: '',
            top_cross: '',
            bottom_cross: '',
            mid_cross: '',
            mid_left: '',
            mid_right: '',
            horizontal: '',
            inner_horizontal: '',
            vertical: '',
        }
    }

    fn heavy() -> Self {
        Self {
            top_left: '',
            top_right: '',
            bottom_left: '',
            bottom_right: '',
            top_cross: '',
            bottom_cross: '',
            mid_cross: '',
            mid_left: '',
            mid_right: '',
            horizontal: '',
            inner_horizontal: '',
            vertical: '',
        }
    }
}