Commit dfb0c190 authored by Paul Wise's avatar Paul Wise Committed by Chris Lamb

Clear the progress bar after completion. (Closes: #901758)

Handle terminals that do not support erasing the line by
filling the terminal with spaces.

Ignore output devices that are not terminals.

Implements: https://bugs.debian.org/901758
Requires: Python 3.2 for shutil.get_terminal_size
parent 3ee43e17
Pipeline #12565 failed with stage
in 11 minutes and 32 seconds
......@@ -3,6 +3,7 @@
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2016 Chris Lamb <lamby@debian.org>
# Copyright © 2018 Paul Wise <pabs@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -20,6 +21,7 @@
import os
import sys
import json
import signal
import logging
logger = logging.getLogger(__name__)
......@@ -215,10 +217,38 @@ class ProgressBar(object):
# Remove after https://github.com/niltonvolpato/python-progressbar/pull/57 is fixed.
kwargs.setdefault('fd', sys.stderr)
super().__init__(*args, **kwargs)
# Terminal handling after parent init since that sets self.fd
if self.fd.isatty():
from curses import tigetstr, setupterm
setupterm()
self.erase_to_eol = tigetstr('el')
else:
self.erase_to_eol = None
def _need_update(self):
return True
def erase_line(self):
if self.erase_to_eol:
self.fd.buffer.write(self.erase_to_eol)
elif self.fd.isatty():
from shutil import get_terminal_size
width = get_terminal_size().columns
print(end='\r', file=self.fd)
print(' ' * width, end='', file=self.fd)
else:
# Do not flush if nothing was written
return
self.fd.flush()
def finish(self):
self.finished = True
self.update(self.maxval)
# Clear the progress bar after completion
self.erase_line()
if self.signal_set:
signal.signal(signal.SIGWINCH, signal.SIG_DFL)
self.bar = OurProgressBar(widgets=(
' ',
progressbar.Bar(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment