Skip to content

Commit

Permalink
test for ansi_aware_split
Browse files Browse the repository at this point in the history
  • Loading branch information
blueforesticarus committed Nov 12, 2022
1 parent 96e44ef commit cceb3ac
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/utils/formatting/content_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub fn ansi_aware_split(line: &str, delimiter: char) -> Vec<String> {
}
}
lines.push(current_line);
fix_style_in_split_str(lines.as_mut());
lines
}

Expand Down Expand Up @@ -330,10 +331,14 @@ pub fn fix_style_in_split_str(words: &mut [String]) {
let mut escapes: Vec<String> = Vec::new();

for word in words {
if !escapes.is_empty() {
word.insert_str(0, escapes.join("").as_str())
}
// before we modify the escape list, make a copy
let prepend = if !escapes.is_empty() {
Some(escapes.join(""))
} else {
None
};

// add escapes in word to escape list
let iter = console::AnsiCodeIterator::new(word)
.filter(|(_, is_esc)| *is_esc)
.map(|v| v.0);
Expand All @@ -345,8 +350,37 @@ pub fn fix_style_in_split_str(words: &mut [String]) {
}
}

// insert previous esc sequences at the beginning of the segment
if let Some(prepend) = prepend {
word.insert_str(0, &prepend);
}

// if there are active escape sequences, we need to append reset
if !escapes.is_empty() {
word.push_str(ANSI_RESET);
}
}
}

#[cfg(test)]

mod test {
#[cfg(feature = "ansi")]
#[test]
fn ansi_aware_split_test() {
use super::ansi_aware_split;

let text = "\u{1b}[1m head [ middle [ tail \u{1b}[0m[ after";
let split = ansi_aware_split(text, '[');

assert_eq!(
split,
[
"\u{1b}[1m head \u{1b}[0m",
"\u{1b}[1m middle \u{1b}[0m",
"\u{1b}[1m tail \u{1b}[0m",
" after"
]
)
}
}

0 comments on commit cceb3ac

Please sign in to comment.