How to colorize cargo's output with a pager
2021-02-13
Techtl;dr This worked for me (iTerm2 on Mac)
$ cargo --color always build | less -R 2>&1
Problem
When I use lv (or bat) to read the output of cargo build, the output is not colored, even with --color always (i.e. $ cargo --color always build |& lv).
https://twitter.com/yhara/status/1360518450012016643
The Cause
By saving the output to file, I found that cargo and rustc are working correctly -- both emits ANSI escape sequence. However, it seems that rustc uses codes like ESC[38;5;12m, which (I heard) is an extension of ANSI and not supported by all softwares.
https://twitter.com/yhara/status/1360532292079292418
Solution
I found that less -R can pass through the extended code. So the answer is
$ cargo --color always build |& less -R
And you can also use bat by
$ cargo --color always build |& bat --pager='less -R'
(I see no reason to do that though.)