[tor-commits] [stem/master] Providing an option to disable colorized prompt output

atagar at torproject.org atagar at torproject.org
Mon May 12 06:01:26 UTC 2014


commit 87aecce52ff95c5f79759a4cd57b77280f4beefd
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun May 11 16:21:29 2014 -0700

    Providing an option to disable colorized prompt output
    
    Not everyone will probably want highly colorized output so providing an option
    to disable it.
    
    I dislike this approach of using a global in the stem.util.term module. I'm
    explicitely not vending this to others since I'm sure there's a more graceful
    way of toggling this.
---
 stem/interpretor/__init__.py  |    6 ++++++
 stem/interpretor/arguments.py |    4 ++++
 stem/interpretor/settings.cfg |    1 +
 stem/util/term.py             |    8 ++++++++
 4 files changed, 19 insertions(+)

diff --git a/stem/interpretor/__init__.py b/stem/interpretor/__init__.py
index 0e4492e..5ecd356 100644
--- a/stem/interpretor/__init__.py
+++ b/stem/interpretor/__init__.py
@@ -16,6 +16,7 @@ import stem.connection
 import stem.process
 import stem.util.conf
 import stem.util.system
+import stem.util.term
 
 from stem.util.term import RESET, Attr, Color, format
 
@@ -60,6 +61,11 @@ def main():
     print stem.interpretor.arguments.get_help()
     sys.exit()
 
+  if args.disable_color:
+    global PROMPT
+    stem.util.term.DISABLE_COLOR_SUPPORT = True
+    PROMPT = '>>> '
+
   # If the user isn't connecting to something in particular then offer to start
   # tor if it isn't running.
 
diff --git a/stem/interpretor/arguments.py b/stem/interpretor/arguments.py
index 38ef41f..278d2a0 100644
--- a/stem/interpretor/arguments.py
+++ b/stem/interpretor/arguments.py
@@ -17,6 +17,7 @@ DEFAULT_ARGS = {
   'user_provided_port': False,
   'control_socket': '/var/run/tor/control',
   'user_provided_socket': False,
+  'disable_color': False,
   'print_help': False,
 }
 
@@ -25,6 +26,7 @@ OPT = 'i:s:h'
 OPT_EXPANDED = [
   'interface=',
   'socket=',
+  'no-color',
   'help',
 ]
 
@@ -68,6 +70,8 @@ def parse(argv):
     elif opt in ('-s', '--socket'):
       args['control_socket'] = arg
       args['user_provided_socket'] = True
+    elif opt == '--no-color':
+      args['disable_color'] = True
     elif opt in ('-h', '--help'):
       args['print_help'] = True
 
diff --git a/stem/interpretor/settings.cfg b/stem/interpretor/settings.cfg
index f03ac76..b8d39dc 100644
--- a/stem/interpretor/settings.cfg
+++ b/stem/interpretor/settings.cfg
@@ -17,6 +17,7 @@ msg.help
 |  -i, --interface [ADDRESS:]PORT  change control interface from {address}:{port}
 |  -s, --socket SOCKET_PATH        attach using unix domain socket if present,
 |                                    SOCKET_PATH defaults to: {socket}
+|  --no-color                      disables colorized output
 |  -h, --help                      presents this help
 |
 
diff --git a/stem/util/term.py b/stem/util/term.py
index f390cfd..72d82b7 100644
--- a/stem/util/term.py
+++ b/stem/util/term.py
@@ -46,6 +46,11 @@ import stem.util.str_tools
 
 TERM_COLORS = ('BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAGENTA', 'CYAN', 'WHITE')
 
+# DISABLE_COLOR_SUPPORT is *not* being vended to Stem users. This is likely to
+# go away if I can think of a more graceful method for color toggling.
+
+DISABLE_COLOR_SUPPORT = False
+
 Color = stem.util.enum.Enum(*TERM_COLORS)
 BgColor = stem.util.enum.Enum(*['BG_' + color for color in TERM_COLORS])
 Attr = stem.util.enum.Enum('BOLD', 'UNDERLINE', 'HILIGHT')
@@ -78,6 +83,9 @@ def format(msg, *attr):
     attributes and ending with a reset
   """
 
+  if DISABLE_COLOR_SUPPORT:
+    return msg
+
   # if we have reset sequences in the message then apply our attributes
   # after each of them
 





More information about the tor-commits mailing list