News for hackers. Stuff that matters.
Re:Everything you ever wanted to know about terminals (2018) by inetknght on Thursday September 10, 2020 @07:17PM (#24437604)
> “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.

Re:Everything you ever wanted to know about terminals (2018) by theamk on Thursday September 10, 2020 @09:03PM (#24438173)
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:

    grep box file.txt
    grep box file.txt | grep -v orange-box
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.
Re:Everything you ever wanted to know about terminals (2018) by inetknght on Thursday September 10, 2020 @09:58PM (#24438437)
If I wanted to see just orange-box, then I would do

    grep --color=always orange-box file.txt
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:

    grep --color=always box file.txt | grep --color=always orange-box
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

    grep --color=never orange-box file.txt > matches.txt
Alternatively, I'd add a filter to the file with `ansi2txt`

    grep --color=always box file.txt | grep --color=always box | ansi2txt > no-color-matches.txt
Re:Everything you ever wanted to know about terminals (2018) by rixed on Thursday September 10, 2020 @11:38PM (#24438945)
I think the point the person you are replying to was trying to make is that in your proposed sequence of colored greps, the second one would not catch the "orange-box"es because of the extra chars added for coloring the output of the first grep.

Maybe grep should ignore ansi sequences optionally or by default, to solve this?

Re:Everything you ever wanted to know about terminals (2018) by anitil on Thursday September 10, 2020 @08:51PM (#24438125)
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 '.')

Re:Everything you ever wanted to know about terminals (2018) by inetknght on Thursday September 10, 2020 @09:02PM (#24438167)
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