Class: Console::Terminal::Formatter::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/console/terminal/formatter/progress.rb

Overview

Format a progress event, including the current progress and total.

Constant Summary collapse

KEY =

The key used to identify this formatter.

:progress
BLOCK =

The block characters used to render the progress bar.

[
  " ",
  "▏",
  "▎",
  "▍",
  "▌",
  "▋",
  "▊",
  "▉",
  "█",
]

Instance Method Summary collapse

Constructor Details

#initialize(terminal) ⇒ Progress

Create a new progress formatter.

Parameters:



30
31
32
33
# File 'lib/console/terminal/formatter/progress.rb', line 30

def initialize(terminal)
  @terminal = terminal
  @terminal[:progress_bar] ||= terminal.style(:blue, :white)
end

Instance Method Details

#format(event, stream, verbose: false, width: 80) ⇒ Object

Format the given event.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/console/terminal/formatter/progress.rb', line 41

def format(event, stream, verbose: false, width: 80)
  current = event[:current].to_f
  total = event[:total].to_f
  value = current / total
  
  # Clamp value to 1.0 to avoid rendering issues:
  if value > 1.0
    value = 1.0
  end
  
  stream.puts "#{@terminal[:progress_bar]}#{self.bar(value, width-10)}#{@terminal.reset} #{sprintf('%6.2f', value * 100)}%"
end