Coverage Report

Created: 2025-11-24 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/nu-ansi-term-0.49.0/src/util.rs
Line
Count
Source
1
use crate::display::{AnsiString, AnsiStrings};
2
use std::ops::Deref;
3
4
/// Return a substring of the given AnsiStrings sequence, while keeping the formatting.
5
0
pub fn sub_string(start: usize, len: usize, strs: &AnsiStrings) -> Vec<AnsiString<'static>> {
6
0
    let mut vec = Vec::new();
7
0
    let mut pos = start;
8
0
    let mut len_rem = len;
9
10
0
    for i in strs.0.iter() {
11
0
        let frag_len = i.string.len();
12
0
        if pos >= frag_len {
13
0
            pos -= frag_len;
14
0
            continue;
15
0
        }
16
0
        if len_rem == 0 {
17
0
            break;
18
0
        }
19
20
0
        let end = pos + len_rem;
21
0
        let pos_end = if end >= frag_len { frag_len } else { end };
22
23
0
        vec.push(i.style_ref().paint(String::from(&i.string[pos..pos_end])));
24
25
0
        if end <= frag_len {
26
0
            break;
27
0
        }
28
29
0
        len_rem -= pos_end - pos;
30
0
        pos = 0;
31
    }
32
33
0
    vec
34
0
}
35
36
/// Return a concatenated copy of `strs` without the formatting, as an allocated `String`.
37
0
pub fn unstyle(strs: &AnsiStrings) -> String {
38
0
    let mut s = String::new();
39
40
0
    for i in strs.0.iter() {
41
0
        s += i.string.deref();
42
0
    }
43
44
0
    s
45
0
}
46
47
/// Return the unstyled length of AnsiStrings. This is equaivalent to `unstyle(strs).len()`.
48
0
pub fn unstyled_len(strs: &AnsiStrings) -> usize {
49
0
    let mut l = 0;
50
0
    for i in strs.0.iter() {
51
0
        l += i.string.len();
52
0
    }
53
0
    l
54
0
}
55
56
#[cfg(test)]
57
mod test {
58
    use super::*;
59
    use crate::Color::*;
60
61
    #[test]
62
    fn test() {
63
        let l = [
64
            Black.paint("first"),
65
            Red.paint("-second"),
66
            White.paint("-third"),
67
        ];
68
        let a = AnsiStrings(&l);
69
        assert_eq!(unstyle(&a), "first-second-third");
70
        assert_eq!(unstyled_len(&a), 18);
71
72
        let l2 = [Black.paint("st"), Red.paint("-second"), White.paint("-t")];
73
        assert_eq!(sub_string(3, 11, &a), l2);
74
    }
75
}