]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
sambatool: heuristics to decided whether colour is wanted
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Thu, 19 Apr 2018 02:15:25 +0000 (14:15 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 31 May 2018 02:34:52 +0000 (04:34 +0200)
The easy cases are --color=yes and --color=no.

With --color=auto, we use color if it seems we're writing to a TTY.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Thu May 31 04:34:52 CEST 2018 on sn-devel-144

python/samba/netcmd/__init__.py

index 9df096d5bd3e15b72f4c9c7eabdf2e7a6a5820c8..9b144d97f43fe1da36af1afd2e9cfb2de7616a46 100644 (file)
@@ -18,6 +18,7 @@
 
 import optparse, samba
 from samba import getopt as options
+from samba import colour
 from ldb import LdbError
 import sys, traceback
 import textwrap
@@ -189,6 +190,29 @@ class Command(object):
         logger.addHandler(logging.StreamHandler(self.errf))
         return logger
 
+    def apply_colour_choice(self, requested):
+        """Heuristics to work out whether the user wants colour output, from a
+        --color=yes|no|auto option. This alters the ANSI 16 bit colour
+        "constants" in the colour module to be either real colours or empty
+        strings.
+        """
+        requested = requested.lower()
+        if requested == 'no':
+            colour.switch_colour_off()
+
+        elif requested == 'yes':
+            colour.switch_colour_on()
+
+        elif requested == 'auto':
+            if (hasattr(self.outf, 'isatty') and self.outf.isatty()):
+                colour.switch_colour_on()
+            else:
+                colour.switch_colour_off()
+
+        else:
+            raise CommandError("Unknown --color option: %s "
+                               "please choose from yes, no, auto")
+
 
 class SuperCommand(Command):
     """A samba-tool command with subcommands."""