Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owo color #2914

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Expand Up @@ -39,7 +39,7 @@ bytes = "1.2.0"
argh = "0.1.8"

# sloggish example
nu-ansi-term = "0.46.0"
owo-colors = "4.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has a very low MSRV (1.56), so it's a fine choice https://github.com/jam1garner/owo-colors/blob/master/Cargo.toml

humantime = "2.1.0"
log = "0.4.17"

Expand Down
39 changes: 10 additions & 29 deletions examples/examples/sloggish/sloggish_collector.rs
@@ -1,4 +1,4 @@
use nu_ansi_term::{Color, Style};
use owo_colors::OwoColorize;
use tracing::{
field::{Field, Visit},
Collect, Id, Level, Metadata,
Expand Down Expand Up @@ -79,13 +79,12 @@ struct ColorLevel<'a>(&'a Level);
impl<'a> fmt::Display for ColorLevel<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self.0 {
Level::TRACE => Color::Purple.paint("TRACE"),
Level::DEBUG => Color::Blue.paint("DEBUG"),
Level::INFO => Color::Green.paint("INFO "),
Level::WARN => Color::Yellow.paint("WARN "),
Level::ERROR => Color::Red.paint("ERROR"),
Level::TRACE => "TRACE".purple().fmt(f),
Level::DEBUG => "DEBUG".blue().fmt(f),
Level::INFO => "INFO".green().fmt(f),
Level::WARN => "WARN".yellow().fmt(f),
stefnotch marked this conversation as resolved.
Show resolved Hide resolved
Level::ERROR => "ERROR".red().fmt(f),
}
.fmt(f)
}
}

Expand Down Expand Up @@ -117,21 +116,9 @@ impl<'a> Visit for Event<'a> {
.unwrap();
let name = field.name();
if name == "message" {
write!(
&mut self.stderr,
"{}",
// Have to alloc here due to `nu_ansi_term`'s API...
Style::new().bold().paint(format!("{:?}", value))
)
.unwrap();
write!(&mut self.stderr, "{:?}", value.bold()).unwrap();
} else {
write!(
&mut self.stderr,
"{}: {:?}",
Style::new().bold().paint(name),
value
)
.unwrap();
write!(&mut self.stderr, "{}: {:?}", name.bold(), value).unwrap();
}
self.comma = true;
}
Expand Down Expand Up @@ -162,16 +149,10 @@ impl SloggishCollector {
{
let mut kvs = kvs.into_iter();
if let Some((k, v)) = kvs.next() {
write!(
writer,
"{}{}: {}",
leading,
Style::new().bold().paint(k.as_ref()),
v
)?;
write!(writer, "{}{}: {}", leading, k.as_ref().bold(), v)?;
}
for (k, v) in kvs {
write!(writer, ", {}: {}", Style::new().bold().paint(k.as_ref()), v)?;
write!(writer, ", {}: {}", k.as_ref().bold(), v)?;
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions tracing-subscriber/Cargo.toml
Expand Up @@ -29,7 +29,7 @@ alloc = ["tracing-core/alloc"]
std = ["alloc", "tracing-core/std"]
env-filter = ["matchers", "regex", "once_cell", "tracing", "std", "thread_local"]
fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
ansi = ["fmt", "owo-colors"]
registry = ["sharded-slab", "thread_local", "std"]
json = ["tracing-serde", "serde", "serde_json"]

Expand All @@ -49,7 +49,7 @@ once_cell = { optional = true, version = "1.13.0" }

# fmt
tracing-log = { path = "../tracing-log", version = "0.2", optional = true, default-features = false, features = ["log-tracer", "std"] }
nu-ansi-term = { version = "0.46.0", optional = true }
owo-colors = { version = "4.0", optional = true }
time = { version = "0.3.2", features = ["formatting"], optional = true }

# only required by the json feature
Expand Down
38 changes: 16 additions & 22 deletions tracing-subscriber/src/filter/env/builder.rs
Expand Up @@ -210,45 +210,39 @@ impl Builder {
}

if !disabled.is_empty() {
#[cfg(feature = "nu_ansi_term")]
use nu_ansi_term::{Color, Style};
#[cfg(feature = "ansi")]
use owo_colors::{OwoColorize, XtermColors};
// NOTE: We can't use a configured `MakeWriter` because the EnvFilter
// has no knowledge of any underlying subscriber or collector, which
// may or may not use a `MakeWriter`.
let warn = |msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "ansi"))]
let msg = format!("warning: {}", msg);
#[cfg(feature = "nu_ansi_term")]
let msg = {
let bold = Style::new().bold();
let mut warning = Color::Yellow.paint("warning");
warning.style_ref_mut().is_bold = true;
format!("{}{} {}", warning, bold.paint(":"), bold.paint(msg))
};
#[cfg(feature = "ansi")]
let msg = { format!("{}{} {}", "warning".yellow().bold(), ":".bold(), msg.bold()) };
stefnotch marked this conversation as resolved.
Show resolved Hide resolved
eprintln!("{}", msg);
};
let ctx_prefixed = |prefix: &str, msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "ansi"))]
let msg = format!("{} {}", prefix, msg);
#[cfg(feature = "nu_ansi_term")]
#[cfg(feature = "ansi")]
let msg = {
stefnotch marked this conversation as resolved.
Show resolved Hide resolved
let mut equal = Color::Fixed(21).paint("="); // dark blue
equal.style_ref_mut().is_bold = true;
format!(" {} {} {}", equal, Style::new().bold().paint(prefix), msg)
format!(
" {} {} {}",
"=".color(XtermColors::Blue).bold(),
prefix.bold(),
msg
)
};
eprintln!("{}", msg);
};
let ctx_help = |msg| ctx_prefixed("help:", msg);
let ctx_note = |msg| ctx_prefixed("note:", msg);
let ctx = |msg: &str| {
#[cfg(not(feature = "nu_ansi_term"))]
#[cfg(not(feature = "ansi"))]
let msg = format!("note: {}", msg);
#[cfg(feature = "nu_ansi_term")]
let msg = {
let mut pipe = Color::Fixed(21).paint("|");
pipe.style_ref_mut().is_bold = true;
format!(" {} {}", pipe, msg)
};
#[cfg(feature = "ansi")]
let msg = { format!(" {} {}", "|".color(XtermColors::Blue).bold(), msg) };
stefnotch marked this conversation as resolved.
Show resolved Hide resolved
eprintln!("{}", msg);
};
warn("some trace filter directives would enable traces that are disabled statically");
Expand Down