use stdout but do nothing if redirected

This commit is contained in:
Martin Diehl 2023-12-09 14:40:58 +01:00
parent 7063c59b6a
commit 5d07851d60
1 changed files with 7 additions and 8 deletions

View File

@ -930,8 +930,8 @@ class ProgressBar:
self.time_start = self.time_last_update = _datetime.datetime.now() self.time_start = self.time_last_update = _datetime.datetime.now()
self.fraction_last = 0.0 self.fraction_last = 0.0
_sys.stderr.write(f"{self.prefix} {''*self.bar_length} 0% ETA n/a") if _sys.stdout.isatty():
_sys.stderr.flush() _sys.stdout.write(f"{self.prefix} {''*self.bar_length} 0% ETA n/a")
def update(self, def update(self,
iteration: int) -> None: iteration: int) -> None:
@ -945,11 +945,10 @@ class ProgressBar:
remaining_time = (_datetime.datetime.now() - self.time_start) \ remaining_time = (_datetime.datetime.now() - self.time_start) \
* (self.total - (iteration+1)) / (iteration+1) * (self.total - (iteration+1)) / (iteration+1)
remaining_time -= _datetime.timedelta(microseconds=remaining_time.microseconds) # remove μs remaining_time -= _datetime.timedelta(microseconds=remaining_time.microseconds) # remove μs
_sys.stderr.write(f'\r{self.prefix} {bar} {fraction:>4.0%} ETA {remaining_time}') if _sys.stdout.isatty():
_sys.stderr.flush() _sys.stdout.write(f'\r{self.prefix} {bar} {fraction:>4.0%} ETA {remaining_time}')
self.fraction_last = fraction self.fraction_last = fraction
if iteration == self.total - 1: if iteration == self.total - 1 and _sys.stdout.isatty():
_sys.stderr.write('\n') _sys.stdout.write('\n')
_sys.stderr.flush()