Skip to content

Commit 3e18f93

Browse files
dnicolodirgommers
authored andcommitted
BUG: fix CLICounter terminal output
On my terminal emulator the previous solution always duplicate the last character of the last line printed by CLICounter. This simplifies the implementation and fixes the issue. Instead than overwriting the previous line with white space, this implementation uses the ANSI escape character to ask the terminal emulator to clear the line. The emulation for this on the Windows console is supported by colorama, which we already initialize.
1 parent d05cf79 commit 3e18f93

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

mesonpy/_util.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import contextlib
1010
import gzip
11+
import itertools
1112
import os
1213
import sys
1314
import tarfile
@@ -73,22 +74,18 @@ def create_targz(path: Path) -> Iterator[Tuple[tarfile.TarFile, Optional[int]]]:
7374
class CLICounter:
7475
def __init__(self, total: int) -> None:
7576
self._total = total - 1
76-
self._count = -1
77-
self._current_line = ''
77+
self._count = itertools.count()
7878

7979
def update(self, description: str) -> None:
80-
self._count += 1
81-
new_line = f'[{self._count}/{self._total}] {description}'
80+
line = f'[{next(self._count)}/{self._total}] {description}'
8281
if sys.stdout.isatty():
83-
pad_size = abs(len(self._current_line) - len(new_line))
84-
print(' ' + new_line + ' ' * pad_size, end='\r', flush=True)
82+
print('\r', line, sep='', end='\33[0K', flush=True)
8583
else:
86-
print(new_line)
87-
self._current_line = new_line
84+
print(line)
8885

8986
def finish(self) -> None:
9087
if sys.stdout.isatty():
91-
print(f'\r{self._current_line}')
88+
print()
9289

9390

9491
@contextlib.contextmanager

0 commit comments

Comments
 (0)