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

Migrate to Anstyle #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Migrate to Anstyle #7

wants to merge 3 commits into from

Conversation

Pat-Lafon
Copy link
Contributor

This is a first pass at moving away from term. It compiles but I haven't actually looked too much into what styling happens.

One nice thing is that now many of the bounds are now based on the standard library like std::io::{Write, Result} instead of Terminal + ?Sized or term::Result.

The way styles are applied is a little different, but all of the colors and most of the styles are basically the same. Note Standout which i think is equivalent to anstyle::Effects::BOLD | anstyle::Effects::INVERT but not 100% clear on that.

This crate has very few tests which makes these kinds of updates a little risky imo though I don't think the diff is too big that we can't go through it thoroughly and maybe add some insta snapshot testing.

@Pat-Lafon
Copy link
Contributor Author

@lalrpop/maintainers Looking for a review and also noting that I don't seem to have write access despite being a lalrpop member... that's fine but hopefully someone active does for when this is good to go.

@Pat-Lafon
Copy link
Contributor Author

Will close #4 and a pre-req for lalrpop/lalrpop#856


macro_rules! fg_color {
($color:expr, $term_color:ident) => {
if self.contains($color) {
if term.supports_color() {
Copy link

@yannham yannham May 27, 2024

Choose a reason for hiding this comment

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

I think this functionality is lost in this PR. That is, if the thing we output to isn't a proper terminal (a file), we used to not add any styling, while we probably now always output ANSI escape codes - which can be annoying when redirecting stderr to a log file.

The simplest approximation is to use a bound std::io::Write + std::io::IsTerminal, and use is_terminal() as a replacement for supports_color() (I don't know if it's an exact replacement but I've seen is_terminal being used for that in practice).

Copy link
Contributor

Choose a reason for hiding this comment

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

The approach you mention is also the official recommendation of the termcolor library, which seems like a relevant endorsement: https://docs.rs/termcolor/latest/termcolor/#detecting-presence-of-a-terminal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, fair point, I didn't know about std::io::IsTerminal. I think for Lalrpop, the functionality was maintained because there I started using AutoStream/StripStream which manage this for me. I pushed the change that has been suggested here. It does break my lalrpop implementation because AutoStream/StripStream don't implement std::io::IsTerminal. It might be related to IsTerminal being a sealed trait? Some trait shenanigans I think.

Maybe I don't need Anstream on the Lalrpop side if all of the style/color checking can happen here?

@nikomatsakis
Copy link
Contributor

@Pat-Lafon you (and other @lalrpop/maintainers) should now have write access, sorry about that

dburgener added a commit to dburgener/ascii-canvas that referenced this pull request Sep 4, 2024
This new version has some bugfixes and improvements, and greatly reduces
the transitive dependencies.

Dropping the term dependency entirely
(lalrpop#7) is still something we
should do, but this is an easy improvement in the meantime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants