progress.lua

Wed, 08 Aug 2012 12:17:13 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 08 Aug 2012 12:17:13 +0100
changeset 4
3cd674f5353b
parent 3
d3e20e89d06b
permissions
-rw-r--r--

Add :clear() method to remove progress bar and pretend it was never there

0
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local ceil, floor = math.ceil, math.floor;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local write = io.write;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 module("progress");
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 function new(total)
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 total = total or 100;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local anim = { '/', '-', '\\', '|' };
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local anim_max, anim_frame = #anim, 0;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local completed, percent_complete = 0, 0;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local needs_update = true;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local function update_data(self, new_completed)
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 completed = new_completed;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local new_percent_complete = floor(100/(total/completed));
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if percent_complete ~= new_percent_complete then
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 needs_update = true;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 percent_complete = new_percent_complete;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 return needs_update;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local function update_progress_display()
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 anim_frame = (anim_frame%anim_max) + 1;
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 write("\n\027[K0% [", ("="):rep(floor(percent_complete/2)), percent_complete ~= 100 and ">" or "", (" "):rep((50-floor(percent_complete/2))-1), "] 100%");
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 write("\n\027[K", anim[anim_frame], " ", percent_complete, "% complete"); --; ETA ", ceil((now()-start_time)/(bytes_completed/bytes_total)/60), "min");
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return _M;

mercurial