From 12e46ed70209bdc3bb8ca1f0416039883b94e4e1 Mon Sep 17 00:00:00 2001 From: Georgios Verigakis Date: Mon, 20 Jul 2020 13:22:48 +0300 Subject: [PATCH] Do not use ANSI to clear line --- progress/__init__.py | 18 +++++++++--------- test_progress.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/progress/__init__.py b/progress/__init__.py index dd094ba..0e0560c 100644 --- a/progress/__init__.py +++ b/progress/__init__.py @@ -47,15 +47,14 @@ class Infinite(object): for key, val in kwargs.items(): setattr(self, key, val) - self._width = 0 + self._max_width = 0 self.message = message if self.file and self.is_tty(): if self.hide_cursor: print(HIDE_CURSOR, end='', file=self.file) atexit.register(self.finish) - print(self.message, end='', file=self.file) - self.file.flush() + self.writeln('') def __getitem__(self, key): if key.startswith('_'): @@ -87,14 +86,15 @@ class Infinite(object): def start(self): pass - def clearln(self): - if self.file and self.is_tty(): - print('\r\x1b[K', end='', file=self.file) - def writeln(self, line): if self.file and self.is_tty(): - self.clearln() - print(line, end='', file=self.file) + width = len(line) + if width < self._max_width: + # Add padding to cover previous contents + line += ' ' * (self._max_width - width) + else: + self._max_width = width + print('\r' + line, end='', file=self.file) self.file.flush() def finish(self): diff --git a/test_progress.py b/test_progress.py index c15d350..4c61bf0 100755 --- a/test_progress.py +++ b/test_progress.py @@ -33,7 +33,7 @@ for bar_cls in (IncrementalBar, PixelBar, ShadyBar): sleep() for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner): - for i in spin(spin.__name__ + ' %(index) 3d ').iter(range(100)): + for i in spin(spin.__name__ + ' %(index)d ').iter(range(100)): sleep() for singleton in (Counter, Countdown, Stack, Pie):