mirror of
https://github.com/verigak/progress.git
synced 2025-12-08 19:33:24 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32dc3db6f1 | ||
|
|
fc605a7217 | ||
|
|
84a67ab6cf | ||
|
|
8f69000ab6 | ||
|
|
ddab8c7a2b | ||
|
|
22dcfc24a7 |
@@ -19,12 +19,11 @@ from sys import stderr
|
||||
from time import time
|
||||
|
||||
|
||||
__version__ = '1.0.2'
|
||||
__version__ = '1.1'
|
||||
|
||||
|
||||
class Infinite(object):
|
||||
file = stderr
|
||||
avg_window = 10
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.ctx = {}
|
||||
@@ -42,8 +41,7 @@ class Infinite(object):
|
||||
# Calculate moving average
|
||||
now = time()
|
||||
dt = now - self._ts
|
||||
w = self.avg_window
|
||||
self.avg = dt if self.avg else (dt + w * self.avg) / (w + 1)
|
||||
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||
self._ts = now
|
||||
|
||||
kv = [(key, val) for key, val in self.__dict__.items()
|
||||
@@ -59,8 +57,8 @@ class Infinite(object):
|
||||
def finish(self):
|
||||
pass
|
||||
|
||||
def next(self):
|
||||
self.index = self.index + 1
|
||||
def next(self, n=1):
|
||||
self.index = self.index + n
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
@@ -88,8 +86,7 @@ class Progress(Infinite):
|
||||
now = time()
|
||||
if self.delta:
|
||||
dt = (now - self._ts) / self.delta
|
||||
w = self.avg_window
|
||||
self.avg = dt if self.avg else (dt + w * self.avg) / (w + 1)
|
||||
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||
self.eta = int(ceil(self.avg * self.remaining))
|
||||
self._ts = now
|
||||
|
||||
@@ -102,9 +99,9 @@ class Progress(Infinite):
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
def next(self):
|
||||
def next(self, n=1):
|
||||
prev = self.index
|
||||
self.index = min(self.index + 1, self.max)
|
||||
self.index = min(self.index + n, self.max)
|
||||
self.delta = self.index - prev
|
||||
self.update_stats()
|
||||
self.update()
|
||||
|
||||
@@ -37,7 +37,8 @@ class WriteMixin(object):
|
||||
def write(self, s):
|
||||
if self.file.isatty():
|
||||
b = '\b' * self._width
|
||||
print(b + s.ljust(self._width), end='', file=self.file)
|
||||
c = s.encode('utf8').ljust(self._width)
|
||||
print(b + c, end='', file=self.file)
|
||||
self._width = max(self._width, len(s))
|
||||
self.file.flush()
|
||||
|
||||
@@ -64,7 +65,7 @@ class WritelnMixin(object):
|
||||
def writeln(self, line):
|
||||
if self.file.isatty():
|
||||
self.clearln()
|
||||
print(line, end='', file=self.file)
|
||||
print(line.encode('utf8'), end='', file=self.file)
|
||||
self.file.flush()
|
||||
|
||||
def finish(self):
|
||||
@@ -72,3 +73,19 @@ class WritelnMixin(object):
|
||||
print(file=self.file)
|
||||
if self.hide_cursor:
|
||||
print(SHOW_CURSOR, end='', file=self.file)
|
||||
|
||||
|
||||
from signal import signal, SIGINT
|
||||
from sys import exit
|
||||
|
||||
|
||||
class SigIntMixin(object):
|
||||
"""Registers a signal handler that calls finish on SIGINT"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SigIntMixin, self).__init__(*args, **kwargs)
|
||||
signal(SIGINT, self._sigint_handler)
|
||||
|
||||
def _sigint_handler(self, signum, frame):
|
||||
self.finish()
|
||||
exit(0)
|
||||
|
||||
Reference in New Issue
Block a user