Skip to content

Commit

Permalink
fix(debug): Improve control over colors
Browse files Browse the repository at this point in the history
This fixes a bug with `NO_COLOR` and `CLICOLOR`
  • Loading branch information
epage committed Apr 28, 2023
1 parent e945818 commit cb84bc6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 113 deletions.
78 changes: 58 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ default = ["std"]
alloc = []
std = ["alloc", "memchr?/std"]
simd = ["dep:memchr"]
debug = ["dep:anstyle", "dep:is-terminal", "dep:terminal_size", "dep:concolor"]
debug = ["dep:anstyle", "dep:is-terminal", "dep:terminal_size", "dep:anstyle-stream"]

unstable-doc = ["alloc", "std", "simd"]

[dependencies]
anstyle = { version = "0.2.5", optional = true }
anstyle = { version = "0.3.1", optional = true }
is-terminal = { version = "0.4.3", optional = true }
memchr = { version = "2.3", optional = true, default-features = false }
terminal_size = { version = "0.2.3", optional = true }
concolor = { version = "0.0.8", optional = true, features = ["auto"] }
anstyle-stream = { version = "0.2.0", optional = true }

[dev-dependencies]
doc-comment = "0.3"
Expand Down
128 changes: 38 additions & 90 deletions src/trace/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,9 @@ pub fn start<I: Stream>(
count: usize,
input: &I,
) {
let ansi_color = ansi_color();
let reset = if ansi_color {
anstyle::Reset.render().to_string()
} else {
"".to_owned()
};
let gutter_style = if ansi_color {
anstyle::Style::new().bold()
} else {
anstyle::Style::new()
}
.render();
let input_style = if ansi_color {
anstyle::Style::new().underline()
} else {
anstyle::Style::new()
}
.render();
let eof_style = if ansi_color {
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Cyan.into()))
} else {
anstyle::Style::new()
}
.render();
let gutter_style = anstyle::Style::new().bold();
let input_style = anstyle::Style::new().underline();
let eof_style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Cyan.into()));

let (call_width, input_width) = column_widths();

Expand Down Expand Up @@ -132,9 +111,18 @@ pub fn start<I: Stream>(
(debug_slice, eof)
};

let writer = std::io::stderr();
let writer = anstyle_stream::stderr();
let mut writer = writer.lock();
let _ = writeln!(writer, "{call_column:call_width$} {gutter_style}|{reset} {input_style}{debug_slice}{eof_style}{eof}{reset}");
let _ = writeln!(
writer,
"{call_column:call_width$} {gutter_style}|{gutter_reset} {input_style}{debug_slice}{input_reset}{eof_style}{eof}{eof_reset}",
gutter_style=gutter_style.render(),
gutter_reset=gutter_style.render_reset(),
input_style=input_style.render(),
input_reset=input_style.render_reset(),
eof_style=eof_style.render(),
eof_reset=eof_style.render_reset(),
);
}

pub fn end(
Expand All @@ -144,18 +132,7 @@ pub fn end(
consumed: Option<usize>,
severity: Severity,
) {
let ansi_color = ansi_color();
let reset = if ansi_color {
anstyle::Reset.render().to_string()
} else {
"".to_owned()
};
let gutter_style = if ansi_color {
anstyle::Style::new().bold()
} else {
anstyle::Style::new()
}
.render();
let gutter_style = anstyle::Style::new().bold();

let (call_width, _) = column_widths();

Expand All @@ -166,105 +143,76 @@ pub fn end(
};
let call_column = format!("{:depth$}< {name}{count}", "");

let (mut status_style, status) = match severity {
let (status_style, status) = match severity {
Severity::Success => {
let style = anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Green.into()))
.render();
let style = anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Green.into()));
let status = format!("+{}", consumed.unwrap_or_default());
(style, status)
}
Severity::Backtrack => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Yellow.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Yellow.into())),
"backtrack".to_owned(),
),
Severity::Cut => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Red.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())),
"cut".to_owned(),
),
Severity::Incomplete => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Red.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())),
"incomplete".to_owned(),
),
};
if !ansi_color {
status_style = anstyle::Style::new().render();
}

let writer = std::io::stderr();
let writer = anstyle_stream::stderr();
let mut writer = writer.lock();
let _ = writeln!(
writer,
"{status_style}{call_column:call_width$}{reset} {gutter_style}|{reset} {status_style}{status}{reset}"
"{status_style}{call_column:call_width$}{status_reset} {gutter_style}|{gutter_reset} {status_style}{status}{status_reset}",
gutter_style=gutter_style.render(),
gutter_reset=gutter_style.render_reset(),
status_style=status_style.render(),
status_reset=status_style.render_reset(),
);
}

pub fn result(depth: usize, name: &dyn crate::lib::std::fmt::Display, severity: Severity) {
let ansi_color = ansi_color();
let reset = if ansi_color {
anstyle::Reset.render().to_string()
} else {
"".to_owned()
};
let gutter_style = if ansi_color {
anstyle::Style::new().bold()
} else {
anstyle::Style::new()
}
.render();
let gutter_style = anstyle::Style::new().bold();

let (call_width, _) = column_widths();

let call_column = format!("{:depth$}| {name}", "");

let (mut status_style, status) = match severity {
let (status_style, status) = match severity {
Severity::Success => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Green.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Green.into())),
"",
),
Severity::Backtrack => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Yellow.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Yellow.into())),
"backtrack",
),
Severity::Cut => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Red.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())),
"cut",
),
Severity::Incomplete => (
anstyle::Style::new()
.fg_color(Some(anstyle::AnsiColor::Red.into()))
.render(),
anstyle::Style::new().fg_color(Some(anstyle::AnsiColor::Red.into())),
"incomplete",
),
};
if !ansi_color {
status_style = anstyle::Style::new().render();
}

let writer = std::io::stderr();
let writer = anstyle_stream::stderr();
let mut writer = writer.lock();
let _ = writeln!(
writer,
"{status_style}{call_column:call_width$}{reset} {gutter_style}|{reset} {status_style}{status}{reset}"
"{status_style}{call_column:call_width$}{status_reset} {gutter_style}|{gutter_reset} {status_style}{status}{status_reset}",
gutter_style=gutter_style.render(),
gutter_reset=gutter_style.render_reset(),
status_style=status_style.render(),
status_reset=status_style.render_reset(),
);
}

fn ansi_color() -> bool {
concolor::get(concolor::Stream::Stderr).ansi_color()
}

fn column_widths() -> (usize, usize) {
let term_width = term_width();

Expand Down

0 comments on commit cb84bc6

Please sign in to comment.