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 || 0
end

Instance Method Details

#<<(bytes) ⇒ Object



58
59
60
# File 'lib/buildr/core/progressbar.rb', line 58

def <<(bytes)
  inc bytes.size
end

#countObject



73
74
75
# File 'lib/buildr/core/progressbar.rb', line 73

def count
  human(@count)
end

#duration(seconds) ⇒ Object



116
117
118
# File 'lib/buildr/core/progressbar.rb', line 116

def duration(seconds)
  '%02d:%02d:%02d' % [seconds / 3600, (seconds / 60) % 60, seconds % 60]
end

#elapsedObject



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

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

#etaObject



89
90
91
92
93
94
# File 'lib/buildr/core/progressbar.rb', line 89

def eta
  return 'ETA:  --:--:--' if @count == 0
  elapsed = Time.now - @start
  eta = elapsed * @total / @count - elapsed
  'ETA:  %s' % duration(eta.ceil)
end

#finishObject



120
121
122
123
124
125
# File 'lib/buildr/core/progressbar.rb', line 120

def finish
  unless @finished
    @finished = true
    render
  end
end

#human(bytes) ⇒ Object



110
111
112
113
114
# File 'lib/buildr/core/progressbar.rb', line 110

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



81
82
83
# File 'lib/buildr/core/progressbar.rb', line 81

def percentage
  '%3d%%' % (@total == 0 ? 100 : (@count * 100 / @total))
end

#progress(width) ⇒ Object



104
105
106
107
108
# File 'lib/buildr/core/progressbar.rb', line 104

def progress(width)
  width -= 2
  marks = @total == 0 ? width : (@count * width / @total)
  "|%-#{width}s|" % (@mark * marks)
end

#rateObject



100
101
102
# File 'lib/buildr/core/progressbar.rb', line 100

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



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

def time
  @finished ? elapsed : eta
end

#titleObject



68
69
70
71
# File 'lib/buildr/core/progressbar.rb', line 68

def title
  return @title if ProgressBar.width <= 10
  @title.size > ProgressBar.width / 5 ? (@title[0, ProgressBar.width / 5 - 2] + '..') : @title
end

#totalObject



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

def total
  human(@total)
end