Move the computation of the terminal line eraser into

Signed-off-by: Mattia Rizzolo's avatarMattia Rizzolo <>
parent 8c2b0797
......@@ -17,10 +17,27 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <>.
import sys
import contextlib
import logging
def line_ereser(fd=sys.stderr) -> bytes:
ereser = b'' # avoid None to avoid 'NoneType + str/bytes' failures
if fd.isatty():
from curses import tigetstr, setupterm
ereser = tigetstr('el')
if not ereser and fd.isatty():
# is a tty, but doesn't support the proper scape code, so let's fake it
from shutil import get_terminal_size
width = get_terminal_size().columns
ereser = b'\r{}\r'.format(b' ' * width)
return ereser
def setup_logging(debug, log_handler):
logger = logging.getLogger()
......@@ -24,6 +24,9 @@ import json
import signal
import logging
from .logging import line_ereser
logger = logging.getLogger(__name__)
......@@ -215,12 +218,7 @@ class ProgressBar(object):
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
self.erase_to_eol = tigetstr('el')
self.erase_to_eol = None
self.erase_to_eol = line_ereser(self.fd)
def _need_update(self):
return True
......@@ -228,13 +226,7 @@ class ProgressBar(object):
def erase_line(self):
if self.erase_to_eol:
elif self.fd.isatty():
print(end='\r', file=self.fd)
print(' ' * self.term_width, end='', file=self.fd)
# Do not flush if nothing was written
def finish(self):
self.finished = True
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