DEV Community

Discussion on: Finding the other end of a pipe on Linux

Collapse
 
joshcheek profile image
Josh Cheek

I've always done it by checking whether the output is a tty (eg). If you really wanted to be legit, then that's not good enough, you have to then read the terminal databases (b/c different terminals have different capabilities and different escape codes for a given ability), but that gets painful pretty quickly, this approach gets you really far really fast.

Collapse
 
hoelzro profile image
Rob Hoelz

I probably should have provided a more concrete example in my post, but I'm interested in finding out if the program on the other side of a pipe supports color, rather than if my terminal does. Here's an example of what I was going for:

$ ls --color=auto | less # doesn't support color

versus

$ ls --color=auto | less -R # supports color
Collapse
 
jselbie profile image
John Selbie • Edited

Rob,

How many programs, aside from less, actually support color codes read from stdin?

Thread Thread
 
hoelzro profile image
Rob Hoelz

I had less in mind when thinking about this, but I suppose any pager. There's also filter programs like nl and ts that just annotate their input - a whitelist approach would probably work pretty well. One idea I kind of like is modifying ack to detect if it's talking to another ack, and having the second ack change its color to highlight different patterns in the output.

Looking through my notes on this, there's a lot of potential application beyond color - you could have a program tell another what encoding it's using (hey, my input is UTF-16LE!) so it doesn't need to guess, or you could have an implementation of uniq that checks to see if its input is coming from sort or not.

Thread Thread
 
joshcheek profile image
Josh Cheek

Omg, I never knew about less -R!!