Class: Philiprehberger::Progress::Multi
- Inherits:
-
Object
- Object
- Philiprehberger::Progress::Multi
- Defined in:
- lib/philiprehberger/progress/multi.rb
Instance Method Summary collapse
- #[](label) ⇒ Object
- #add(label, total:, width: 30) ⇒ Object
- #bars ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(output: $stderr) ⇒ Multi
constructor
A new instance of Multi.
- #labels ⇒ Object
- #render ⇒ Object
- #reset ⇒ Object
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.new(total: total, width: width, output: @output) @bars[label] = @order << label unless @order.include?(label) end |
#bars ⇒ Object
23 24 25 |
# File 'lib/philiprehberger/progress/multi.rb', line 23 def @bars.dup end |
#finished? ⇒ 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 |
#labels ⇒ Object
27 28 29 |
# File 'lib/philiprehberger/progress/multi.rb', line 27 def labels @order.dup end |
#render ⇒ Object
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| = @bars[label] "#{label}: #{}" end @output.print("\e[#{lines.size}A") if @rendered_once lines.each { |line| @output.puts(line) } @rendered_once = true end |
#reset ⇒ Object
49 50 51 52 53 |
# File 'lib/philiprehberger/progress/multi.rb', line 49 def reset @bars.clear @order.clear @rendered_once = false end |