diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 09:26:03 +0000 |
commit | 9918693037dce8aa4bb6f08741b6812923486c18 (patch) | |
tree | 21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/nu-ansi-term/src/util.rs | |
parent | Releasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff) | |
download | rustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip |
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/nu-ansi-term/src/util.rs')
-rw-r--r-- | vendor/nu-ansi-term/src/util.rs | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/vendor/nu-ansi-term/src/util.rs b/vendor/nu-ansi-term/src/util.rs deleted file mode 100644 index a35020137..000000000 --- a/vendor/nu-ansi-term/src/util.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::display::{AnsiString, AnsiStrings}; -use std::ops::Deref; - -/// Return a substring of the given AnsiStrings sequence, while keeping the formatting. -pub fn sub_string<'a>( - start: usize, - len: usize, - strs: &AnsiStrings<'a>, -) -> Vec<AnsiString<'static>> { - let mut vec = Vec::new(); - let mut pos = start; - let mut len_rem = len; - - for i in strs.0.iter() { - let frag_len = i.string.len(); - if pos >= frag_len { - pos -= frag_len; - continue; - } - if len_rem == 0 { - break; - } - - let end = pos + len_rem; - let pos_end = if end >= frag_len { frag_len } else { end }; - - vec.push(i.style_ref().paint(String::from(&i.string[pos..pos_end]))); - - if end <= frag_len { - break; - } - - len_rem -= pos_end - pos; - pos = 0; - } - - vec -} - -/// Return a concatenated copy of `strs` without the formatting, as an allocated `String`. -pub fn unstyle(strs: &AnsiStrings) -> String { - let mut s = String::new(); - - for i in strs.0.iter() { - s += i.string.deref(); - } - - s -} - -/// Return the unstyled length of AnsiStrings. This is equaivalent to `unstyle(strs).len()`. -pub fn unstyled_len(strs: &AnsiStrings) -> usize { - let mut l = 0; - for i in strs.0.iter() { - l += i.string.len(); - } - l -} - -#[cfg(test)] -mod test { - use super::*; - use crate::Color::*; - - #[test] - fn test() { - let l = [ - Black.paint("first"), - Red.paint("-second"), - White.paint("-third"), - ]; - let a = AnsiStrings(&l); - assert_eq!(unstyle(&a), "first-second-third"); - assert_eq!(unstyled_len(&a), 18); - - let l2 = [Black.paint("st"), Red.paint("-second"), White.paint("-t")]; - assert_eq!(sub_string(3, 11, &a), l2); - } -} |