Class: Philiprehberger::Progress::Multi

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/progress/multi.rb

Instance Method Summary collapse

Constructor Details

#initialize(output: $stderr) ⇒ Multi

Returns a new instance of Multi.



6
7
8
9
10
# File 'lib/philiprehberger/progress/multi.rb', line 6

def initialize(output: $stderr)
  @output = output
  @bars = {}
  @order = []
end

Instance Method Details

#[](label) ⇒ Object



19
20
21
# File 'lib/philiprehberger/progress/multi.rb', line 19

def [](label)
  @bars[label]
end

#add(label, total:, width: 30) ⇒ Object



12
13
14
15
16
17
# File 'lib/philiprehberger/progress/multi.rb', line 12

def add(label, total:, width: 30)
  bar = Bar.new(total: total, width: width, output: @output)
  @bars[label] = bar
  @order << label unless @order.include?(label)
  bar
end

#barsObject



23
24
25
# File 'lib/philiprehberger/progress/multi.rb', line 23

def bars
  @bars.dup
end

#finished?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/philiprehberger/progress/multi.rb', line 31

def finished?
  return false if @bars.empty?

  @bars.values.all?(&:finished?)
end

#labelsObject



27
28
29
# File 'lib/philiprehberger/progress/multi.rb', line 27

def labels
  @order.dup
end

#renderObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/philiprehberger/progress/multi.rb', line 37

def render
  return unless @output.respond_to?(:tty?) && @output.tty?

  lines = @order.map do |label|
    bar = @bars[label]
    "#{label}: #{bar}"
  end
  @output.print("\e[#{lines.size}A") if @rendered_once
  lines.each { |line| @output.puts(line) }
  @rendered_once = true
end

#resetObject



49
50
51
52
53
# File 'lib/philiprehberger/progress/multi.rb', line 49

def reset
  @bars.clear
  @order.clear
  @rendered_once = false
end