-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer
More file actions
executable file
·72 lines (57 loc) · 1.28 KB
/
Copy pathtimer
File metadata and controls
executable file
·72 lines (57 loc) · 1.28 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
#!/usr/bin/env ruby -w
# timer continuation function
def make_timer
start_time = Time.now.to_f
lambda { Time.now.to_f - start_time }
end
def duration(string)
begin
if string =~ /:/
parts = string.split(':').map{|s| s.to_f.round(2)}.reverse
factors = [1, 60, 60 * 60, 60 * 60 * 24]
seconds = 0
while parts.size > 0 && factors.size > 0
seconds += parts.shift * factors.shift
end
seconds
else
string.to_f.round(2)
end
rescue
0
end
end
if ARGV[0]
total_time = duration(ARGV[0])
direction = :down
puts "Counting down from #{total_time} seconds..."
else
direction = :up
puts "Couting up..."
end
timer = make_timer
begin
sleep 0.01
elapsed = timer.call.round(2)
if direction == :up
reporting = elapsed
else
reporting = total_time - elapsed
end
seconds = reporting.truncate
subseconds = ((reporting - seconds.to_f) * 100.0).truncate
minutes = 0
hours = 0
if seconds >= 60
minutes = (seconds / 60).truncate
seconds %= 60
end
if minutes >= 60
hours = (minutes / 60).truncate
minutes %= 60
end
print " \r%02d:%02d:%02d.%02d (%0.2fs)" %
[hours, minutes, seconds, subseconds, reporting]
end while direction == :up or elapsed < total_time
puts ""
puts "Done!"