forked from avivkiss/warp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
193 lines (150 loc) · 5.21 KB
/
progress.py
File metadata and controls
193 lines (150 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from __future__ import division
from config import *
from common_tools import *
from blessings import Terminal
import time, threading, sys
SLEEP_TIME = 0.1
class WarpInterface(object):
def __init__(self):
self.screen = Screen()
self.progress_bar = ProgressComponent()
self.screen.add_component(self.progress_bar, to_bottom=True)
self.status_line = Line()
self.files_sent_indicator = CounterComponent(format="Sent {} files. ")
self.files_processed_indicator = CounterComponent(format="Processed {} files.")
self.status_line.add_component(self.files_sent_indicator)
self.status_line.add_component(self.files_processed_indicator)
self.screen.add_line(self.status_line, to_bottom=True)
def log_message(self, message):
self.screen.add_component(Component(message))
self.redraw()
def redraw(self):
self.screen.redraw()
def exit(self):
self.screen.exit()
class Screen(object):
def __init__(self):
self.term = Terminal()
self.top_lines = {}
self.bottom_lines = {}
self.next_line_top = 0
self.next_line_bottom = 1
print self.term.enter_fullscreen()
def redraw(self):
print self.term.clear()
for n, line in self.top_lines.iteritems():
with self.term.location(0, n):
for component in line:
print str(component),
for n, line in self.bottom_lines.iteritems():
with self.term.location(0, self.term.height - n):
for component in line:
print str(component),
sys.stdout.flush()
def add_line(self, line, to_bottom=False):
if to_bottom is False:
self.top_lines[self.next_line_top] = line
self.next_line_top += 1
else:
self.bottom_lines[self.next_line_bottom] = line
self.next_line_bottom += 1
def add_component(self, component, to_bottom=False):
if to_bottom is False:
self.top_lines[self.next_line_top] = Line(comp=component)
self.next_line_top += 1
else:
self.bottom_lines[self.next_line_bottom] = Line(comp=component)
self.next_line_bottom += 1
def exit(self):
print self.term.clear()
print self.term.exit_fullscreen()
class Line(object):
def __init__(self, comp=None):
self.components = []
if comp is not None:
self.components.append(comp)
def add_component(self, component):
self.components.append(component)
def __iter__(self):
for each in self.components:
yield each
class Component(object):
def __init__(self, value=""):
self.value = value
self.active = True
def set_label(self, value):
self.value = value
def updateCallback(self):
pass
def set_update(self, func):
def update():
while self.active:
try:
self.value = func()
self.updateCallback()
except:
break
time.sleep(SLEEP_TIME)
thread = threading.Thread(target=update)
thread.setDaemon(True)
thread.start()
def __str__(self):
return self.value
class CounterComponent(Component):
def __init__(self, format="{}"):
self.value = 0
self.format = format
self.active = True
def increment(self):
self.value += 1
def __str__(self):
return self.format.format(self.value)
class ProgressComponent(Component):
def __init__(self, label="Progress", fill_char='#', empty_char=' ', expected_size=0, progress=0):
super(ProgressComponent, self).__init__(label)
self.expected_size = expected_size
self.fill_char = fill_char
self.progress = progress
self.empty_char = empty_char
self.term = Terminal()
self.label = label
self.units = ["bytes", "KB", "MB", "GB", "TB"]
self.lastProgress = [0, 0]
self.lastUpdated = time.time()
self.timeDiff = 0
self.value = (expected_size, progress, False)
def updateCallback(self):
if self.progress > self.lastProgress[1] and time.time() != self.lastUpdated:
self.lastProgress[0] = self.lastProgress[1]
self.lastProgress[1] = self.progress
self.timeDiff = time.time() - self.lastUpdated
self.lastUpdated = time.time()
def printableUnits(self, value):
i = 0
for i in range(1, len(self.units)+1):
if value // pow(1000, i) == 0:
break
i-=1
return i
def __str__(self):
self.progress = self.value[1]
self.expected_size = self.value[0]
if self.value[2] and self.progress == self.expected_size:
self.fill_char = "V"
i = self.printableUnits(self.expected_size)
j = self.printableUnits(self.progress)
progress = "{0:.3f}".format(self.progress/pow(1000, j)) + self.units[j] + "/" + "{0:.3f}".format(self.expected_size/pow(1000, i)) + self.units[i]
speed = 0
if self.timeDiff != 0:
speed = (self.lastProgress[1] - self.lastProgress[0])/self.timeDiff
# speed is currently in bytes per second
k = self.printableUnits(speed)
progress += " " + "{0:.3f}".format(speed/pow(1000, k)) + self.units[k] + "/s"
width = self.term.width - len(self.label) - 5 - len(progress)
if self.expected_size != 0:
p = self.progress * width // self.expected_size
else:
p = 0
if self.lastProgress < self.progress:
self.lastProgress = self.progress
return self.label + ": [" + self.fill_char * p + self.empty_char * (width - p) + "]" + " " + progress