Comment by theamk
6 years ago
That’s pretty nasty - this article is factually incorrect in probably the worst way: the code will work on author’s computer, but not for other people [0]
Not to mention that it is missing “good citizen” features like turning off colors when stdout is not a tty.
> (...) the code will work on author’s computer, but not for other people
Is it actually the case?
The comment you cite says the code won't work on xterm or gnome-terminal.
I've checked with what I had available: XTerm(330) and GNOME Terminal 3.30.1 using VTE 0.54.1. On both terminals the last code sample from the post works perfectly fine as far as I can tell.
What terminal should I use to see improper behavior of the program?
> “good citizen” features like turning off colors when stdout is not a tty.
Better solution: do not detect whether your output is a tty or not.
Let the user decide they want colors or not by using `--color=always` or `--color=never` or similar.
There's few things worse than writing a script and getting different output just because you're no longer running the program interactively.
I don't see how this is supposed to work? Let's take "grep" for example -- you want both of these to work, and print color to terminal:
Having --color=always is nice sometimes, but there is a reason grep has "--color=auto" and it is the default. If we write invisible characters to file, this breaks all sorts of tools, so it is much safer and more predictable to not include them.
If I wanted to see just orange-box, then I would do
But, I don't want to see just orange-box. I want to see orange-box and compare against other cases of box. So I would do:
Without that --color=always, other cases of box will be drowned out by the noise.
And if I wanted to write that out to a file then I would do
Alternatively, I'd add a filter to the file with `ansi2txt`
1 reply →
Gah i got bit by this with 'jq'!
> $ echo "{}" | jq > {} > $ echo "{}" | jq > out.json > jq - commandline JSON processor [version 1.5-1-a5b5cbe] ... etc ...
Turns out when stdout isn't a tty you _must_ specify a filter (in this case '.')
Yup! I had the same problem too. It was 'fixed' in jq 1.6, such that it now assumes `.` is the filter if no program arguments are provided [0].
It happens all the time with anything systemd or networkmanager too
[0]: https://github.com/stedolan/jq/releases/tag/jq-1.6
> Not to mention that it is missing “good citizen” features like turning off colors when stdout is not a tty.
It's a tutorial, not a 1000 page book. Some things won't be in there.
Besides, detecting stdout or tty isn't even relevant for interactive TUI applications.