chore: Remove unneeded ansi escape resets from output

This commit is contained in:
Scott Schafer
2025-07-28 20:04:48 -06:00
parent a484119046
commit 5aedef17f9
4 changed files with 86 additions and 73 deletions

View File

@@ -3408,7 +3408,6 @@ pub(crate) fn emit_to_destination(
short_message: bool,
) -> io::Result<()> {
use crate::lock;
const RESET: anstyle::Reset = anstyle::Reset;
// In order to prevent error message interleaving, where multiple error lines get intermixed
// when multiple compiler processes error simultaneously, we emit errors with additional
@@ -3426,7 +3425,7 @@ pub(crate) fn emit_to_destination(
for (pos, line) in rendered_buffer.iter().enumerate() {
for part in line {
let style = part.style.anstyle(*lvl);
write!(dst, "{RESET}{style}{}{RESET}", part.text)?;
write!(dst, "{style}{}{style:#}", part.text)?;
}
if !short_message && (!lvl.is_failure_note() || pos != rendered_buffer.len() - 1) {
writeln!(dst)?;

View File

@@ -6,7 +6,6 @@ use anstyle::{AnsiColor, Effects, Style};
use crate::markdown::{MdStream, MdTree};
const DEFAULT_COLUMN_WIDTH: usize = 140;
const RESET: anstyle::Reset = anstyle::Reset;
thread_local! {
/// Track the position of viewable characters in our buffer
@@ -31,53 +30,52 @@ fn write_stream(
default: Option<Style>,
indent: usize,
) -> io::Result<()> {
match default {
Some(c) => write!(buf, "{c:#}{c}")?,
None => write!(buf, "{RESET}")?,
}
for tt in stream {
write_tt(tt, buf, default, indent)?;
if let Some(c) = default {
write!(buf, "{c:#}{c}")?;
}
}
reset_opt_style(buf, default)?;
write!(buf, "{RESET}")?;
Ok(())
}
fn write_tt(
tt: &MdTree<'_>,
buf: &mut Vec<u8>,
_default: Option<Style>,
default: Option<Style>,
indent: usize,
) -> io::Result<()> {
match tt {
MdTree::CodeBlock { txt, lang: _ } => {
write!(buf, "{RESET}")?;
reset_opt_style(buf, default)?;
let style = Style::new().effects(Effects::DIMMED);
write!(buf, "{style}{txt}")?;
write!(buf, "{style}{txt}{style:#}")?;
render_opt_style(buf, default)?;
}
MdTree::CodeInline(txt) => {
write!(buf, "{RESET}")?;
let style = Style::new().effects(Effects::DIMMED);
write_wrapping(buf, txt, indent, None, Some(style))?;
reset_opt_style(buf, default)?;
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::DIMMED)))?;
render_opt_style(buf, default)?;
}
MdTree::Strong(txt) => {
write!(buf, "{RESET}")?;
let style = Style::new().effects(Effects::BOLD);
write_wrapping(buf, txt, indent, None, Some(style))?;
reset_opt_style(buf, default)?;
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::BOLD)))?;
render_opt_style(buf, default)?;
}
MdTree::Emphasis(txt) => {
write!(buf, "{RESET}")?;
let style = Style::new().effects(Effects::ITALIC);
write_wrapping(buf, txt, indent, None, Some(style))?;
reset_opt_style(buf, default)?;
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::ITALIC)))?;
render_opt_style(buf, default)?;
}
MdTree::Strikethrough(txt) => {
write!(buf, "{RESET}")?;
let style = Style::new().effects(Effects::STRIKETHROUGH);
write_wrapping(buf, txt, indent, None, Some(style))?;
reset_opt_style(buf, default)?;
write_wrapping(
buf,
txt,
indent,
None,
Some(Style::new().effects(Effects::STRIKETHROUGH)),
)?;
render_opt_style(buf, default)?;
}
MdTree::PlainText(txt) => {
write_wrapping(buf, txt, indent, None, None)?;
@@ -105,7 +103,11 @@ fn write_tt(
4.. => AnsiColor::Cyan.on_default().effects(Effects::UNDERLINE | Effects::ITALIC),
0 => unreachable!(),
};
reset_opt_style(buf, default)?;
write!(buf, "{cs}")?;
write_stream(stream, buf, Some(cs), 0)?;
write!(buf, "{cs:#}")?;
render_opt_style(buf, default)?;
buf.write_all(b"\n")?;
}
MdTree::OrderedListItem(n, stream) => {
@@ -122,8 +124,20 @@ fn write_tt(
MdTree::Comment(_) | MdTree::LinkDef { .. } | MdTree::RefLink { .. } => unreachable!(),
}
write!(buf, "{RESET}")?;
Ok(())
}
fn render_opt_style(buf: &mut Vec<u8>, style: Option<Style>) -> io::Result<()> {
if let Some(style) = &style {
write!(buf, "{style}")?;
}
Ok(())
}
fn reset_opt_style(buf: &mut Vec<u8>, style: Option<Style>) -> io::Result<()> {
if let Some(style) = &style {
write!(buf, "{style:#}")?;
}
Ok(())
}
@@ -141,9 +155,8 @@ fn write_wrapping(
link_url: Option<&str>,
style: Option<Style>,
) -> io::Result<()> {
if let Some(style) = &style {
write!(buf, "{style}")?;
}
render_opt_style(buf, style)?;
let ind_ws = &b" "[..indent];
let mut to_write = text;
if let Some(url) = link_url {
@@ -191,6 +204,7 @@ fn write_wrapping(
if link_url.is_some() {
buf.write_all(b"\x1b]8;;\x1b\\")?;
}
reset_opt_style(buf, style)?;
Ok(())
})
}

View File

@@ -1,35 +1,35 @@
H1 Heading ]8;;http://docs.rs\with a link]8;;\
H1 content: some words in bold and so does inline code
H1 Heading ]8;;http://docs.rs\with a link]8;;\
H1 content: some words in bold and so does inline code
H2 Heading
H2 content: some words in italic
H2 Heading
H2 content: some words in italic
H3 Heading
H3 content: strikethrough text
H3 Heading
H3 content: strikethrough text
H4 Heading
H4 content: A ]8;;https://docs.rs\simple link]8;;\ and a ]8;;http://docs.rs\remote-link]8;;\.
--------------------------------------------------------------------------------------------------------------------------------------------
A section break was above. We can also do paragraph breaks:
H4 Heading
H4 content: A ]8;;https://docs.rs\simple link]8;;\ and a ]8;;http://docs.rs\remote-link]8;;\.
--------------------------------------------------------------------------------------------------------------------------------------------
A section break was above. We can also do paragraph breaks:
(new paragraph) and unordered lists:
(new paragraph) and unordered lists:
* Item 1 in code
* Item 2 in italics
* Item 1 in code
* Item 2 in italics
Or ordered:
Or ordered:
1. Item 1 in bold
2. Item 2 with some long lines that should wrap: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac mattis nunc. Phasellus
elit quam, pulvinar ac risus in, dictum vehicula turpis. Vestibulum neque est, accumsan in cursus sit amet, dictum a nunc. Suspendisse
aliquet, lorem eu eleifend accumsan, magna neque sodales nisi, a aliquet lectus leo eu sem.
--------------------------------------------------------------------------------------------------------------------------------------------
Code
Both inline code and code blocks are supported:
1. Item 1 in bold
2. Item 2 with some long lines that should wrap: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac mattis nunc. Phasellus
elit quam, pulvinar ac risus in, dictum vehicula turpis. Vestibulum neque est, accumsan in cursus sit amet, dictum a nunc. Suspendisse
aliquet, lorem eu eleifend accumsan, magna neque sodales nisi, a aliquet lectus leo eu sem.
--------------------------------------------------------------------------------------------------------------------------------------------
Code
Both inline code and code blocks are supported:
/// A rust enum
/// A rust enum
#[derive(Debug, PartialEq, Clone)]
enum Foo {
/// Start of line
Bar
}
}