From: Stephen Hemminger <stephen@networkplumber.org>
To: David Ahern <dsahern@gmail.com>
Cc: Phil Sutter <phil@nwl.cc>,
netdev@vger.kernel.org, Till Maas <opensource@till.name>
Subject: Re: [iproute PATCH 4/4] lib: Enable colored output only for TTYs
Date: Wed, 15 Aug 2018 08:04:06 -0700 [thread overview]
Message-ID: <20180815080406.7110bd76@xeon-e3> (raw)
In-Reply-To: <8f48ba0a-ba36-67f8-8dee-f0d86b72e8e0@gmail.com>
On Wed, 15 Aug 2018 08:40:20 -0600
David Ahern <dsahern@gmail.com> wrote:
> On 8/15/18 3:06 AM, Phil Sutter wrote:
> > Add an additional prerequisite to check_enable_color() to make sure
> > stdout actually points to an open TTY device. Otherwise calls like
> >
> > | ip -color a s >/tmp/foo
> >
> > will print color escape sequences into that file.
> >
> > Signed-off-by: Phil Sutter <phil@nwl.cc>
> > ---
> > lib/color.c | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/color.c b/lib/color.c
> > index edf96e5c6ecd7..500ba09682697 100644
> > --- a/lib/color.c
> > +++ b/lib/color.c
> > @@ -3,6 +3,7 @@
> > #include <stdarg.h>
> > #include <stdlib.h>
> > #include <string.h>
> > +#include <unistd.h>
> > #include <sys/socket.h>
> > #include <sys/types.h>
> > #include <linux/if.h>
> > @@ -79,7 +80,7 @@ void enable_color(void)
> >
> > int check_enable_color(int color, int json)
> > {
> > - if (color && !json) {
> > + if (color && !json && isatty(fileno(stdout))) {
> > enable_color();
> > return 0;
> > }
> >
>
> This also disables color sequence when the output is piped to a pager
> such as less which with the -R argument can handle it just fine.
>
> ie., the user needs to remove the color arg when that output is not wanted.
If you are going to change the color enabling, why not make it compatible
with what ls does?
From man ls(1) (and grep)
--color[=WHEN]
colorize the output; WHEN can be 'always' (default if omitted),
'auto', or 'never'; more info below
...
Using color to distinguish file types is disabled both by default and
with --color=never. With --color=auto, ls emits color codes only when
standard output is connected to a terminal. The LS_COLORS environment
variable can change the settings. Use the dircolors command to set it.
Make -c be same as --color=always to keep compatiablity
next prev parent reply other threads:[~2018-08-15 17:56 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-15 9:06 [iproute PATCH 0/4] A bunch of fixes regarding colored output Phil Sutter
2018-08-15 9:06 ` [iproute PATCH 1/4] tc: Fix typo in check for " Phil Sutter
2018-08-15 9:06 ` [iproute PATCH 2/4] bridge: Fix " Phil Sutter
2018-08-15 9:06 ` [iproute PATCH 3/4] Merge common code for conditionally " Phil Sutter
2018-08-15 9:06 ` [iproute PATCH 4/4] lib: Enable colored output only for TTYs Phil Sutter
2018-08-15 14:40 ` David Ahern
2018-08-15 15:04 ` Stephen Hemminger [this message]
2018-08-15 15:34 ` David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180815080406.7110bd76@xeon-e3 \
--to=stephen@networkplumber.org \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=opensource@till.name \
--cc=phil@nwl.cc \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.