Wed, 08 Aug 2012 12:17:13 +0100
Add :clear() method to remove progress bar and pretend it was never there
0 | 1 | local ceil, floor = math.ceil, math.floor; |
2 | local write = io.write; | |
3 | module("progress"); | |
4 | ||
5 | function new(total) | |
6 | total = total or 100; | |
7 | ||
8 | local anim = { '/', '-', '\\', '|' }; | |
9 | local anim_max, anim_frame = #anim, 0; | |
10 | ||
11 | local completed, percent_complete = 0, 0; | |
12 | local needs_update = true; | |
13 | ||
14 | local function update_data(self, new_completed) | |
15 | completed = new_completed; | |
16 | local new_percent_complete = floor(100/(total/completed)); | |
17 | if percent_complete ~= new_percent_complete then | |
18 | needs_update = true; | |
19 | end | |
20 | percent_complete = new_percent_complete; | |
21 | return needs_update; | |
22 | end | |
23 | ||
24 | local function update_progress_display() | |
25 | anim_frame = (anim_frame%anim_max) + 1; | |
26 | write("\n\027[K0% [", ("="):rep(floor(percent_complete/2)), percent_complete ~= 100 and ">" or "", (" "):rep((50-floor(percent_complete/2))-1), "] 100%"); | |
27 | write("\n\027[K", anim[anim_frame], " ", percent_complete, "% complete"); --; ETA ", ceil((now()-start_time)/(bytes_completed/bytes_total)/60), "min"); | |
28 | write("\027[3A\n"); | |
3
d3e20e89d06b
Reset needs_update flag on refresh
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
29 | needs_update = false; |
0 | 30 | end |
4
3cd674f5353b
Add :clear() method to remove progress bar and pretend it was never there
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
31 | |
3cd674f5353b
Add :clear() method to remove progress bar and pretend it was never there
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
32 | local function clear() |
3cd674f5353b
Add :clear() method to remove progress bar and pretend it was never there
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
33 | write(("\n\027[K"):rep(3), "\027[3A"); |
3cd674f5353b
Add :clear() method to remove progress bar and pretend it was never there
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
34 | end |
3cd674f5353b
Add :clear() method to remove progress bar and pretend it was never there
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
35 | return { refresh = update_progress_display, update = update_data, clear = clear }; |
0 | 36 | end |
37 | ||
38 | return _M; |