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
Open

Owo color #2914

wants to merge 12 commits into from

Conversation

stefnotch
Copy link

@stefnotch stefnotch commented Mar 20, 2024

Motivation

Fix #2759

Also update docs to fix #2268

Solution

Replaced nu-ansi-term with owo-colors.

I then ran all the unit tests cargo test, and tested the cargo run --example sloggish and cargo run --example fmt-pretty examples. I did my best to verify that the output hasn't changed.

Questions

During development, rust-analyzer always shows me bogus error messages and I'm not entirely sure what I'm doing wrong. I frequently ended up having to work with cargo check and a bit of guesswork.
image

Old fmt-pretty output

image

New, visually unchanged fmt-pretty output

image

@stefnotch stefnotch requested review from hawkw, davidbarsky and a team as code owners March 20, 2024 12:26
}
}
}

Copy link
Author

Choose a reason for hiding this comment

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

This ended up being the relatively nice workaround for jam1garner/owo-colors#123

Comment on lines 463 to 466
self.result = if field.name() == "message" {
self.record_debug_impl(field, &format_args!("{}", self.style.paint(value)))
} else {
self.record_debug(field, &value)
self.record_debug_impl(field, &self.style.paint(value))
Copy link
Author

Choose a reason for hiding this comment

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

Is there a difference between the two calls? I wasn't sure, so I was afraid of touching this code.

@@ -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

Copy link
Contributor

@mladedav mladedav left a comment

Choose a reason for hiding this comment

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

I don't know the library, but is it possible to somehow emulate the prefix/suffix pattern?

What I think will happen with this implementation is it will print for example the equivalent of <b>name</b><b>:</b> to print the colon in the same style as name.

That said it's a few more bytes for less allocations so still a net gain, just wondering.

examples/examples/sloggish/sloggish_collector.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/env/builder.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/env/builder.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/filter/env/builder.rs Outdated Show resolved Hide resolved
tracing-subscriber/src/fmt/format/mod.rs Show resolved Hide resolved
@@ -1688,7 +1719,7 @@ pub(super) mod test {
#[cfg(feature = "ansi")]
#[test]
fn with_ansi_true() {
let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[2mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n";
let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[39m \u{1b}[2mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

From what I understand 39 sets the foreground colour to default while 0 resets everything to default. So I guess it is fine here if we don't have other modifiers, but 0 is safer if possible.

@@ -403,24 +394,62 @@ impl<'a> PrettyVisitor<'a> {
}

pub(crate) fn with_style(self, style: Style) -> Self {
Self { style, ..self }
if self.writer.has_ansi_escapes() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is correct. Cannot this be changed later by some calls like with_writer or set_ansi?

I am honestly unsure if these can be called after this point.

Copy link
Author

Choose a reason for hiding this comment

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

PrettyVisitor takes ownership of the writer, and doesn't expose the writer. So I suppose it cannot be cannot be changed.

However, I agree that this isn't ideal code. If the writer were to ever get exposed, it'd be an potential bug.

Copy link
Author

Choose a reason for hiding this comment

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

I changed it, let me know what you think.

@stefnotch
Copy link
Author

stefnotch commented Mar 26, 2024

@mladedav Thank you for the excellent review!

To answer your question, yes, it is possible to get the prefix-suffix pattern to work. I had that, and removed it in this commit
51758ee
That approach basically (ab)used a dummy struct to call the relevant owo-colors functions.

There's also an alternative approach, along the lines of

struct FormatPair<'a, 'b, A, B>(&'a A, &'b B, Style);

impl fmt::Display for FormatPair {
  // print the style prefix
  // print a
  // print b
  // print the style suffix
}

I could absolutely add one or more of those approaches, if the extra complexity is worth it.

@stefnotch
Copy link
Author

In case a reviewer is curious, I also tried out an alternative design, where a Style class also manages the is_ansi flag.
master...stefnotch:tracing:owo-color-alternate-design#diff-8f6911488e5b3b7a50b97db5ab61c50af439560e330a54015889651c63a0dc52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants