Skip to content

Commit e36256d

Browse files
committed
Improve error coloring: bold path, red message, clickable link
For errors following "- path: message", the path is now bold and the message is red. The wiki link uses an OSC 8 hyperlink (clickable NPV code) in terminal output. Errors with other formats fall back to all-red with the link appended.
1 parent d4a582a commit e36256d

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/status.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,33 @@ impl Status {
4242
// These all respect the NO_COLOR environment variable even if `use_color` is true.
4343
let maybe_green = |s: &str| if use_color { s.green() } else { s.into() };
4444
let maybe_yellow = |s: &str| if use_color { s.yellow() } else { s.into() };
45-
let maybe_red = |s: &str| if use_color { s.red() } else { s.into() };
46-
47-
// If there are errors, print them all out first in red.
45+
// Print each error with its wiki link.
4846
if let Some(errors) = self.errors() {
4947
for error in errors {
5048
let code = error.npv_code();
5149
let url = error.wiki_url();
52-
let link = if use_color {
50+
51+
if use_color {
52+
let error_str = format!("{error}");
5353
// OSC 8 hyperlink: \e]8;;URL\e\\TEXT\e]8;;\e\\
54-
format!("\x1b]8;;{url}\x1b\\{code}\x1b]8;;\x1b\\")
54+
let link = format!("\x1b]8;;{url}\x1b\\{code}\x1b]8;;\x1b\\");
55+
56+
// Most errors follow "- {path}: {message}". When we can identify
57+
// that pattern, make the path bold and the message red. Otherwise
58+
// fall back to coloring the entire error red.
59+
if let Some(rest) = error_str.strip_prefix("- ")
60+
&& let Some((location, message)) = rest.split_once(": ")
61+
&& !location.contains('\n')
62+
{
63+
writeln!(f, "- {}: {} ({})", location.bold(), message.red(), link)?;
64+
continue;
65+
}
66+
67+
// Fallback for messages that don't match the simple pattern.
68+
writeln!(f, "{} ({})", error_str.red(), link)?;
5569
} else {
56-
format!("{url}")
57-
};
58-
let line = format!("{error} ({link})\n");
59-
fmt::Display::fmt(&maybe_red(&line), f)?;
70+
writeln!(f, "{error} ({url})")?;
71+
}
6072
}
6173
}
6274

0 commit comments

Comments
 (0)