Skip to content

Commit

Permalink
strip ANSI esc sequences from logs (#117)
Browse files Browse the repository at this point in the history
Related #67

Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
  • Loading branch information
TomasTomecek authored Jan 4, 2017
1 parent c20f781 commit f5c450e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
61 changes: 60 additions & 1 deletion sen/tui/widgets/list/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
import threading
import traceback

Expand All @@ -11,6 +12,62 @@
logger = logging.getLogger(__name__)


# def translate_asci_sequence(s):
# # FIXME: not finished
# translation_map = {
# "34": "dark blue"
# }
# return translation_map.get(s, "")


def strip_from_ansi_esc_sequences(text):
"""
find ANSI escape sequences in text and remove them
:param text: str
:return: list, should be passed to ListBox
"""
# esc[ + values + control character
# h, l, p commands are complicated, let's ignore them
seq_regex = r"\x1b\[[0-9;]*[mKJusDCBAfH]"
regex = re.compile(seq_regex)
start = 0
response = ""
for match in regex.finditer(text):
end = match.start()
response += text[start:end]

start = match.end()
response += text[start:len(text)]
return response


# def colorize_text(text):
# """
# finds ANSI color escapes in text and transforms them to urwid
#
# :param text: str
# :return: list, should be passed to ListBox
# """
# # FIXME: not finished
# response = []
# # http://ascii-table.com/ansi-escape-sequences.php
# regex_pattern = r"(?:\x1b\[(\d+)?(?:;(\d+))*m)([^\x1b]+)" # [%d;%d;...m
# regex = re.compile(regex_pattern, re.UNICODE)
# for match in regex.finditer(text):
# groups = match.groups()
# t = groups[-1]
# color_specs = groups[:-1]
# urwid_spec = translate_asci_sequence(color_specs)
# if urwid_spec:
# item = (urwid.AttrSpec(urwid_spec, "main_list_dg"), t)
# else:
# item = t
# item = urwid.AttrMap(urwid.Text(t, align="left", wrap="any"), "main_list_dg", "main_list_white")
# response.append(item)
# return response


class ScrollableListBox(WidgetBase):
def __init__(self, ui, text, focus_bottom=True):
self.walker = urwid.SimpleFocusListWalker([])
Expand All @@ -25,9 +82,11 @@ def __init__(self, ui, text, focus_bottom=True):
def set_text(self, text):
self.walker.clear()
text = _ensure_unicode(text)
# logger.debug(repr(text))
text = strip_from_ansi_esc_sequences(text)
list_of_texts = text.split("\n")
self.walker[:] = [
urwid.AttrMap(urwid.Text(t, align="left", wrap="any"), "main_list_dg", "main_list_white")
urwid.AttrMap(urwid.Text(t.strip(), align="left", wrap="any"), "main_list_dg", "main_list_white")
for t in list_of_texts
]

Expand Down
33 changes: 33 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from flexmock import flexmock

from sen.tui.widgets.list.common import strip_from_ansi_esc_sequences
from sen.util import _ensure_unicode, log_traceback, repeater, humanize_time, \
OrderedSet

Expand Down Expand Up @@ -121,3 +122,35 @@ def test_ordered_set():
assert s == [1, 2]
s.append(1)
assert s == [2, 1]


# @pytest.mark.parametrize("inp,expected", [
# ("aaa", ["aaa"]),
# (
# "qwe [01;34m asd [01;33mbnm",
# ["qwe ", " asd ", "bnm"]
# )
# ])
# def test_colorize_text(inp, expected):
# got = colorize_text(inp)
# for idx, c in enumerate(got):
# if isinstance(c, str):
# assert expected[idx] == c
# else:
# assert expected[idx] == c[1]


@pytest.mark.parametrize("inp,expected", [
("aaa", "aaa"),
(
"root:x:0:0:root:/root:/bin/b\x1b[01;31m\x1b[Ka\x1b[m\x1b[Ksh",
"root:x:0:0:root:/root:/bin/bash"
),
(
"\x1b[0m\x1b[01;36mbin\x1b[0m\r\n\x1b[01;34mboot\x1b[0m\r\n\x1b[01;34mdev\x1b[0m\r\n",
"bin\r\nboot\r\ndev\r\n"
)
])
def test_strip_from_ansi_seqs(inp, expected):
got = strip_from_ansi_esc_sequences(inp)
assert got == expected

0 comments on commit f5c450e

Please sign in to comment.