Add helper for splitting a long line into multiple lines.
This commit is contained in:
parent
961e323c36
commit
c1b8040ae2
|
@ -1,3 +1,5 @@
|
|||
use std::cmp;
|
||||
|
||||
pub fn ansi_len(s: &str) -> usize {
|
||||
String::from_utf8(strip_ansi_escapes::strip(s).unwrap())
|
||||
.unwrap()
|
||||
|
@ -120,3 +122,22 @@ pub fn style_if(styled: bool, s: String, apply_style: fn(String) -> String) -> S
|
|||
s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn multiline(max_len: usize, s: String) -> Vec<String> {
|
||||
let mut xs = Vec::new();
|
||||
let mut i = 0;
|
||||
let len = s.len();
|
||||
loop {
|
||||
let lo = i * max_len;
|
||||
let hi = cmp::min(len - 1, lo + max_len - 1);
|
||||
|
||||
if lo >= len {
|
||||
break;
|
||||
}
|
||||
|
||||
let chunk = &s[lo..hi];
|
||||
xs.push(chunk.to_string());
|
||||
i += 1;
|
||||
}
|
||||
xs
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue