lib/bumbler/progress.rb in bumbler-0.5.0 vs lib/bumbler/progress.rb in bumbler-0.6.0

- old
+ new

@@ -1,60 +1,54 @@ +# frozen_string_literal: true +# TODO: replace with ruby-progressbar dependency module Bumbler module Progress @item_count = 0 @loaded_items = 0 - # registry[item_type][item_name] = {:time => 123.45} - @registry = Hash.new { |h,k| h[k] = {} } + # registry[item_name] = 123.45 + @registry = {} class << self - def registry - @registry - end + attr_reader :registry - def register_item(type, name) + def register_item(name) # Build a blank key for the item - unless @registry[type][name] - @item_count += 1 - end + @item_count += 1 unless @registry[name] - @registry[type][name] = {} + @registry[name] = nil end - def item_started(type, name) - @curr_item = {:type => type, :name => name} + def item_started(name) + @curr_item = { name: name } - self.render_progress + render_progress end - def item_finished(type, name, time) - @registry[type][name] = {:time => time} + def item_finished(name, time) + @registry[name] = time - @loaded_items += 1 + @loaded_items += 1 - @prev_item = {:type => type, :name => name, :time => time} - @curr_item = nil if @curr_item && @curr_item[:name] == @prev_item[:name] && @curr_item[:type] == @prev_item[:type] + @prev_item = { name: name, time: time } + @curr_item = nil if @curr_item && @curr_item[:name] == name - self.render_progress + render_progress end - def start! - # No-op for now. - end - def tty_width `tput cols`.to_i || 80 end def bar(width) inner_size = width - 2 - fill_size = [((@loaded_items.to_f / @item_count.to_f) * inner_size).to_i, inner_size].min - fill = '#' * fill_size + fill_size = [((@loaded_items / @item_count.to_f) * inner_size).to_i, inner_size].min + fill = '#' * fill_size empty = ' ' * (inner_size - fill_size) - return "[#{fill}#{empty}]" + "[#{fill}#{empty}]" end def render_progress # Do nothing if we don't have any items to load return if @item_count == 0 @@ -62,32 +56,32 @@ # Output components: # [#######################################] # (##/##) <current>... <prev> (####.##ms) # # Skip the current if there isn't enough room - count = '(%s/%d) ' % [@loaded_items.to_s.rjust(@item_count.to_s.size), @item_count] + 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 ? '%s (%sms)' % [@prev_item[:name], ('%.2f' % @prev_item[:time]).rjust(7)] : '' + prev = @prev_item ? format('%s (%sms)', @prev_item[:name], ('%.2f' % @prev_item[:time]).rjust(7)) : '' if $stdout.tty? - width = self.tty_width + 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 self.bar(width) + 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 '%s %s' % [count, @curr_item[:name]] + puts format('%s %s', count, @curr_item[:name]) end end end end end