[tor-commits] [stem/master] Fix interaction of color prompt with readline

atagar at torproject.org atagar at torproject.org
Sun May 25 20:12:32 UTC 2014


commit d699f816ea1f05d085e52f8adfa75f09442c1d81
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun May 25 13:06:22 2014 -0700

    Fix interaction of color prompt with readline
    
    For a long while we've had an issue where readline history would break with
    long inputs. For instance, entering...
    
      >>> the quick brown fox jumped over the very nice fence and ran around in pretty fields
    
    ... then pressing 'up' to autocomplete history then 'down' to go back to an
    empty prompt would leave a fragment of this mingled with the prompt.
    
    This turned out to be an issue with how readline calculates column width.
    Readline included the formatting sequences when determining our prompts width,
    which can be corrected by wrapping all encodings with RL_PROMPT_START_IGNORE
    and RL_PROMPT_END_IGNORE sequences...
    
      https://stackoverflow.com/questions/9468435/look-how-to-fix-column-calculation-in-python-readline-if-use-color-prompt
    
    Many thanks to Yawning for pointing this out!
---
 stem/interpreter/__init__.py |    9 +--------
 stem/util/term.py            |   25 ++++++++++++++++---------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py
index 23453a3..6d6fea1 100644
--- a/stem/interpreter/__init__.py
+++ b/stem/interpreter/__init__.py
@@ -20,14 +20,7 @@ import stem.util.term
 
 from stem.util.term import RESET, Attr, Color, format
 
-# Our color prompt triggers a bug between raw_input() and readline history,
-# where scrolling through history widens our prompt. Widening our prompt via
-# invisible characters (like resets) seems to sidestep this bug for short
-# inputs. Contrary to the ticket, this still manifests with python 2.7.1...
-#
-#   http://bugs.python.org/issue12972
-
-PROMPT = format('>>> ', Color.GREEN, Attr.BOLD) + RESET * 10
+PROMPT = format('>>> ', Color.GREEN, Attr.BOLD, Attr.READLINE_ESCAPE)
 
 STANDARD_OUTPUT = (Color.BLUE, )
 BOLD_OUTPUT = (Color.BLUE, Attr.BOLD)
diff --git a/stem/util/term.py b/stem/util/term.py
index 72d82b7..42e3fc4 100644
--- a/stem/util/term.py
+++ b/stem/util/term.py
@@ -32,13 +32,14 @@ Utilities for working with the terminal.
 
   Enumerations of terminal text attributes.
 
-  ============= ===========
-  Attr          Description
-  ============= ===========
-  **BOLD**      heavy typeface
-  **HILIGHT**   inverted foreground and background
-  **UNDERLINE** underlined text
-  ============= ===========
+  =================== ===========
+  Attr                Description
+  =================== ===========
+  **BOLD**            heavy typeface
+  **HILIGHT**         inverted foreground and background
+  **UNDERLINE**       underlined text
+  **READLINE_ESCAPE** wrap encodings in `RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE sequences <https://stackoverflow.com/questions/9468435/look-how-to-fix-column-calculation-in-python-readline-if-use-color-prompt>`_
+  =================== ===========
 """
 
 import stem.util.enum
@@ -53,7 +54,7 @@ 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')
+Attr = stem.util.enum.Enum('BOLD', 'UNDERLINE', 'HILIGHT', 'READLINE_ESCAPE')
 
 # mappings of terminal attribute enums to their ANSI escape encoding
 FG_ENCODING = dict([(list(Color)[i], str(30 + i)) for i in range(8)])
@@ -104,6 +105,12 @@ def format(msg, *attr):
       encodings.append(encoding)
 
   if encodings:
-    return (CSI % ';'.join(encodings)) + msg + RESET
+    prefix, suffix = CSI % ';'.join(encodings), RESET
+
+    if Attr.READLINE_ESCAPE in attr:
+      prefix = '\001%s\002' % prefix
+      suffix = '\001%s\002' % suffix
+
+    return prefix + msg + suffix
   else:
     return msg



More information about the tor-commits mailing list