aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/utils/shared.rs
blob: 9717837a0fdfae61deb96e6ab0a986f9b3a8d362 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//! Some general utility functions.

use std::{iter, str::from_utf8};

use strip_ansi_escapes::strip;

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")
}