Change `ansi_term` to `anstyle` by sharifhsn · Pull Request #176 · sharkdp/hexyl

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:

  1. Replaces ansi_term with anstyle wherever necessary.

  2. 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.

  3. 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 the time command in the terminal. The hyperfine measurement is significantly slower, but as you have noted this is an artificial measurement.

Command Mean [ms] Min [ms] Max [ms] Relative
hexyl randomdata 209.8 ± 4.6 203.3 223.4 1.09 ± 0.06
target/release/hexyl randomdata 192.3 ± 10.4 184.0 227.9 1.00

In the real terminal the results are different, which you can test yourself using time.