module Bumbler::Progress

Attributes

registry[R]

Public Class Methods

bar(width) click to toggle source
# File lib/bumbler/progress.rb, line 45
def bar(width)
  inner_size = width - 2

  fill_size = [((@loaded_items / @item_count.to_f) * inner_size).to_i, inner_size].min
  fill = '#' * fill_size
  empty = ' ' * (inner_size - fill_size)

  "[#{fill}#{empty}]"
end
item_finished(name, time) click to toggle source
# File lib/bumbler/progress.rb, line 29
def item_finished(name, time)
  @registry[name] = time

  @loaded_items += 1

  @prev_item = { name: name, time: time }
  @curr_item = nil if @curr_item && @curr_item[:name] == name

  render_progress
end
item_started(name) click to toggle source
# File lib/bumbler/progress.rb, line 23
def item_started(name)
  @curr_item = { name: name }

  render_progress
end
register_item(name) click to toggle source
# File lib/bumbler/progress.rb, line 16
def register_item(name)
  # Build a blank key for the item
  @item_count += 1 unless @registry[name]

  @registry[name] = nil
end
render_progress() click to toggle source
# File lib/bumbler/progress.rb, line 55
def render_progress
  # Do nothing if we don't have any items to load
  return if @item_count == 0

  # Output components:
  #   [#######################################]
  #   (##/##) <current>...   <prev> (####.##ms)
  #
  # Skip the current if there isn't enough room
  count   = format('(%s/%d) ', @loaded_items.to_s.rjust(@item_count.to_s.size), @item_count)
  current = @curr_item ? "#{@curr_item[:name]}... " : ''
  prev    = @prev_item ? format('%s (%sms)', @prev_item[:name], ('%.2f' % @prev_item[:time]).rjust(7)) : ''

  if $stdout.tty?
    width = tty_width

    print "\r\e[A\r\e[A" if @outputted_once
    @outputted_once = true

    # Align the bottom row
    space_for_current = width - (count.length + prev.length)

    # Render the progress
    puts bar(width)

    if space_for_current >= current.length
      puts count + current + prev.rjust(width - count.length - current.length)
    else
      puts count + prev.rjust(width - count.length)
    end
  elsif @curr_item
    puts format('%s %s', count, @curr_item[:name])
  end
end
tty_width() click to toggle source
# File lib/bumbler/progress.rb, line 40
def tty_width
  # console_winsize: https://github.com/ruby/ruby/blob/f27eb8148f5a72bbacfebfecc7de9305471bb5c9/ext/io/console/console.c#L796
  IO.console.winsize[1]
end