Class: ProgressBar
Class Method Summary collapse
Instance Method Summary collapse
- #<<(bytes) ⇒ Object
- #count ⇒ Object
- #duration(seconds) ⇒ Object
- #elapsed ⇒ Object
- #eta ⇒ Object
- #finish ⇒ Object
- #human(bytes) ⇒ Object
- #inc(count) ⇒ Object
-
#initialize(args = {}) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
- #percentage ⇒ Object
- #progress(width) ⇒ Object
- #rate ⇒ Object
- #set(count) ⇒ Object
- #start ⇒ Object
- #time ⇒ Object
- #title ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ ProgressBar
Returns a new instance of ProgressBar.
31 32 33 34 35 36 37 38 |
# File 'lib/buildr/core/progressbar.rb', line 31 def initialize(args = {}) @title = args[:title] || '' @total = args[:total] || 0 @mark = args[:mark] || '.' @format = args[:format] || default_format @output = args[:output] || $stderr unless args[:hidden] || !$stdout.isatty clear end |
Class Method Details
.start(args, &block) ⇒ Object
21 22 23 |
# File 'lib/buildr/core/progressbar.rb', line 21 def start(args, &block) new(args).start &block end |
.width ⇒ Object
25 26 27 |
# File 'lib/buildr/core/progressbar.rb', line 25 def width @width ||= $terminal.output_cols || 80 end |
Instance Method Details
#<<(bytes) ⇒ Object
58 59 60 |
# File 'lib/buildr/core/progressbar.rb', line 58 def <<(bytes) inc bytes.size end |
#count ⇒ Object
72 73 74 |
# File 'lib/buildr/core/progressbar.rb', line 72 def count human(@count) end |
#duration(seconds) ⇒ Object
115 116 117 |
# File 'lib/buildr/core/progressbar.rb', line 115 def duration(seconds) '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60] end |
#elapsed ⇒ Object
95 96 97 |
# File 'lib/buildr/core/progressbar.rb', line 95 def elapsed 'Time: %s' % duration(Time.now - @start) end |
#eta ⇒ Object
88 89 90 91 92 93 |
# File 'lib/buildr/core/progressbar.rb', line 88 def eta return 'ETA: --:--:--' if @count == 0 elapsed = Time.now - @start eta = elapsed * @total / @count - elapsed 'ETA: %s' % duration(eta.ceil) end |
#finish ⇒ Object
119 120 121 122 123 124 |
# File 'lib/buildr/core/progressbar.rb', line 119 def finish unless @finished @finished = true render end end |
#human(bytes) ⇒ Object
109 110 111 112 113 |
# File 'lib/buildr/core/progressbar.rb', line 109 def human(bytes) magnitude = (0..3).find { |i| bytes < (1024 << i * 10) } || 3 return '%dB' % bytes if magnitude == 0 return '%.1f%s' % [ bytes.to_f / (1 << magnitude * 10), [nil, 'KB', 'MB', 'GB'][magnitude] ] end |
#inc(count) ⇒ Object
54 55 56 |
# File 'lib/buildr/core/progressbar.rb', line 54 def inc(count) set @count + count end |
#percentage ⇒ Object
80 81 82 |
# File 'lib/buildr/core/progressbar.rb', line 80 def percentage '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total)) end |
#progress(width) ⇒ Object
103 104 105 106 107 |
# File 'lib/buildr/core/progressbar.rb', line 103 def progress(width) width -= 2 marks = @total == 0 ? width : (@count * width / @total) "|%-#{width}s|" % (@mark * marks) end |
#rate ⇒ Object
99 100 101 |
# File 'lib/buildr/core/progressbar.rb', line 99 def rate '%s/s' % human(@count / (Time.now - @start)) end |
#set(count) ⇒ Object
62 63 64 65 66 |
# File 'lib/buildr/core/progressbar.rb', line 62 def set(count) @count = [count, 0].max @count = [count, @total].min unless @total == 0 render if changed? end |
#start ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/buildr/core/progressbar.rb', line 40 def start @start = @last_time = Time.now @count = 0 @finished = false render if block_given? result = yield(self) if block_given? finish result else self end end |
#time ⇒ Object
84 85 86 |
# File 'lib/buildr/core/progressbar.rb', line 84 def time @finished ? elapsed : eta end |
#title ⇒ Object
68 69 70 |
# File 'lib/buildr/core/progressbar.rb', line 68 def title @title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title end |
#total ⇒ Object
76 77 78 |
# File 'lib/buildr/core/progressbar.rb', line 76 def total human(@total) end |