Class: ProgressBar

Inherits:
Object show all
Defined in:
lib/buildr/core/progressbar.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.widthObject



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

#countObject



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

#elapsedObject



95
96
97
# File 'lib/buildr/core/progressbar.rb', line 95

def elapsed
  'Time: %s' % duration(Time.now - @start) 
end

#etaObject



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

#finishObject



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

#percentageObject



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

#rateObject



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

#startObject



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

#timeObject



84
85
86
# File 'lib/buildr/core/progressbar.rb', line 84

def time
  @finished ? elapsed : eta
end

#titleObject



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

#totalObject



76
77
78
# File 'lib/buildr/core/progressbar.rb', line 76

def total
  human(@total)
end