From df33c164de7d73323814ac439a394173d662e366 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 17 Oct 2018 13:46:08 +0100 Subject: [PATCH 1/3] Only colourise synctl output when attached to tty --- synctl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/synctl b/synctl index 09b64459b1..111b9c1816 100755 --- a/synctl +++ b/synctl @@ -48,7 +48,12 @@ def pid_running(pid): def write(message, colour=NORMAL, stream=sys.stdout): - if colour == NORMAL: + # Lets check if we're writing to a TTY before colouring + should_colour = False + if stream in (sys.stdout, sys.stderr): + should_colour = stream.isatty() + + if not should_colour or colour == NORMAL: stream.write(message + "\n") else: stream.write(colour + message + NORMAL + "\n") From 1af16acd4ca40b15183ae0a25ffcbc13c36c130d Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 17 Oct 2018 13:48:09 +0100 Subject: [PATCH 2/3] Newsfile --- changelog.d/4049.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4049.misc diff --git a/changelog.d/4049.misc b/changelog.d/4049.misc new file mode 100644 index 0000000000..4370d9dfa6 --- /dev/null +++ b/changelog.d/4049.misc @@ -0,0 +1 @@ +Only colourise synctl output when attached to tty From a5aea15a6b0f4c389a243bbb3ac375c9ed128146 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 17 Oct 2018 17:06:49 +0100 Subject: [PATCH 3/3] Assume isatty is always defined, and catch AttributeError. Also don't bother checking colour==Normal --- synctl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/synctl b/synctl index 111b9c1816..4bd4c68b37 100755 --- a/synctl +++ b/synctl @@ -50,10 +50,14 @@ def pid_running(pid): def write(message, colour=NORMAL, stream=sys.stdout): # Lets check if we're writing to a TTY before colouring should_colour = False - if stream in (sys.stdout, sys.stderr): + try: should_colour = stream.isatty() + except AttributeError: + # Just in case `isatty` isn't defined on everything. The python + # docs are incredibly vague. + pass - if not should_colour or colour == NORMAL: + if not should_colour: stream.write(message + "\n") else: stream.write(colour + message + NORMAL + "\n")