Skip to content

Commit a42ea90

Browse files
committed
Apply colors only when they are supported (#236, #237)
1 parent 6e9ced8 commit a42ea90

File tree

10 files changed

+223
-31
lines changed

10 files changed

+223
-31
lines changed

Cargo.lock

Lines changed: 153 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ indenter = "0.3.0"
1818
once_cell = "1.18.0"
1919
owo-colors = "4.0"
2020
autocfg = "1.0"
21+
anstream = "0.6.15"
2122

2223
[profile.dev.package.backtrace]
2324
opt-level = 3

color-eyre/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ owo-colors = { workspace = true }
2626
color-spantrace = { version = "0.2", path = "../color-spantrace", optional = true }
2727
once_cell = { workspace = true }
2828
url = { version = "2.1.1", optional = true }
29+
anstream = { workspace = true }
2930

3031
[dev-dependencies]
3132
tracing-subscriber = { version = "0.3.0", features = ["env-filter"] }

color-eyre/src/config.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
//! and error reporting hooks
33
use crate::{
44
section::PanicMessage,
5+
style_if_possible,
56
writers::{EnvSection, WriterExt},
67
};
78
use fmt::Display;
89
use indenter::{indented, Format};
9-
use owo_colors::{style, OwoColorize, Style};
10+
use owo_colors::{style, Style};
1011
use std::env;
1112
use std::fmt::Write as _;
1213
use std::{fmt, path::PathBuf, sync::Arc};
@@ -192,12 +193,12 @@ impl fmt::Display for StyledFrame<'_> {
192193
};
193194

194195
if is_dependency_code {
195-
write!(f, "{}", (name).style(theme.dependency_code))?;
196+
write!(f, "{}", style_if_possible(name, theme.dependency_code))?;
196197
} else {
197-
write!(f, "{}", (name).style(theme.crate_code))?;
198+
write!(f, "{}", style_if_possible(name, theme.crate_code))?;
198199
}
199200

200-
write!(f, "{}", (hash_suffix).style(theme.code_hash))?;
201+
write!(f, "{}", style_if_possible(hash_suffix, theme.code_hash))?;
201202

202203
let mut separated = f.header("\n");
203204

@@ -214,8 +215,8 @@ impl fmt::Display for StyledFrame<'_> {
214215
write!(
215216
&mut separated.ready(),
216217
" at {}:{}",
217-
file.style(theme.file),
218-
lineno.style(theme.line_number),
218+
style_if_possible(file, theme.file),
219+
style_if_possible(lineno, theme.line_number),
219220
)?;
220221

221222
let v = if std::thread::panicking() {
@@ -266,9 +267,9 @@ impl fmt::Display for SourceSection<'_> {
266267
write!(
267268
&mut f,
268269
"{:>8} {} {}",
269-
cur_line_no.style(theme.active_line),
270-
">".style(theme.active_line),
271-
line.style(theme.active_line),
270+
style_if_possible(cur_line_no, theme.active_line),
271+
style_if_possible(">", theme.active_line),
272+
style_if_possible(line, theme.active_line),
272273
)?;
273274
} else {
274275
write!(&mut f, "{:>8} │ {}", cur_line_no, line)?;
@@ -797,7 +798,7 @@ impl PanicMessage for DefaultPanicMessage {
797798
writeln!(
798799
f,
799800
"{}",
800-
"The application panicked (crashed).".style(theme.panic_header)
801+
style_if_possible("The application panicked (crashed).", theme.panic_header)
801802
)?;
802803

803804
// Print panic message.
@@ -809,7 +810,7 @@ impl PanicMessage for DefaultPanicMessage {
809810
.unwrap_or("<non string panic payload>");
810811

811812
write!(f, "Message: ")?;
812-
writeln!(f, "{}", payload.style(theme.panic_message))?;
813+
writeln!(f, "{}", style_if_possible(payload, theme.panic_message))?;
813814

814815
// If known, print panic location.
815816
write!(f, "Location: ")?;
@@ -1131,7 +1132,7 @@ impl fmt::Display for BacktraceFormatter<'_> {
11311132
write!(
11321133
&mut separated.ready(),
11331134
"{:^80}",
1134-
buf.style(self.theme.hidden_frames)
1135+
style_if_possible(&buf, self.theme.hidden_frames)
11351136
)?;
11361137
};
11371138
}

color-eyre/src/fmt.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Module for new types that isolate complext formatting
22
use std::fmt;
3-
4-
use owo_colors::OwoColorize;
3+
use crate::style_if_possible;
54

65
pub(crate) struct LocationSection<'a>(
76
pub(crate) Option<&'a std::panic::Location<'a>>,
@@ -13,9 +12,9 @@ impl fmt::Display for LocationSection<'_> {
1312
let theme = self.1;
1413
// If known, print panic location.
1514
if let Some(loc) = self.0 {
16-
write!(f, "{}", loc.file().style(theme.panic_file))?;
15+
write!(f, "{}", style_if_possible(loc.file(), theme.panic_file))?;
1716
write!(f, ":")?;
18-
write!(f, "{}", loc.line().style(theme.panic_line_number))?;
17+
write!(f, "{}", style_if_possible(&loc.line(), theme.panic_line_number))?;
1918
} else {
2019
write!(f, "<unknown>")?;
2120
}

color-eyre/src/handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
config::BacktraceFormatter,
33
section::help::HelpInfo,
4+
style_if_possible,
45
writers::{EnvSection, WriterExt},
56
Handler,
67
};
@@ -63,7 +64,7 @@ impl eyre::EyreHandler for Handler {
6364

6465
for (n, error) in errors() {
6566
writeln!(f)?;
66-
write!(indented(f).ind(n), "{}", self.theme.error.style(error))?;
67+
write!(indented(f).ind(n), "{}", style_if_possible(error, self.theme.error))?;
6768
}
6869

6970
let mut separated = f.header("\n\n");

color-eyre/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,16 @@
359359

360360
use std::sync::Arc;
361361

362+
use anstream::AutoStream;
362363
use backtrace::Backtrace;
363364
pub use eyre;
364365
#[doc(hidden)]
365366
pub use eyre::Report;
366367
#[doc(hidden)]
367368
pub use eyre::Result;
368369
pub use owo_colors;
370+
use owo_colors::OwoColorize;
371+
use owo_colors::Style;
369372
use section::help::HelpInfo;
370373
#[doc(hidden)]
371374
pub use section::Section as Help;
@@ -458,3 +461,17 @@ pub enum ErrorKind<'a> {
458461
pub fn install() -> Result<(), crate::eyre::Report> {
459462
config::HookBuilder::default().install()
460463
}
464+
465+
/// Apply owo_colors style if possible. Returns a string with/without ANSI
466+
/// escape symbols.
467+
fn style_if_possible<S>(str: S, style: Style) -> String
468+
where
469+
S: ToString + std::fmt::Display,
470+
{
471+
let color_choice = AutoStream::choice(&std::io::stderr());
472+
473+
match color_choice {
474+
anstream::ColorChoice::Never => str.to_string(),
475+
_ => str.style(style).to_string(),
476+
}
477+
}

0 commit comments

Comments
 (0)