forked from kastner/ruby-junk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress.rb
More file actions
48 lines (36 loc) · 710 Bytes
/
progress.rb
File metadata and controls
48 lines (36 loc) · 710 Bytes
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
#!/usr/bin/env ruby
module Progress
extend self
attr_accessor :file, :total, :current
WIDTH = `tput cols`.to_i - 2
def height
`tput lines`.to_i
end
def percent_done
("%0.4f" % (current.call.to_f / total.call)).to_f
end
def done_chars
(percent_done * WIDTH).to_i
end
def progress
out = ""
out << "["
out << "=" * (done_chars)
out << ">"
out << "-" * ((WIDTH - done_chars) - 1)
out << "]"
end
def status
"#{current.call}/#{total.call} (#{percent_done * 100}%)"
end
def output
s = status
unless @cleared
puts "\n\n"
@cleared = true
end
print "\033[2A" # up 2 lines
puts s
puts progress
end
end