progress.lua

Wed, 13 Jan 2010 04:31:16 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 13 Jan 2010 04:31:16 +0000
changeset 1
7279a9d08042
parent 0
f7beab89e5a4
child 3
d3e20e89d06b
permissions
-rw-r--r--

Add demo.lua

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");
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return { refresh = update_progress_display, update = update_data };
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
f7beab89e5a4 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return _M;

mercurial