From d75f8545e37648f8ddc4b896ec7c648af1ad5730 Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Sun, 11 Feb 2024 19:01:00 -0800 Subject: [PATCH] Remove ANSI support (demo) --- src/printer.rs | 243 +++++++++++++++++++++---------------------------- 1 file changed, 103 insertions(+), 140 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index f413fdc..273c985 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -164,7 +164,6 @@ pub(crate) struct InteractivePrinter<'a> { config: &'a Config<'a>, decorations: Vec>, panel_width: usize, - ansi_style: AnsiStyle, content_type: Option, #[cfg(feature = "git")] pub line_changes: &'a Option, @@ -248,7 +247,6 @@ impl<'a> InteractivePrinter<'a> { config, decorations, content_type: input.reader.content_type, - ansi_style: AnsiStyle::new(), #[cfg(feature = "git")] line_changes, highlighter_from_set, @@ -565,10 +563,6 @@ impl<'a> Printer for InteractivePrinter<'a> { let highlight_this_line = self.config.highlighted_lines.0.check(line_number) == RangeCheckResult::InRange; - if highlight_this_line && self.config.theme == "ansi" { - self.ansi_style.update(ANSI_UNDERLINE_ENABLE); - } - let background_color = self .background_color_highlight .filter(|_| highlight_this_line); @@ -593,53 +587,38 @@ impl<'a> Printer for InteractivePrinter<'a> { let italics = self.config.use_italic_text; for &(style, region) in ®ions { - let ansi_iterator = EscapeSequenceIterator::new(region); - for chunk in ansi_iterator { - match chunk { - // Regular text. - EscapeSequence::Text(text) => { - let text = self.preprocess(text, &mut cursor_total); - let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n'); - - write!( - handle, - "{}{}", - as_terminal_escaped( - style, - &format!("{}{}", self.ansi_style, text_trimmed), - true_color, - colored_output, - italics, - background_color - ), - self.ansi_style.to_reset_sequence(), - )?; - - // Pad the rest of the line. - if text.len() != text_trimmed.len() { - if let Some(background_color) = background_color { - let ansi_style = Style { - background: to_ansi_color(background_color, true_color), - ..Default::default() - }; - - let width = if cursor_total <= cursor_max { - cursor_max - cursor_total + 1 - } else { - 0 - }; - write!(handle, "{}", ansi_style.paint(" ".repeat(width)))?; - } - write!(handle, "{}", &text[text_trimmed.len()..])?; - } - } + let text = self.preprocess(region, &mut cursor_total); + let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n'); - // ANSI escape passthrough. - _ => { - write!(handle, "{}", chunk.raw())?; - self.ansi_style.update(chunk); - } + write!( + handle, + "{}", + as_terminal_escaped( + style, + &text_trimmed, + true_color, + colored_output, + italics, + background_color + ), + )?; + + // Pad the rest of the line. + if text.len() != text_trimmed.len() { + if let Some(background_color) = background_color { + let ansi_style = Style { + background: to_ansi_color(background_color, true_color), + ..Default::default() + }; + + let width = if cursor_total <= cursor_max { + cursor_max - cursor_total + 1 + } else { + 0 + }; + write!(handle, "{}", ansi_style.paint(" ".repeat(width)))?; } + write!(handle, "{}", &text[text_trimmed.len()..])?; } } @@ -648,99 +627,84 @@ impl<'a> Printer for InteractivePrinter<'a> { } } else { for &(style, region) in ®ions { - let ansi_iterator = EscapeSequenceIterator::new(region); - for chunk in ansi_iterator { - match chunk { - // Regular text. - EscapeSequence::Text(text) => { - let text = self.preprocess( - text.trim_end_matches(|c| c == '\r' || c == '\n'), - &mut cursor_total, - ); - - let mut max_width = cursor_max - cursor; - - // line buffer (avoid calling write! for every character) - let mut line_buf = String::with_capacity(max_width * 4); - - // Displayed width of line_buf - let mut current_width = 0; - - for c in text.chars() { - // calculate the displayed width for next character - let cw = c.width().unwrap_or(0); - current_width += cw; - - // if next character cannot be printed on this line, - // flush the buffer. - if current_width > max_width { - // Generate wrap padding if not already generated. - if panel_wrap.is_none() { - panel_wrap = if self.panel_width > 0 { - Some(format!( - "{} ", - self.decorations - .iter() - .map(|d| d - .generate(line_number, true, self) - .text) - .collect::>() - .join(" ") - )) - } else { - Some("".to_string()) - } - } - - // It wraps. - write!( - handle, - "{}{}\n{}", - as_terminal_escaped( - style, - &format!("{}{}", self.ansi_style, line_buf), - self.config.true_color, - self.config.colored_output, - self.config.use_italic_text, - background_color - ), - self.ansi_style.to_reset_sequence(), - panel_wrap.clone().unwrap() - )?; - - cursor = 0; - max_width = cursor_max; - - line_buf.clear(); - current_width = cw; - } - - line_buf.push(c); - } + let text = self.preprocess( + region.trim_end_matches(|c| c == '\r' || c == '\n'), + &mut cursor_total, + ); - // flush the buffer - cursor += current_width; - write!( - handle, - "{}", - as_terminal_escaped( - style, - &format!("{}{}", self.ansi_style, line_buf), - self.config.true_color, - self.config.colored_output, - self.config.use_italic_text, - background_color - ) - )?; + let mut max_width = cursor_max - cursor; + + // line buffer (avoid calling write! for every character) + let mut line_buf = String::with_capacity(max_width * 4); + + // Displayed width of line_buf + let mut current_width = 0; + + for c in text.chars() { + // calculate the displayed width for next character + let cw = c.width().unwrap_or(0); + current_width += cw; + + // if next character cannot be printed on this line, + // flush the buffer. + if current_width > max_width { + // Generate wrap padding if not already generated. + if panel_wrap.is_none() { + panel_wrap = if self.panel_width > 0 { + Some(format!( + "{} ", + self.decorations + .iter() + .map(|d| d + .generate(line_number, true, self) + .text) + .collect::>() + .join(" ") + )) + } else { + Some("".to_string()) + } } - // ANSI escape passthrough. - _ => { - write!(handle, "{}", chunk.raw())?; - self.ansi_style.update(chunk); - } + // It wraps. + write!( + handle, + "{}\n{}", + as_terminal_escaped( + style, + &line_buf, + self.config.true_color, + self.config.colored_output, + self.config.use_italic_text, + background_color + ), + panel_wrap.clone().unwrap() + )?; + + cursor = 0; + max_width = cursor_max; + + line_buf.clear(); + current_width = cw; } + + line_buf.push(c); } + + // flush the buffer + cursor += current_width; + write!( + handle, + "{}", + as_terminal_escaped( + style, + &line_buf, + self.config.true_color, + self.config.colored_output, + self.config.use_italic_text, + background_color + ) + )?; } if let Some(background_color) = background_color { @@ -760,7 +724,6 @@ impl<'a> Printer for InteractivePrinter<'a> { if highlight_this_line && self.config.theme == "ansi" { write!(handle, "{}", ANSI_UNDERLINE_DISABLE.raw())?; - self.ansi_style.update(ANSI_UNDERLINE_DISABLE); } Ok(()) -- 2.42.0