Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions humanfriendly/terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
styles. Only widely supported text styles are included here.
"""

CLEAN_OUTPUT_PATTERN = re.compile(u'(\r|\n|\b|%s)' % re.escape(ANSI_ERASE_LINE))
CLEAN_OUTPUT_PATTERN = re.compile(u'(\r|\n|\b|%s0?K)' % re.escape(ANSI_CSI))
"""
A compiled regular expression used to separate significant characters from other text.

Expand Down Expand Up @@ -334,7 +334,9 @@ def clean_terminal_output(text):

This function emulates the effect of backspace (0x08), carriage return
(0x0D) and line feed (0x0A) characters and the ANSI 'erase line' escape
sequence on interactive terminals. It's intended to clean up command output
sequence on interactive terminals. Erasing a line with ``ESC[K`` or
``ESC[0K`` removes text from the cursor to the end of the line without
moving the cursor. It's intended to clean up command output
that was originally meant to be rendered on an interactive terminal and
that has been captured using e.g. the :man:`script` program [#]_ or the
:mod:`pty` module [#]_.
Expand Down Expand Up @@ -376,11 +378,14 @@ def clean_terminal_output(text):
elif token == '\b':
# Seek back one character in the current line.
current_position = max(0, current_position - 1)
elif token in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
# Erase to the right without moving the cursor or losing the prefix.
current_line = current_line[:current_position]
else:
if token == '\n':
# Capture the current line.
cleaned_lines.append(current_line)
if token in ('\n', ANSI_ERASE_LINE):
if token == '\n':
# Clear the current line.
current_line = ''
current_position = 0
Expand Down
38 changes: 38 additions & 0 deletions humanfriendly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,44 @@ def test_clean_output(self):
# Trailing empty lines should be stripped.
assert clean_terminal_output('foo\nbar\nbaz\n\n\n') == ['foo', 'bar', 'baz']

def test_clean_output_erase_line_preserves_prefix(self):
"""Erasing to the right must not discard text before the cursor."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
assert clean_terminal_output('ready' + erase) == ['ready']

def test_clean_output_erase_line_progress(self):
"""Keep a shorter progress message after erasing the old suffix."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
text = 'progress: 100%\rdone' + erase + '\n'
assert clean_terminal_output(text) == ['done']

def test_clean_output_erase_line_cursor(self):
"""Erasing must leave the cursor at its current position."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
assert clean_terminal_output('abcdef\b\b' + erase + 'XY') == ['abcdXY']

def test_clean_output_erase_line_carriage_return(self):
"""A later carriage return can overwrite the preserved prefix."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
assert clean_terminal_output('abcdef\b\b' + erase + '\rXY') == ['XYcd']

def test_clean_output_erase_line_newline(self):
"""Keep erased lines separate from the following line."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
text = 'abcdef\rxy' + erase + '\nnext' + erase
assert clean_terminal_output(text) == ['xy', 'next']

def test_clean_output_erase_line_repeated(self):
"""Repeated erases must preserve text left of each cursor position."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
text = 'abcdef\b\b' + erase + '\b' + erase + 'Z'
assert clean_terminal_output(text) == ['abcZ']

def test_clean_output_erase_line_start(self):
"""Erasing from column zero must still clear the entire line."""
for erase in (ANSI_ERASE_LINE, ANSI_CSI + '0K'):
assert clean_terminal_output('old\r' + erase + 'new') == ['new']

def test_find_terminal_size(self):
"""Test :func:`humanfriendly.terminal.find_terminal_size()`."""
lines, columns = find_terminal_size()
Expand Down