From f0502fb8278f20e432695e46d884b80770b76d1b Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:21:41 +0530 Subject: [PATCH 1/2] Preserve prefix and cursor when cleaning erase-right sequences --- humanfriendly/terminal/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/humanfriendly/terminal/__init__.py b/humanfriendly/terminal/__init__.py index ba9739c..8fd5db9 100644 --- a/humanfriendly/terminal/__init__.py +++ b/humanfriendly/terminal/__init__.py @@ -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. @@ -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 [#]_. @@ -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 From 865b477908c4517fd89c0a074af39fdec9a2e830 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Sun, 22 Nov 2026 13:23:12 +0530 Subject: [PATCH 2/2] Test captured progress output with default and explicit erase-right --- humanfriendly/tests.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 72dad99..d3fce30 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -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()