[PATCH 1/1] log: clear any progress information before logging

0 views
Skip to first unread message

Rob Browning

unread,
Aug 24, 2025, 12:37:13 PMAug 24
to bup-...@googlegroups.com
When a \r based progress line is pending, clear it via ANSI "Erase in
Line" before logging so we can't end up with a trailing progress
remainder, cf.

progress: clear to end-of-line when appropriate
536d1b5875cf53b0d51b38168592b641244b9b5d

Signed-off-by: Rob Browning <r...@defaultvalue.org>
Tested-by: Rob Browning <r...@defaultvalue.org>
---

Proposed for main.

lib/bup/io.py | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/bup/io.py b/lib/bup/io.py
index b93de5de..d4e0116b 100644
--- a/lib/bup/io.py
+++ b/lib/bup/io.py
@@ -23,10 +23,15 @@ def _hard_write(fd, buf):
buf = buf[sz:]


+_clear_line_seq = b'\x1b[0K'
_last_prog = 0
+_last_progress = ''
+
def log(s):
"""Print a log message to stderr."""
global _last_prog
+ if _last_prog and _last_progress.endswith('\r'):
+ _hard_write(sys.stderr.fileno(), _clear_line_seq)
sys.stdout.flush()
_hard_write(sys.stderr.fileno(), s if isinstance(s, bytes) else s.encode())
_last_prog = 0
@@ -45,13 +50,13 @@ def debug2(s):

istty1 = os.isatty(1) or (int(os.environ.get('BUP_FORCE_TTY', 0)) & 1)
istty2 = os.isatty(2) or (int(os.environ.get('BUP_FORCE_TTY', 0)) & 2)
-_last_progress = ''
+
def progress(s):
"""Calls log() if stderr is a TTY. Does nothing otherwise."""
global _last_progress
if istty2:
if _last_progress.endswith('\r'):
- log('\x1b[0K')
+ _hard_write(sys.stderr.fileno(), _clear_line_seq)
log(s)
_last_progress = s

--
2.47.2

Reply all
Reply to author
Forward
0 new messages