-
-
Notifications
You must be signed in to change notification settings - Fork 245
Change ansi_term
to anstyle
#176
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
Conversation
Thank you very much! In some of my other repositories, it was suggested to switch to nu-ansi-term. What are the differences? |
That's what
You can use |
To be honest, I just picked the first option that was suggested on the advisory. From a cursory glance, Currently, none of these libraries expose direct access to the color codes, instead returning an |
Oh, I didn't know about this! This is very useful and I should have been using it when benchmarking performance to begin with...
Here's proof for the claims I made about random and text data. |
Cool - thank you for the benchmark results. Is this not ready for review yet? |
I wanted to test a different way of writing the Color prefixes that doesn't use |
This PR is ready to merge. It will probably cause conflicts with #170 so I think it would be best to merge that first, then I can make changes to this one so it does not conflict. |
Thank you, sounds good. |
#170 has been merged now. Thank you for your help @sharifhsn. |
Thank you very much. This looks great. I know this will be next-to-impossible to properly test automatically (because the ANSI escape sequence output changed), but I trust you did some manual checks to make sure that the output still looks the same, color-wise? |
Yes, I checked on both Linux and Windows (I don't have access to macOS). |
The
ansi_term
crate has been declared deprecated, so I've replaced it with the maintained crate anstyle which is recommended. This PR does three things:Replaces
ansi_term
withanstyle
wherever necessary.Fixes 256-color still in use #156 by using 4-bit ANSI colors everywhere. The gray color looks exactly the same as when using
BrightBlack
, so there should be no need to introduce multiple color modes.Increases performance significantly when printing with color.
As a quick review on how ANSI color codes work in the terminal, there are prefix codes that change the color/style, and a suffix code that resets the color/style to the original.
Previously, every single element would be colored with its own prefix and suffix, regardless of whether the next element had the same color or not. This meant that colored output wrote many more characters to the screen than was strictly necessary.
This PR writes the color prefix only when the color has changed, and writes the reset suffix only when a separator is reached (since separators are not colored).
In the best-case where most or all of the file is the same color e.g. a text file, this is almost 3x faster in real time. In the worst-case where the color changes often e.g. random data, this is almost 2x faster in real time.
hyperfine
does not show this because it pipes all output to/dev/null
, but this effect is apparent if you use thetime
command in the terminal. Thehyperfine
measurement is significantly slower, but as you have noted this is an artificial measurement.hexyl randomdata
target/release/hexyl randomdata
In the real terminal the results are different, which you can test yourself using
time
.