Improved logic when encountering ANSI in character wrap mode

Fixes #551
This commit is contained in:
Ethan P 2019-05-08 19:30:06 -07:00 committed by David Peter
parent 7ef0fe091d
commit 0ad09df7ab

View file

@ -367,15 +367,25 @@ impl<'a> Printer for InteractivePrinter<'a> {
match chunk { match chunk {
// ANSI escape passthrough. // ANSI escape passthrough.
(text, true) => { (text, true) => {
if text.chars().last().map_or(false, |c| c == 'm') { let is_ansi_csi = text.chars().skip(1).nth(0).map_or(false, |c|c == '[');
if is_ansi_csi && text.chars().last().map_or(false, |c| c == 'm') {
// It's an ANSI SGR sequence.
// We should be mostly safe to just append these together.
ansi_prefix.push_str(text); ansi_prefix.push_str(text);
if text == "\x1B[0m" { if text == "\x1B[0m" {
self.ansi_prefix_sgr = "\x1B[0m".to_owned(); self.ansi_prefix_sgr = "\x1B[0m".to_owned();
} else { } else {
self.ansi_prefix_sgr.push_str(text); self.ansi_prefix_sgr.push_str(text);
} }
} else { } else if is_ansi_csi {
// It's a regular CSI sequence.
// We should be mostly safe to just append these together.
ansi_prefix.push_str(text); ansi_prefix.push_str(text);
} else {
// It's probably a VT100 code.
// Passing it through is the safest bet.
write!(handle, "{}", text)?;
} }
} }