[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Format interpreter lines individually
commit 7553e1e9d25c93de31981180800c3a1edea01aa4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sat Jul 30 10:47:35 2016 -0700
Format interpreter lines individually
Applying terminal formatting to each line of the tor output individually rather
than the whole multi-line message. This doesn't matter for our interpreter, but
makes the output much easier for nyx to work with.
---
docs/change_log.rst | 1 +
stem/interpreter/__init__.py | 10 +++++-----
stem/util/term.py | 12 +++++++++++-
test/unit/interpreter/commands.py | 5 +++--
test/unit/util/term.py | 2 ++
5 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/docs/change_log.rst b/docs/change_log.rst
index 839c7c9..4857b04 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -92,6 +92,7 @@ The following are only available within Stem's `git repository
* Additional information when :func:`~stem.util.system.call` fails through a :class:`~stem.util.system.CallError`
* Added **stem.util.system.SYSTEM_CALL_TIME** with the total time spent on system calls
* Added an **is_ipv6** value to :class:`~stem.util.connection.Connection` instances
+ * Added LINES attribute to :data:`~stem.util.term.Attr`
* Added :func:`~stem.util.system.pids_by_user`
* Added :func:`~stem.util.connection.address_to_int`
* Added :func:`~stem.util.term.encoding`
diff --git a/stem/interpreter/__init__.py b/stem/interpreter/__init__.py
index 0b24894..5031900 100644
--- a/stem/interpreter/__init__.py
+++ b/stem/interpreter/__init__.py
@@ -28,11 +28,11 @@ __all__ = [
PROMPT = format('>>> ', Color.GREEN, Attr.BOLD, Attr.READLINE_ESCAPE)
-STANDARD_OUTPUT = (Color.BLUE, )
-BOLD_OUTPUT = (Color.BLUE, Attr.BOLD)
-HEADER_OUTPUT = (Color.GREEN, )
-HEADER_BOLD_OUTPUT = (Color.GREEN, Attr.BOLD)
-ERROR_OUTPUT = (Attr.BOLD, Color.RED)
+STANDARD_OUTPUT = (Color.BLUE, Attr.LINES)
+BOLD_OUTPUT = (Color.BLUE, Attr.BOLD, Attr.LINES)
+HEADER_OUTPUT = (Color.GREEN, Attr.LINES)
+HEADER_BOLD_OUTPUT = (Color.GREEN, Attr.BOLD, Attr.LINES)
+ERROR_OUTPUT = (Attr.BOLD, Color.RED, Attr.LINES)
settings_path = os.path.join(os.path.dirname(__file__), 'settings.cfg')
uses_settings = stem.util.conf.uses_settings('stem_interpreter', settings_path)
diff --git a/stem/util/term.py b/stem/util/term.py
index a32fe93..1940b9e 100644
--- a/stem/util/term.py
+++ b/stem/util/term.py
@@ -33,6 +33,9 @@ Utilities for working with the terminal.
Terminal text attributes.
+ .. versionchanged:: 1.5.0
+ Added the LINES attribute.
+
=================== ===========
Attr Description
=================== ===========
@@ -40,6 +43,7 @@ Utilities for working with the terminal.
**HIGHLIGHT** 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>`_
+ **LINES** formats lines individually
=================== ===========
"""
@@ -55,7 +59,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', 'HIGHLIGHT', 'READLINE_ESCAPE')
+Attr = stem.util.enum.Enum('BOLD', 'UNDERLINE', 'HIGHLIGHT', 'READLINE_ESCAPE', 'LINES')
# mappings of terminal attribute enums to their ANSI escape encoding
FG_ENCODING = dict([(list(Color)[i], str(30 + i)) for i in range(8)])
@@ -123,6 +127,12 @@ def format(msg, *attr):
if DISABLE_COLOR_SUPPORT:
return msg
+ if Attr.LINES in attr:
+ attr = list(attr)
+ attr.remove(Attr.LINES)
+ lines = [format(line, *attr) for line in msg.split('\n')]
+ return '\n'.join(lines)
+
# if we have reset sequences in the message then apply our attributes
# after each of them
diff --git a/test/unit/interpreter/commands.py b/test/unit/interpreter/commands.py
index 30407ba..1fe52f7 100644
--- a/test/unit/interpreter/commands.py
+++ b/test/unit/interpreter/commands.py
@@ -174,7 +174,8 @@ class TestInterpretorCommands(unittest.TestCase):
interpreter = ControlInterpretor(controller)
- self.assertEqual('\x1b[34m%s\x1b[0m\n' % response, interpreter.run_command('GETINFO version'))
+ self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version'))
+ self.assertEqual('\x1b[34m250-version=0.2.5.1-alpha-dev (git-245ecfff36c0cecc)\r\x1b[0m\n\x1b[34m250 OK\x1b[0m\n', interpreter.run_command('GETINFO version'))
controller.msg.assert_called_with('GETINFO version')
controller.msg.side_effect = stem.ControllerError('kaboom!')
@@ -188,7 +189,7 @@ class TestInterpretorCommands(unittest.TestCase):
interpreter = ControlInterpretor(controller)
- self.assertEqual('\x1b[34m%s\x1b[0m\n' % response, interpreter.run_command('GETCONF log address'))
+ self.assertEqual('\x1b[34m250-Log=notice stdout\r\x1b[0m\n\x1b[34m250 Address\x1b[0m\n', interpreter.run_command('GETCONF log address'))
controller.msg.assert_called_with('GETCONF log address')
def test_setevents(self):
diff --git a/test/unit/util/term.py b/test/unit/util/term.py
index 758db22..130b1ae 100644
--- a/test/unit/util/term.py
+++ b/test/unit/util/term.py
@@ -27,4 +27,6 @@ class TestTerminal(unittest.TestCase):
self.assertEqual('hi!', stem.util.term.format('hi!'))
self.assertEqual('\x1b[31mhi!\x1b[0m', stem.util.term.format('hi!', Color.RED))
self.assertEqual('\x1b[31;1mhi!\x1b[0m', stem.util.term.format('hi!', Color.RED, Attr.BOLD))
+ self.assertEqual('\x1b[31mhi\nthere!\x1b[0m', stem.util.term.format('hi\nthere!', Color.RED))
+ self.assertEqual('\x1b[31mhi\x1b[0m\n\x1b[31mthere!\x1b[0m', stem.util.term.format('hi\nthere!', Color.RED, Attr.LINES))
self.assertEqual('\001\x1b[31m\002hi!\001\x1b[0m\002', stem.util.term.format('hi!', Color.RED, Attr.READLINE_ESCAPE))
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits