aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/shared.rs
blob: 1d8ca5c34edf133a322de29d360d88cd64e5ad40 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::{iter, str::from_utf8};

use strip_ansi_escapes::strip;

pub fn ansi_len(s: &str) -> usize {
    from_utf8(&strip(s.as_bytes()).unwrap())
        .unwrap()
        .chars()
        .count()
}

pub fn pad_to_size(s: &str, rows: usize, columns: usize) -> String {
    s.lines()
        .map(|l| [l, &str::repeat(" ", columns - ansi_len(l))].concat())
        .chain(iter::repeat(str::repeat(" ", columns)))
        .take(rows)
        .collect::<Vec<_>>()
        .join("\n\r")
}