mirror of
https://github.com/verigak/progress.git
synced 2025-12-08 19:33:24 +00:00
Fold the mixins in __init__.py
The 2 mixins were almost the same, simplify the codebase by merging them in.
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
from __future__ import division
|
from __future__ import division, print_function
|
||||||
|
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
@@ -23,12 +23,17 @@ from time import time
|
|||||||
|
|
||||||
__version__ = '1.4'
|
__version__ = '1.4'
|
||||||
|
|
||||||
|
HIDE_CURSOR = '\x1b[?25l'
|
||||||
|
SHOW_CURSOR = '\x1b[?25h'
|
||||||
|
|
||||||
|
|
||||||
class Infinite(object):
|
class Infinite(object):
|
||||||
file = stderr
|
file = stderr
|
||||||
sma_window = 10 # Simple Moving Average window
|
sma_window = 10 # Simple Moving Average window
|
||||||
|
check_tty = True
|
||||||
|
hide_cursor = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, message='', **kwargs):
|
||||||
self.index = 0
|
self.index = 0
|
||||||
self.start_ts = time()
|
self.start_ts = time()
|
||||||
self.avg = 0
|
self.avg = 0
|
||||||
@@ -37,6 +42,15 @@ class Infinite(object):
|
|||||||
for key, val in kwargs.items():
|
for key, val in kwargs.items():
|
||||||
setattr(self, key, val)
|
setattr(self, key, val)
|
||||||
|
|
||||||
|
self._width = 0
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
if self.hide_cursor:
|
||||||
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
|
print(self.message, end='', file=self.file)
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
if key.startswith('_'):
|
if key.startswith('_'):
|
||||||
return None
|
return None
|
||||||
@@ -61,8 +75,31 @@ class Infinite(object):
|
|||||||
def start(self):
|
def start(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def clearln(self):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
print('\r\x1b[K', end='', file=self.file)
|
||||||
|
|
||||||
|
def write(self, s):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
line = self.message + s.ljust(self._width)
|
||||||
|
print('\r' + line, end='', file=self.file)
|
||||||
|
self._width = max(self._width, len(s))
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
|
def writeln(self, line):
|
||||||
|
if self.file and self.is_tty():
|
||||||
|
self.clearln()
|
||||||
|
print(line, end='', file=self.file)
|
||||||
|
self.file.flush()
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
pass
|
if self.file and self.is_tty():
|
||||||
|
print(file=self.file)
|
||||||
|
if self.hide_cursor:
|
||||||
|
print(SHOW_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
|
def is_tty(self):
|
||||||
|
return self.file.isatty() if self.check_tty else True
|
||||||
|
|
||||||
def next(self, n=1):
|
def next(self, n=1):
|
||||||
now = time()
|
now = time()
|
||||||
|
|||||||
@@ -19,18 +19,15 @@ from __future__ import unicode_literals
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import Progress
|
from . import Progress
|
||||||
from .helpers import WritelnMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Bar(WritelnMixin, Progress):
|
class Bar(Progress):
|
||||||
width = 32
|
width = 32
|
||||||
message = ''
|
|
||||||
suffix = '%(index)d/%(max)d'
|
suffix = '%(index)d/%(max)d'
|
||||||
bar_prefix = ' |'
|
bar_prefix = ' |'
|
||||||
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)
|
||||||
|
|||||||
@@ -16,27 +16,20 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from . import Infinite, Progress
|
from . import Infinite, Progress
|
||||||
from .helpers import WriteMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Counter(WriteMixin, Infinite):
|
class Counter(Infinite):
|
||||||
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(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(Progress):
|
||||||
phases = (' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
|
phases = (' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█')
|
||||||
hide_cursor = True
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
nphases = len(self.phases)
|
nphases = len(self.phases)
|
||||||
|
|||||||
@@ -12,76 +12,6 @@
|
|||||||
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
|
|
||||||
HIDE_CURSOR = '\x1b[?25l'
|
|
||||||
SHOW_CURSOR = '\x1b[?25h'
|
|
||||||
|
|
||||||
|
|
||||||
class WriteMixin(object):
|
|
||||||
check_tty = True
|
|
||||||
hide_cursor = False
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
super(WriteMixin, self).__init__(**kwargs)
|
|
||||||
self._width = 0
|
|
||||||
if message:
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
if self.file and self.is_tty():
|
|
||||||
if self.hide_cursor:
|
|
||||||
print(HIDE_CURSOR, end='', file=self.file)
|
|
||||||
print(self.message, end='', file=self.file)
|
|
||||||
self.file.flush()
|
|
||||||
|
|
||||||
def write(self, s):
|
|
||||||
if self.file and self.is_tty():
|
|
||||||
line = self.message + s.ljust(self._width)
|
|
||||||
print('\r' + line, end='', file=self.file)
|
|
||||||
self._width = max(self._width, len(s))
|
|
||||||
self.file.flush()
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
if self.file and self.is_tty() and self.hide_cursor:
|
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
def is_tty(self):
|
|
||||||
return self.file.isatty() if self.check_tty else True
|
|
||||||
|
|
||||||
|
|
||||||
class WritelnMixin(object):
|
|
||||||
check_tty = True
|
|
||||||
hide_cursor = False
|
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
|
||||||
super(WritelnMixin, self).__init__(**kwargs)
|
|
||||||
if message:
|
|
||||||
self.message = message
|
|
||||||
|
|
||||||
if self.file and self.is_tty() and self.hide_cursor:
|
|
||||||
print(HIDE_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
def clearln(self):
|
|
||||||
if self.file and self.is_tty():
|
|
||||||
print('\r\x1b[K', end='', file=self.file)
|
|
||||||
|
|
||||||
def writeln(self, line):
|
|
||||||
if self.file and self.is_tty():
|
|
||||||
self.clearln()
|
|
||||||
print(line, end='', file=self.file)
|
|
||||||
self.file.flush()
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
if self.file and self.is_tty():
|
|
||||||
print(file=self.file)
|
|
||||||
if self.hide_cursor:
|
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
|
||||||
|
|
||||||
def is_tty(self):
|
|
||||||
return self.file.isatty() if self.check_tty else True
|
|
||||||
|
|
||||||
|
|
||||||
from signal import signal, SIGINT
|
from signal import signal, SIGINT
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,9 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from . import Infinite
|
from . import Infinite
|
||||||
from .helpers import WriteMixin
|
|
||||||
|
|
||||||
|
|
||||||
class Spinner(WriteMixin, Infinite):
|
class Spinner(Infinite):
|
||||||
message = ''
|
|
||||||
phases = ('-', '\\', '|', '/')
|
phases = ('-', '\\', '|', '/')
|
||||||
hide_cursor = True
|
hide_cursor = True
|
||||||
|
|
||||||
@@ -40,5 +38,6 @@ class MoonSpinner(Spinner):
|
|||||||
class LineSpinner(Spinner):
|
class LineSpinner(Spinner):
|
||||||
phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻']
|
phases = ['⎺', '⎻', '⎼', '⎽', '⎼', '⎻']
|
||||||
|
|
||||||
|
|
||||||
class PixelSpinner(Spinner):
|
class PixelSpinner(Spinner):
|
||||||
phases = ['⣾','⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
|
phases = ['⣾', '⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽']
|
||||||
|
|||||||
@@ -35,12 +35,10 @@ for bar_cls in (IncrementalBar, PixelBar, ShadyBar):
|
|||||||
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
|
for spin in (Spinner, PieSpinner, MoonSpinner, LineSpinner, PixelSpinner):
|
||||||
for i in spin(spin.__name__ + ' ').iter(range(100)):
|
for i in spin(spin.__name__ + ' ').iter(range(100)):
|
||||||
sleep()
|
sleep()
|
||||||
print()
|
|
||||||
|
|
||||||
for singleton in (Counter, Countdown, Stack, Pie):
|
for singleton in (Counter, Countdown, Stack, Pie):
|
||||||
for i in singleton(singleton.__name__ + ' ').iter(range(100)):
|
for i in singleton(singleton.__name__ + ' ').iter(range(100)):
|
||||||
sleep()
|
sleep()
|
||||||
print()
|
|
||||||
|
|
||||||
bar = IncrementalBar('Random', suffix='%(index)d')
|
bar = IncrementalBar('Random', suffix='%(index)d')
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
|
|||||||
Reference in New Issue
Block a user