netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: "Štěpán Němec" <snemec@redhat.com>
Cc: netfilter-devel@vger.kernel.org, Pablo Neira Ayuso <pablo@netfilter.org>
Subject: Re: [PATCH iptables] iptables-test.py: print with color escapes only when stdout isatty
Date: Fri, 3 Sep 2021 14:52:50 +0200	[thread overview]
Message-ID: <20210903125250.GK7616@orbyte.nwl.cc> (raw)
In-Reply-To: <20210902113307.2368834-1-snemec@redhat.com>

Hi,

On Thu, Sep 02, 2021 at 01:33:07PM +0200, Štěpán Němec wrote:
> When the output doesn't go to a terminal (typical case: log files),
> the escape sequences are just noise.
> 
> Signed-off-by: Štěpán Němec <snemec@redhat.com>

Acked-by: Phil Sutter <phil@nwl.cc>

With one minor nit:

> diff --git a/iptables-test.py b/iptables-test.py
> index 90e07feed365..e8fc0c75a43e 100755
> --- a/iptables-test.py
> +++ b/iptables-test.py
> @@ -32,22 +32,25 @@ EXTENSIONS_PATH = "extensions"
>  LOGFILE="/tmp/iptables-test.log"
>  log_file = None
>  
> +STDOUT_IS_TTY = sys.stdout.isatty()
>  
> -class Colors:
> -    HEADER = '\033[95m'
> -    BLUE = '\033[94m'
> -    GREEN = '\033[92m'
> -    YELLOW = '\033[93m'
> -    RED = '\033[91m'
> -    ENDC = '\033[0m'
> +def maybe_colored(color, text):
> +    terminal_sequences = {
> +        'green': '\033[92m',
> +        'red': '\033[91m',
> +    }
> +
> +    return (
> +        terminal_sequences[color] + text + '\033[0m' if STDOUT_IS_TTY else text
> +    )

I would "simplify" this into:

| if not sys.stdout.isatty():
| 	return text
| return terminal_sequences[color] + text + '\033[0m'

But what's beautiful in C might be ugly in Python and I'm blind to that
aspect. ;)

Cheers, Phil

  reply	other threads:[~2021-09-03 12:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-02 11:33 [PATCH iptables] iptables-test.py: print with color escapes only when stdout isatty Štěpán Němec
2021-09-03 12:52 ` Phil Sutter [this message]
2021-09-03 14:44   ` Štěpán Němec
2021-09-03 15:34     ` Phil Sutter
2021-09-06  9:04       ` Štěpán Němec
2021-09-13 15:05         ` Phil Sutter
2021-09-14  9:03           ` Štěpán Němec
2021-09-14 11:25             ` Phil Sutter
2021-09-14 11:49               ` Štěpán Němec

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=20210903125250.GK7616@orbyte.nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=snemec@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).