3 ms·
> One way to solve this nicely, that seems to be getting more common, is to use `isatty()` to check if the output is a terminal and if so print with decorations
by badsectoracula 5y ago
> One way to solve this nicely, that seems to be getting more common, is to use `isatty()` to check if the output is a terminal and if so print with decorations, otherwise leave them away.
This is not a good idea because it can lead to surprising the user (ls's behavior is actually bad from that perspective). For example you run a program
$ foo
ID Thing What
4 Cat Mews
2 Dog Woofs
5 Canary Tweets
...then you run the result through sort and trying to avoid the header...
$ foo | tail -n +2 | sort
...except instead of the expected result you get...
2 Dog Woofs
5 Canary Tweets
...because the program tried to be smart instead of consistent. This is also against the GNU guidelines as mentioned elsewhere.
- grumbel 5y agoThe little surprise is worth the general improvement in usability (e.g. colors, progress bars, filenames you can copy&paste, terminal not getting corrupted by escape sequences, etc). It also makes it clear that the terminal output is for user interaction, so programs no longer have to be both UI and API at the same time, they can focus on one or the other, making both much better and cleaner as a result. > ...then you run the result through sort and trying to avoid the header... The much more common scenario would be doing `foo | sort` and then ending up with random header text in the sorted data. Few people will add a `tail` the first time they type that command or remember do it every time they use it interactively. With `isatty()` it behaves as the user expects it right from the start.