mirror of
https://github.com/verigak/progress.git
synced 2025-12-08 19:33:24 +00:00
Add hide/show cursor support
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user