I have several pet peeves. One I consider to be a programming sin.
Lets imagine, you are asked to integrate a library into your system. You plug it in, call the required API and you run your test executable to see if it works. Well, you see a wall of white. WHITE….
Of course, the library assumes that the terminal has a dark background and sets foreground colour of its stdout to WHITE. WHYYYYYYYY Well, sinners aside, how do we handle this on our end besides fixing the damn loggers.
Simple enough, strip the ANSI colour codes from the executable by piping the output through a ANSI stripper.
Here are a few ways of doing so.
Option 1:
ansi2txt
from colorized-log. Its a small enough c program that does a single thing.
This is my preferred option. I just stole the c file and dumped a binary into my dotfiles.
Option 2:
Ansifilter, seems a bit too powerful for this usecase. But also does the job.
Option 3:
Pipe through tee
, it generally does a good job at stripping ansi sequences
Option 4:
SED IS THERE SOMETHING THIS DOES NOT DO. <3
STUPID_CMD | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g"
Option 5:
TR. My dear translate. This solves the issue by replacing all characters that are not either printable or whitespace
This is another elagent solution, if I didn’t have the custom ansi2txt
program, this would probably be the way I would go.
STUPID_CMD | tr -dc '[[:print:][:space:]]'
Options 6: Novel solution for macos
This seems a bit convoluted, but you could use the clipboard to do it for you.
STUPID_CMD | pbcopy && pbpaste