From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: Re: [iproute PATCH 4/4] lib: Enable colored output only for TTYs Date: Wed, 15 Aug 2018 08:04:06 -0700 Message-ID: <20180815080406.7110bd76@xeon-e3> References: <20180815090610.16646-1-phil@nwl.cc> <20180815090610.16646-5-phil@nwl.cc> <8f48ba0a-ba36-67f8-8dee-f0d86b72e8e0@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Cc: Phil Sutter , netdev@vger.kernel.org, Till Maas To: David Ahern Return-path: Received: from mail-pg1-f195.google.com ([209.85.215.195]:41475 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729391AbeHOR4j (ORCPT ); Wed, 15 Aug 2018 13:56:39 -0400 Received: by mail-pg1-f195.google.com with SMTP id z8-v6so625246pgu.8 for ; Wed, 15 Aug 2018 08:04:09 -0700 (PDT) In-Reply-To: <8f48ba0a-ba36-67f8-8dee-f0d86b72e8e0@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: On Wed, 15 Aug 2018 08:40:20 -0600 David Ahern 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 > >=20 > > | ip -color a s >/tmp/foo > >=20 > > will print color escape sequences into that file. > >=20 > > Signed-off-by: Phil Sutter > > --- > > lib/color.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > >=20 > > 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 > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -79,7 +80,7 @@ void enable_color(void) > > =20 > > int check_enable_color(int color, int json) > > { > > - if (color && !json) { > > + if (color && !json && isatty(fileno(stdout))) { > > enable_color(); > > return 0; > > } > > =20 >=20 > 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. >=20 > ie., the user needs to remove the color arg when that output is not wante= d. If you are going to change the color enabling, why not make it compatible with what ls does? =46rom man ls(1) (and grep) --color[=3DWHEN] colorize the output; WHEN can be 'always' (default if omitte= d), 'auto', or 'never'; more info below ... Using color to distinguish file types is disabled both by default = and with --color=3Dnever. With --color=3Dauto, ls emits color codes onl= y when standard output is connected to a terminal. The LS_COLORS environm= ent variable can change the settings. Use the dircolors command to set = it. Make -c be same as --color=3Dalways to keep compatiablity