Class: Console::Event::Progress
- Defined in:
- lib/console/event/progress.rb
Constant Summary collapse
- BLOCK =
[ " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█", ]
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #bar(value = self.value, width = 70) ⇒ Object
- #format(output, terminal, verbose) ⇒ Object
-
#initialize(current, total) ⇒ Progress
constructor
A new instance of Progress.
- #to_h ⇒ Object
- #value ⇒ Object
Methods inherited from Generic
Constructor Details
#initialize(current, total) ⇒ Progress
Returns a new instance of Progress.
38 39 40 41 |
# File 'lib/console/event/progress.rb', line 38 def initialize(current, total) @current = current @total = total end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
43 44 45 |
# File 'lib/console/event/progress.rb', line 43 def current @current end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
44 45 46 |
# File 'lib/console/event/progress.rb', line 44 def total @total end |
Class Method Details
.register(terminal) ⇒ Object
62 63 64 |
# File 'lib/console/event/progress.rb', line 62 def self.register(terminal) terminal[:progress_bar] ||= terminal.style(:blue, :white) end |
Instance Method Details
#bar(value = self.value, width = 70) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/console/event/progress.rb', line 50 def (value = self.value, width = 70) blocks = width * value full_blocks = blocks.floor partial_block = ((blocks - full_blocks) * BLOCK.size).floor if partial_block.zero? BLOCK.last * full_blocks else "#{BLOCK.last * full_blocks}#{BLOCK[partial_block]}" end.ljust(width) end |
#format(output, terminal, verbose) ⇒ Object
70 71 72 |
# File 'lib/console/event/progress.rb', line 70 def format(output, terminal, verbose) output.puts "#{terminal[:progress_bar]}#{self.}#{terminal.reset} #{sprintf('%6.2f', self.value * 100)}%" end |
#to_h ⇒ Object
66 67 68 |
# File 'lib/console/event/progress.rb', line 66 def to_h {current: @current, total: @total} end |
#value ⇒ Object
46 47 48 |
# File 'lib/console/event/progress.rb', line 46 def value @current.to_f / @total.to_f end |