1use anstyle::Style;
2use std::borrow::Cow;
3use std::io::IsTerminal;
4
5pub fn should_produce_plain_output(nocolor: bool) -> bool {
6 nocolor || !std::io::stdout().is_terminal()
7}
8
9pub fn colorize_helper(nocolor: bool, s: &str, style: Style) -> Cow<'_, str> {
10 if nocolor {
11 s.into()
12 } else {
13 format!("{style}{s}{style:#}").into()
14 }
15}
16
17pub fn split_string_on_spaces(s: &str, line_length: usize) -> Vec<&str> {
18 let mut lines = Vec::new();
19 let mut line_start = 0;
20 let mut last_space = 0;
21
22 for (idx, char) in s.char_indices() {
23 if char.is_whitespace() {
24 last_space = idx;
25 }
26
27 if idx - line_start >= line_length {
28 if last_space == line_start {
29 lines.push(&s[line_start..idx]);
30 line_start = idx + 1;
31 } else {
32 lines.push(&s[line_start..last_space]);
33 line_start = last_space + 1;
34 }
35 last_space = line_start;
36 }
37 }
38
39 if line_start < s.len() {
40 lines.push(&s[line_start..]);
41 }
42
43 lines
44}