diff --git a/progress/bar.py b/progress/bar.py index 264b6b1..311b47a 100644 --- a/progress/bar.py +++ b/progress/bar.py @@ -26,6 +26,7 @@ class Bar(WritelnMixin, Progress): bar_suffix = '| ' empty_fill = ' ' fill = '#' + hide_cursor = True def update(self): filled_length = int(self.width * self.progress) diff --git a/progress/counter.py b/progress/counter.py index 1d5de89..caaddc6 100644 --- a/progress/counter.py +++ b/progress/counter.py @@ -20,18 +20,22 @@ from .helpers import WriteMixin class Counter(WriteMixin, Infinite): message = '' + hide_cursor = True def update(self): self.write(str(self.index)) class Countdown(WriteMixin, Progress): + hide_cursor = True + def update(self): self.write(str(self.remaining)) class Stack(WriteMixin, Progress): phases = (u' ', u'▁', u'▂', u'▃', u'▄', u'▅', u'▆', u'▇', u'█') + hide_cursor = True def update(self): nphases = len(self.phases) diff --git a/progress/helpers.py b/progress/helpers.py index 48194f7..08f8b61 100644 --- a/progress/helpers.py +++ b/progress/helpers.py @@ -15,7 +15,13 @@ from __future__ import print_function +HIDE_CURSOR = '\x1b[?25l' +SHOW_CURSOR = '\x1b[?25h' + + class WriteMixin(object): + hide_cursor = False + def __init__(self, message=None, **kwargs): super(WriteMixin, self).__init__(**kwargs) self._width = 0 @@ -23,6 +29,8 @@ class WriteMixin(object): self.message = message if self.file.isatty(): + if self.hide_cursor: + print(HIDE_CURSOR, end='', file=self.file) print(self.message, end='', file=self.file) self.file.flush() @@ -33,13 +41,21 @@ class WriteMixin(object): self._width = max(self._width, len(s)) self.file.flush() + def finish(self): + if self.file.isatty() and self.hide_cursor: + print(SHOW_CURSOR, end='', file=self.file) + class WritelnMixin(object): + hide_cursor = False + def __init__(self, message=None, **kwargs): super(WritelnMixin, self).__init__(**kwargs) if message: self.message = message + if self.file.isatty() and self.hide_cursor: + print(HIDE_CURSOR, end='', file=self.file) def clearln(self): if self.file.isatty(): @@ -54,3 +70,5 @@ class WritelnMixin(object): def finish(self): if self.file.isatty(): print(file=self.file) + if self.hide_cursor: + print(SHOW_CURSOR, end='', file=self.file) diff --git a/progress/spinner.py b/progress/spinner.py index 88e3c61..969bfbb 100644 --- a/progress/spinner.py +++ b/progress/spinner.py @@ -21,6 +21,7 @@ from .helpers import WriteMixin class Spinner(WriteMixin, Infinite): message = '' phases = ('-', '\\', '|', '/') + hide_cursor = True def update(self): i = self.index % len(self.phases)