mirror of
https://github.com/verigak/progress.git
synced 2025-12-10 12:15:34 +00:00
Add hide/show cursor support
This commit is contained in:
@@ -26,6 +26,7 @@ class Bar(WritelnMixin, Progress):
|
|||||||
bar_suffix = '| '
|
bar_suffix = '| '
|
||||||
empty_fill = ' '
|
empty_fill = ' '
|
||||||
fill = '#'
|
fill = '#'
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
filled_length = int(self.width * self.progress)
|
filled_length = int(self.width * self.progress)
|
||||||
|
|||||||
@@ -20,18 +20,22 @@ from .helpers import WriteMixin
|
|||||||
|
|
||||||
class Counter(WriteMixin, Infinite):
|
class Counter(WriteMixin, Infinite):
|
||||||
message = ''
|
message = ''
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.write(str(self.index))
|
self.write(str(self.index))
|
||||||
|
|
||||||
|
|
||||||
class Countdown(WriteMixin, Progress):
|
class Countdown(WriteMixin, Progress):
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.write(str(self.remaining))
|
self.write(str(self.remaining))
|
||||||
|
|
||||||
|
|
||||||
class Stack(WriteMixin, Progress):
|
class Stack(WriteMixin, Progress):
|
||||||
phases = (u' ', u'▁', u'▂', u'▃', u'▄', u'▅', u'▆', u'▇', u'█')
|
phases = (u' ', u'▁', u'▂', u'▃', u'▄', u'▅', u'▆', u'▇', u'█')
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
nphases = len(self.phases)
|
nphases = len(self.phases)
|
||||||
|
|||||||
@@ -15,7 +15,13 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
|
||||||
|
HIDE_CURSOR = '\x1b[?25l'
|
||||||
|
SHOW_CURSOR = '\x1b[?25h'
|
||||||
|
|
||||||
|
|
||||||
class WriteMixin(object):
|
class WriteMixin(object):
|
||||||
|
hide_cursor = False
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
def __init__(self, message=None, **kwargs):
|
||||||
super(WriteMixin, self).__init__(**kwargs)
|
super(WriteMixin, self).__init__(**kwargs)
|
||||||
self._width = 0
|
self._width = 0
|
||||||
@@ -23,6 +29,8 @@ class WriteMixin(object):
|
|||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
if self.file.isatty():
|
if self.file.isatty():
|
||||||
|
if self.hide_cursor:
|
||||||
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
print(self.message, end='', file=self.file)
|
print(self.message, end='', file=self.file)
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
@@ -33,13 +41,21 @@ class WriteMixin(object):
|
|||||||
self._width = max(self._width, len(s))
|
self._width = max(self._width, len(s))
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
|
def finish(self):
|
||||||
|
if self.file.isatty() and self.hide_cursor:
|
||||||
|
print(SHOW_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
|
|
||||||
class WritelnMixin(object):
|
class WritelnMixin(object):
|
||||||
|
hide_cursor = False
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
def __init__(self, message=None, **kwargs):
|
||||||
super(WritelnMixin, self).__init__(**kwargs)
|
super(WritelnMixin, self).__init__(**kwargs)
|
||||||
if message:
|
if message:
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
if self.file.isatty() and self.hide_cursor:
|
||||||
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
def clearln(self):
|
def clearln(self):
|
||||||
if self.file.isatty():
|
if self.file.isatty():
|
||||||
@@ -54,3 +70,5 @@ class WritelnMixin(object):
|
|||||||
def finish(self):
|
def finish(self):
|
||||||
if self.file.isatty():
|
if self.file.isatty():
|
||||||
print(file=self.file)
|
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):
|
class Spinner(WriteMixin, Infinite):
|
||||||
message = ''
|
message = ''
|
||||||
phases = ('-', '\\', '|', '/')
|
phases = ('-', '\\', '|', '/')
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
i = self.index % len(self.phases)
|
i = self.index % len(self.phases)
|
||||||
|
|||||||
Reference in New Issue
Block a user