mirror of
https://github.com/verigak/progress.git
synced 2025-12-09 19:55:34 +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
|
from time import time
|
||||||
|
|
||||||
|
|
||||||
__version__ = '1.0.2'
|
__version__ = '1.1'
|
||||||
|
|
||||||
|
|
||||||
class Infinite(object):
|
class Infinite(object):
|
||||||
file = stderr
|
file = stderr
|
||||||
avg_window = 10
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.ctx = {}
|
self.ctx = {}
|
||||||
@@ -42,8 +41,7 @@ class Infinite(object):
|
|||||||
# Calculate moving average
|
# Calculate moving average
|
||||||
now = time()
|
now = time()
|
||||||
dt = now - self._ts
|
dt = now - self._ts
|
||||||
w = self.avg_window
|
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||||
self.avg = dt if self.avg else (dt + w * self.avg) / (w + 1)
|
|
||||||
self._ts = now
|
self._ts = now
|
||||||
|
|
||||||
kv = [(key, val) for key, val in self.__dict__.items()
|
kv = [(key, val) for key, val in self.__dict__.items()
|
||||||
@@ -59,8 +57,8 @@ class Infinite(object):
|
|||||||
def finish(self):
|
def finish(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def next(self):
|
def next(self, n=1):
|
||||||
self.index = self.index + 1
|
self.index = self.index + n
|
||||||
self.update_stats()
|
self.update_stats()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@@ -88,8 +86,7 @@ class Progress(Infinite):
|
|||||||
now = time()
|
now = time()
|
||||||
if self.delta:
|
if self.delta:
|
||||||
dt = (now - self._ts) / self.delta
|
dt = (now - self._ts) / self.delta
|
||||||
w = self.avg_window
|
self.avg = (dt + self.index * self.avg) / (self.index + 1) if self.avg else dt
|
||||||
self.avg = dt if self.avg else (dt + w * self.avg) / (w + 1)
|
|
||||||
self.eta = int(ceil(self.avg * self.remaining))
|
self.eta = int(ceil(self.avg * self.remaining))
|
||||||
self._ts = now
|
self._ts = now
|
||||||
|
|
||||||
@@ -102,9 +99,9 @@ class Progress(Infinite):
|
|||||||
self.update_stats()
|
self.update_stats()
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
def next(self):
|
def next(self, n=1):
|
||||||
prev = self.index
|
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.delta = self.index - prev
|
||||||
self.update_stats()
|
self.update_stats()
|
||||||
self.update()
|
self.update()
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ class WriteMixin(object):
|
|||||||
def write(self, s):
|
def write(self, s):
|
||||||
if self.file.isatty():
|
if self.file.isatty():
|
||||||
b = '\b' * self._width
|
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._width = max(self._width, len(s))
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ class WritelnMixin(object):
|
|||||||
def writeln(self, line):
|
def writeln(self, line):
|
||||||
if self.file.isatty():
|
if self.file.isatty():
|
||||||
self.clearln()
|
self.clearln()
|
||||||
print(line, end='', file=self.file)
|
print(line.encode('utf8'), end='', file=self.file)
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
@@ -72,3 +73,19 @@ class WritelnMixin(object):
|
|||||||
print(file=self.file)
|
print(file=self.file)
|
||||||
if self.hide_cursor:
|
if self.hide_cursor:
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
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