1
0
mirror of https://github.com/samba-team/samba.git synced 2025-12-14 20:23:54 +03:00

py:colour: is_colour_wanted() can take filenames

We need this for `samba-tool visualize -o -` which means output to
stdout, and which has always had a tty test for colour. Rather than
continue to duplicate the full logic there, we can reuse this.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Douglas Bagnall
2022-09-10 16:55:48 +12:00
committed by Andrew Bartlett
parent c0d0c13670
commit adf8b8b4a1

View File

@@ -93,6 +93,9 @@ def xterm_256_colour(n, bg=False, bold=False):
def is_colour_wanted(*streams, hint='auto'):
"""The hint is presumably a --color argument.
The streams to be considered can be file objects or file names,
with '-' being a special filename indicating stdout.
We follow the behaviour of GNU `ls` in what we accept.
* `git` is stricter, accepting only {always,never,auto}.
* `grep` is looser, accepting mixed case variants.
@@ -117,6 +120,15 @@ def is_colour_wanted(*streams, hint='auto'):
return False
for stream in streams:
if isinstance(stream, str):
# This function can be passed filenames instead of file
# objects, in which case we treat '-' as stdout, and test
# that. Any other string is not regarded as a tty.
if stream != '-':
return False
import sys
stream = sys.stdout
if not stream.isatty():
return False
return True