class PBAndJ::ProgressBar

Public Class Methods

new(desc, count, pad: 0, width: 80, show: true, stream: STDOUT) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 13
def initialize(desc, count, pad: 0, width: 80, show: true, stream: STDOUT)
  @description = desc    || ''
  @count       = count   || 0
  @padding     = pad     || 0
  @width       = width   || 80
  @stream      = stream  || STDOUT
  @show        = !!show
  @index       = 0
  @message     = ''

  raise "Count must be greater than 0" if @count < 1
end

Public Instance Methods

message() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 51
def message
  @message ||= label + marks + suffix
end
show?() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 47
def show?
  @show
end
start(now = Time.now) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 26
def start(now = Time.now)
  @start_at = now
  @index = 0
  reset now
  print
end
stop(now = Time.now) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 41
def stop(now = Time.now)
  reset now
  print
  stream.puts if show?
end
tick(index = nil, finish = nil) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 33
def tick(index = nil, finish = nil)
  start unless defined?(@start_at) && @start_at
  reset
  @index  = index || @index + 1
  @finish = finish if finish
  print
end

Private Instance Methods

current_marks() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 107
def current_marks
  (max_length * percentage).to_i
end
finish(current = @finished) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 80
def finish(current = @finished)
  return @finish if @finish

  time = current - start_at
  return @finish = current if time < 0.001
  avg = time / @index.to_f

  @finish = start_at + @count * avg
end
formatted_finish() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 121
def formatted_finish
  finish.strftime '%H:%M:%S'
end
formatted_start() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 76
def formatted_start
  @formatted_start ||= start_at.strftime '%H:%M:%S'
end
humanize_duration() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 125
def humanize_duration
  humanize_seconds @finished - start_at
end
humanize_seconds(seconds) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 129
def humanize_seconds(seconds)
  ServingSeconds.humanize(seconds, 1).rjust 5
end
label() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 72
def label
  @label ||= "#{description.ljust(padding)}: #{formatted_start} "
end
marks() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 90
def marks
  mark = @index <= @count ? '=' : '>'
  progress = '|' + mark * current_marks

  if @index < @count
    progress += '>' + ' ' * (max_length - current_marks - 1)
  end

  progress += '|'
end
max_length() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 101
def max_length
  return nil unless @width
  return @max_length if defined?(@max_length)
  @max_length ||= width - label.length - suffix.length - 2
end
percentage() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 111
def percentage
  [@index / @count.to_f, 1].min
end
print() click to toggle source
reset(now = Time.now) click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 61
def reset(now = Time.now)
  @finished = now
  @finish   = nil
  @message  = nil
  @suffix   = nil
end
stream() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 57
def stream
  @stream
end
suffix() click to toggle source
# File lib/pb_and_j/progress_bar.rb, line 115
def suffix
  @suffix ||=
    " #{formatted_finish}" +
    " #{humanize_seconds(finish - start_at)} #{humanize_duration}"
end