Skip to content

Commit 1e5a5fd

Browse files
committed
chore: Remove unneeded ansi escape resets from output
1 parent a49d3aa commit 1e5a5fd

File tree

4 files changed

+86
-73
lines changed

4 files changed

+86
-73
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,7 +3408,6 @@ pub(crate) fn emit_to_destination(
34083408
short_message: bool,
34093409
) -> io::Result<()> {
34103410
use crate::lock;
3411-
const RESET: anstyle::Reset = anstyle::Reset;
34123411

34133412
// In order to prevent error message interleaving, where multiple error lines get intermixed
34143413
// when multiple compiler processes error simultaneously, we emit errors with additional
@@ -3426,7 +3425,7 @@ pub(crate) fn emit_to_destination(
34263425
for (pos, line) in rendered_buffer.iter().enumerate() {
34273426
for part in line {
34283427
let style = part.style.anstyle(*lvl);
3429-
write!(dst, "{RESET}{style}{}{RESET}", part.text)?;
3428+
write!(dst, "{style}{}{style:#}", part.text)?;
34303429
}
34313430
if !short_message && (!lvl.is_failure_note() || pos != rendered_buffer.len() - 1) {
34323431
writeln!(dst)?;

compiler/rustc_errors/src/markdown/term.rs

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use anstyle::{AnsiColor, Effects, Style};
66
use crate::markdown::{MdStream, MdTree};
77

88
const DEFAULT_COLUMN_WIDTH: usize = 140;
9-
const RESET: anstyle::Reset = anstyle::Reset;
109

1110
thread_local! {
1211
/// Track the position of viewable characters in our buffer
@@ -31,53 +30,52 @@ fn write_stream(
3130
default: Option<Style>,
3231
indent: usize,
3332
) -> io::Result<()> {
34-
match default {
35-
Some(c) => write!(buf, "{c:#}{c}")?,
36-
None => write!(buf, "{RESET}")?,
37-
}
38-
3933
for tt in stream {
4034
write_tt(tt, buf, default, indent)?;
41-
if let Some(c) = default {
42-
write!(buf, "{c:#}{c}")?;
43-
}
4435
}
36+
reset_opt_style(buf, default)?;
4537

46-
write!(buf, "{RESET}")?;
4738
Ok(())
4839
}
4940

5041
fn write_tt(
5142
tt: &MdTree<'_>,
5243
buf: &mut Vec<u8>,
53-
_default: Option<Style>,
44+
default: Option<Style>,
5445
indent: usize,
5546
) -> io::Result<()> {
5647
match tt {
5748
MdTree::CodeBlock { txt, lang: _ } => {
58-
write!(buf, "{RESET}")?;
49+
reset_opt_style(buf, default)?;
5950
let style = Style::new().effects(Effects::DIMMED);
60-
write!(buf, "{style}{txt}")?;
51+
write!(buf, "{style}{txt}{style:#}")?;
52+
render_opt_style(buf, default)?;
6153
}
6254
MdTree::CodeInline(txt) => {
63-
write!(buf, "{RESET}")?;
64-
let style = Style::new().effects(Effects::DIMMED);
65-
write_wrapping(buf, txt, indent, None, Some(style))?;
55+
reset_opt_style(buf, default)?;
56+
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::DIMMED)))?;
57+
render_opt_style(buf, default)?;
6658
}
6759
MdTree::Strong(txt) => {
68-
write!(buf, "{RESET}")?;
69-
let style = Style::new().effects(Effects::BOLD);
70-
write_wrapping(buf, txt, indent, None, Some(style))?;
60+
reset_opt_style(buf, default)?;
61+
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::BOLD)))?;
62+
render_opt_style(buf, default)?;
7163
}
7264
MdTree::Emphasis(txt) => {
73-
write!(buf, "{RESET}")?;
74-
let style = Style::new().effects(Effects::ITALIC);
75-
write_wrapping(buf, txt, indent, None, Some(style))?;
65+
reset_opt_style(buf, default)?;
66+
write_wrapping(buf, txt, indent, None, Some(Style::new().effects(Effects::ITALIC)))?;
67+
render_opt_style(buf, default)?;
7668
}
7769
MdTree::Strikethrough(txt) => {
78-
write!(buf, "{RESET}")?;
79-
let style = Style::new().effects(Effects::STRIKETHROUGH);
80-
write_wrapping(buf, txt, indent, None, Some(style))?;
70+
reset_opt_style(buf, default)?;
71+
write_wrapping(
72+
buf,
73+
txt,
74+
indent,
75+
None,
76+
Some(Style::new().effects(Effects::STRIKETHROUGH)),
77+
)?;
78+
render_opt_style(buf, default)?;
8179
}
8280
MdTree::PlainText(txt) => {
8381
write_wrapping(buf, txt, indent, None, None)?;
@@ -105,7 +103,11 @@ fn write_tt(
105103
4.. => AnsiColor::Cyan.on_default().effects(Effects::UNDERLINE | Effects::ITALIC),
106104
0 => unreachable!(),
107105
};
106+
reset_opt_style(buf, default)?;
107+
write!(buf, "{cs}")?;
108108
write_stream(stream, buf, Some(cs), 0)?;
109+
write!(buf, "{cs:#}")?;
110+
render_opt_style(buf, default)?;
109111
buf.write_all(b"\n")?;
110112
}
111113
MdTree::OrderedListItem(n, stream) => {
@@ -122,8 +124,20 @@ fn write_tt(
122124
MdTree::Comment(_) | MdTree::LinkDef { .. } | MdTree::RefLink { .. } => unreachable!(),
123125
}
124126

125-
write!(buf, "{RESET}")?;
127+
Ok(())
128+
}
126129

130+
fn render_opt_style(buf: &mut Vec<u8>, style: Option<Style>) -> io::Result<()> {
131+
if let Some(style) = &style {
132+
write!(buf, "{style}")?;
133+
}
134+
Ok(())
135+
}
136+
137+
fn reset_opt_style(buf: &mut Vec<u8>, style: Option<Style>) -> io::Result<()> {
138+
if let Some(style) = &style {
139+
write!(buf, "{style:#}")?;
140+
}
127141
Ok(())
128142
}
129143

@@ -141,9 +155,8 @@ fn write_wrapping(
141155
link_url: Option<&str>,
142156
style: Option<Style>,
143157
) -> io::Result<()> {
144-
if let Some(style) = &style {
145-
write!(buf, "{style}")?;
146-
}
158+
render_opt_style(buf, style)?;
159+
147160
let ind_ws = &b" "[..indent];
148161
let mut to_write = text;
149162
if let Some(url) = link_url {
@@ -191,6 +204,7 @@ fn write_wrapping(
191204
if link_url.is_some() {
192205
buf.write_all(b"\x1b]8;;\x1b\\")?;
193206
}
207+
reset_opt_style(buf, style)?;
194208
Ok(())
195209
})
196210
}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
H1 Heading ]8;;http://docs.rs\with a link]8;;\
2-
H1 content: some words in bold and so does inline code
1+
H1 Heading ]8;;http://docs.rs\with a link]8;;\
2+
H1 content: some words in bold and so does inline code
33

4-
H2 Heading
5-
H2 content: some words in italic
4+
H2 Heading
5+
H2 content: some words in italic
66

7-
H3 Heading
8-
H3 content: strikethrough text
7+
H3 Heading
8+
H3 content: strikethrough text
99

10-
H4 Heading
11-
H4 content: A ]8;;https://docs.rs\simple link]8;;\ and a ]8;;http://docs.rs\remote-link]8;;\.
12-
--------------------------------------------------------------------------------------------------------------------------------------------
13-
A section break was above. We can also do paragraph breaks:
10+
H4 Heading
11+
H4 content: A ]8;;https://docs.rs\simple link]8;;\ and a ]8;;http://docs.rs\remote-link]8;;\.
12+
--------------------------------------------------------------------------------------------------------------------------------------------
13+
A section break was above. We can also do paragraph breaks:
1414

15-
(new paragraph) and unordered lists:
15+
(new paragraph) and unordered lists:
1616

17-
* Item 1 in code
18-
* Item 2 in italics
17+
* Item 1 in code
18+
* Item 2 in italics
1919

20-
Or ordered:
20+
Or ordered:
2121

22-
1. Item 1 in bold
23-
2. Item 2 with some long lines that should wrap: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac mattis nunc. Phasellus
24-
elit quam, pulvinar ac risus in, dictum vehicula turpis. Vestibulum neque est, accumsan in cursus sit amet, dictum a nunc. Suspendisse
25-
aliquet, lorem eu eleifend accumsan, magna neque sodales nisi, a aliquet lectus leo eu sem.
26-
--------------------------------------------------------------------------------------------------------------------------------------------
27-
Code
28-
Both inline code and code blocks are supported:
22+
1. Item 1 in bold
23+
2. Item 2 with some long lines that should wrap: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ac mattis nunc. Phasellus
24+
elit quam, pulvinar ac risus in, dictum vehicula turpis. Vestibulum neque est, accumsan in cursus sit amet, dictum a nunc. Suspendisse
25+
aliquet, lorem eu eleifend accumsan, magna neque sodales nisi, a aliquet lectus leo eu sem.
26+
--------------------------------------------------------------------------------------------------------------------------------------------
27+
Code
28+
Both inline code and code blocks are supported:
2929

30-
/// A rust enum
30+
/// A rust enum
3131
#[derive(Debug, PartialEq, Clone)]
3232
enum Foo {
3333
/// Start of line
3434
Bar
35-
}
35+
}

tests/ui/lint/use_suggestion_json.stderr

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,23 +403,23 @@ mod foo {
403403
"rendered": null
404404
}
405405
],
406-
"rendered": "\u001b[0m\u001b[1m\u001b[91merror[E0412]\u001b[0m\u001b[0m\u001b[1m: cannot find type `Iter` in this scope\u001b[0m
407-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m--> \u001b[0m\u001b[0m$DIR/use_suggestion_json.rs:12:12\u001b[0m
408-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
409-
\u001b[0m\u001b[1m\u001b[94mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m let x: Iter;\u001b[0m
410-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[91m^^^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[91mnot found in this scope\u001b[0m
411-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
412-
\u001b[0m\u001b[1m\u001b[96mhelp\u001b[0m\u001b[0m: consider importing one of these structs\u001b[0m
413-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
414-
\u001b[0m\u001b[1m\u001b[94mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[92m+ use std::collections::binary_heap::Iter;\u001b[0m
415-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
416-
\u001b[0m\u001b[1m\u001b[94mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[92m+ use std::collections::btree_map::Iter;\u001b[0m
417-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
418-
\u001b[0m\u001b[1m\u001b[94mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[92m+ use std::collections::btree_set::Iter;\u001b[0m
419-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
420-
\u001b[0m\u001b[1m\u001b[94mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[92m+ use std::collections::hash_map::Iter;\u001b[0m
421-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m|\u001b[0m
422-
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[94m= \u001b[0m\u001b[0mand 9 other candidates\u001b[0m
406+
"rendered": "\u001b[1m\u001b[91merror[E0412]\u001b[0m\u001b[1m: cannot find type `Iter` in this scope\u001b[0m
407+
\u001b[1m\u001b[94m--> \u001b[0m$DIR/use_suggestion_json.rs:12:12
408+
\u001b[1m\u001b[94m|\u001b[0m
409+
\u001b[1m\u001b[94mLL\u001b[0m \u001b[1m\u001b[94m|\u001b[0m let x: Iter;
410+
\u001b[1m\u001b[94m|\u001b[0m \u001b[1m\u001b[91m^^^^\u001b[0m \u001b[1m\u001b[91mnot found in this scope\u001b[0m
411+
\u001b[1m\u001b[94m|\u001b[0m
412+
\u001b[1m\u001b[96mhelp\u001b[0m: consider importing one of these structs
413+
\u001b[1m\u001b[94m|\u001b[0m
414+
\u001b[1m\u001b[94mLL\u001b[0m \u001b[92m+ use std::collections::binary_heap::Iter;\u001b[0m
415+
\u001b[1m\u001b[94m|\u001b[0m
416+
\u001b[1m\u001b[94mLL\u001b[0m \u001b[92m+ use std::collections::btree_map::Iter;\u001b[0m
417+
\u001b[1m\u001b[94m|\u001b[0m
418+
\u001b[1m\u001b[94mLL\u001b[0m \u001b[92m+ use std::collections::btree_set::Iter;\u001b[0m
419+
\u001b[1m\u001b[94m|\u001b[0m
420+
\u001b[1m\u001b[94mLL\u001b[0m \u001b[92m+ use std::collections::hash_map::Iter;\u001b[0m
421+
\u001b[1m\u001b[94m|\u001b[0m
422+
\u001b[1m\u001b[94m= \u001b[0mand 9 other candidates
423423

424424
"
425425
}
@@ -430,7 +430,7 @@ mod foo {
430430
"level": "error",
431431
"spans": [],
432432
"children": [],
433-
"rendered": "\u001b[0m\u001b[1m\u001b[91merror\u001b[0m\u001b[0m\u001b[1m: aborting due to 1 previous error\u001b[0m
433+
"rendered": "\u001b[1m\u001b[91merror\u001b[0m\u001b[1m: aborting due to 1 previous error\u001b[0m
434434

435435
"
436436
}
@@ -441,6 +441,6 @@ mod foo {
441441
"level": "failure-note",
442442
"spans": [],
443443
"children": [],
444-
"rendered": "\u001b[0m\u001b[1mFor more information about this error, try `rustc --explain E0412`.\u001b[0m
444+
"rendered": "\u001b[1mFor more information about this error, try `rustc --explain E0412`.\u001b[0m
445445
"
446446
}

0 commit comments

Comments
 (0)