class Kafo::ProgressBar

Progress bar base class

To define new progress bar you can inherit from this class and implement finite_template and infinite_template methods. Also you may find useful to change more methods like done_message or print_error

Constants

EVALTRACE_END
EVALTRACE_START
MONITOR_RESOURCE
PREFETCH

Public Class Methods

new() click to toggle source
# File lib/kafo/progress_bar.rb, line 18
def initialize
  @lines                                    = 0
  @all_lines                                = 0
  @total                                    = :unknown
  @resources                                = Set.new
  @term_width                               = terminal_width
  @bar                                      = PowerBar.new
  @bar.settings.tty.infinite.template.main  = infinite_template
  @bar.settings.tty.finite.template.main    = finite_template
  @bar.settings.tty.finite.template.padchar = ' '
  @bar.settings.tty.finite.template.barchar = '.'
  @bar.settings.tty.finite.output           = Proc.new { |s| $stderr.print s }
end

Public Instance Methods

close() click to toggle source
# File lib/kafo/progress_bar.rb, line 71
def close
  @bar.show({ :msg   => done_message,
              :done  => @total == :unknown ? @bar.done + 1 : @total,
              :total => @total }, true)
  @bar.close
end
print(line) click to toggle source
print_error(line) click to toggle source
update(line) click to toggle source
# File lib/kafo/progress_bar.rb, line 32
def update(line)
  @all_lines += 1

  # we print every 20th line during installation preparing otherwise only update at EVALTRACE_START
  update_bar = (@total == :unknown && @all_lines % 20 == 0)
  force_update = false

  if (line_monitor = MONITOR_RESOURCE.match(line))
    @resources << line_monitor[1]
    @total = (@total == :unknown ? 1 : @total + 1)
  end

  if (line_start = EVALTRACE_START.match(line))
    if (known_resource = find_known_resource(line_start[1]))
      line = known_resource
      update_bar = true
      force_update = true
    end
  end

  if (line_end = EVALTRACE_END.match(line)) && @total != :unknown && @lines < @total
    if (known_resource = find_known_resource(line_end[1]))
      @resources.delete(known_resource)  # ensure it's only counted once
      @lines += 1
    end
  end

  if PREFETCH =~ line
    update_bar = true
    force_update = true
  end

  if update_bar
    @bar.show({ :msg   => format(line),
                :done  => @lines,
                :total => @total }, force_update)
  end
end

Private Instance Methods

done_message() click to toggle source
# File lib/kafo/progress_bar.rb, line 99
def done_message
  text = 'Done'
  text + (' ' * (50 - text.length))
end
find_known_resource(resource) click to toggle source
# File lib/kafo/progress_bar.rb, line 116
def find_known_resource(resource)
  loop do
    return resource if @resources.include?(resource)
    # continue to remove prefixes from /Stage[main]/Example/File[/etc/foo] until a resource name is found
    break unless resource.include?('/')
    resource = resource.sub %r{.*?/}, ''
  end
  nil
end
finite_template() click to toggle source
# File lib/kafo/progress_bar.rb, line 108
def finite_template
  'Installing... [${<percent>%}]'
end
format(line) click to toggle source
# File lib/kafo/progress_bar.rb, line 104
def format(line)
  (line.tr("\r\n", '') + (' ' * 50))[0..49]
end
infinite_template() click to toggle source
# File lib/kafo/progress_bar.rb, line 112
def infinite_template
  'Installing...'
end
terminal_width() click to toggle source
# File lib/kafo/progress_bar.rb, line 88
def terminal_width
  # HighLine 2 has Terminal, 1 has SystemExtensions
  terminal_size = if HighLine.respond_to?(:default_instance)
                    HighLine.default_instance.terminal.terminal_size
                  else
                    HighLine::SystemExtensions.terminal_size
                  end

  terminal_size ? (terminal_size[0] || 0) : 0
end