class MonoclePrint::Progress

Attributes

bar_color[RW]
output[R]
position[RW]
title[R]
total[RW]
width[W]

Public Class Methods

enumerate( collection, method = :each, *args ) { |item, bar| ... } click to toggle source
# File lib/monocle-print/progress.rb, line 11
def self.enumerate( collection, method = :each, *args )
  block_given? or return enum_for( :enumerate, collection, method, *args )
  #options = Hash === args.last ? args.pop : {}

  #method = options.fetch( :method, nil )
  #if method.nil? and Symbol === args.first
  #  method, *args = args
  #else
  #  method ||= :each
  #end

  #size = options[ :size ]
  #size ||= options.fetch( :length ) do
  #  collection.length rescue collection.size
  #end
  size = collection.length rescue collection.size

  enum = collection.enum_for( method, *args )
  run( size ) do | bar |
    for item in enum
      v = yield( item, bar )
      bar.step
      v
    end
  end
end
new( total, options = {} ) click to toggle source
Calls superclass method MonoclePrint::OutputDevice::new
# File lib/monocle-print/progress.rb, line 45
def initialize( total, options = {} )
  @total      = Utils.at_least( total.to_i, 1 )
  @position   = 0
  @title      = Line( options[ :title ].to_s )
  @limit      = @width = @time = @next_time = nil
  @bar_color  = options.fetch( :bar_color, :red )
  @text_color = options.fetch( :text_color, :black )
  @progress   = -1
  @draw       = true
  super( options.fetch( :output, $stderr ), options )
end
run( total, options = {} ) { |bar| ... } click to toggle source
# File lib/monocle-print/progress.rb, line 38
def self.run( total, options = {} )
  bar = new( total, options )
  yield( bar )
ensure
  bar.clear_line
end

Public Instance Methods

display() click to toggle source
# File lib/monocle-print/progress.rb, line 113
def display
  return!

  hour, r  = time_remaining.divmod( 3600 )
  min, sec = r.divmod( 60 )
  sec = sec.round
  eta = Line( ' %02i:%02i:%02i' % [ hour, min, sec ] )

  center_width = 6
  right_width  = ( width - center_width ) / 2
  left_width   = width - center_width - right_width

  center = ( @progress.to_s << '%' ).center( 6 ) # " ___% "
  left   = title.align( :left, left_width ).truncate!( left_width, '...' )
  right  = eta.align( :right, right_width )

  bar = left << center << right

  color_code = ''
  @bar_color  and color_code << ansi_color( ?b, @bar_color )
  @text_color and color_code << ansi_color( ?f, @text_color )

  unless color_code.empty?
    fill_point = bar.char_byte( width * @position / @total )
    bar.insert( fill_point, "\e[0m" )
    bar.insert( 0, color_code )
  end

  print( bar )
  @draw = false
  self
end
draw?() click to toggle source
# File lib/monocle-print/progress.rb, line 71
def draw?
  progress = @position * 100 / @total
  if @progress != progress
    @progress = progress
    @draw = true
  end

  return @draw
end
duration() click to toggle source
# File lib/monocle-print/progress.rb, line 101
def duration
  Time.now - start_time
end
hide() { || ... } click to toggle source
# File lib/monocle-print/progress.rb, line 183
def hide
  wipe
  return yield
ensure
  display
end
limit( l = nil ) { |self| ... } click to toggle source
# File lib/monocle-print/progress.rb, line 81
def limit( l = nil )
  unless l.nil?
    begin
      before, @limit = @limit, Utils.at_most( l.to_i, @total )
      yield( self )
    ensure
      self.limit = before
    end
  end
  return( @limit )
end
limit=( l ) click to toggle source
# File lib/monocle-print/progress.rb, line 93
def limit=( l )
  @limit = Utils.at_most( l.to_i, @total )
end
progress() click to toggle source
# File lib/monocle-print/progress.rb, line 171
def progress
  @position * 100 / @total
end
reset() click to toggle source
# File lib/monocle-print/progress.rb, line 150
def reset
  @position = 0
  @limit = nil
  @start_time = nil
end
stage( title, limit = @total - @position, absolute = false ) { |self| ... } click to toggle source
# File lib/monocle-print/progress.rb, line 57
def stage( title, limit = @total - @position, absolute = false )
  limit += @position unless absolute
  self.title = title
  limit( limit ) do
    return( yield( self ) )
  end
end
start_time() click to toggle source
# File lib/monocle-print/progress.rb, line 97
def start_time
  @start_time ||= Time.now
end
step( inc = 1 ) click to toggle source
# File lib/monocle-print/progress.rb, line 65
def step( inc = 1 )
  limit = @limit || @total
  @position = Utils.at_most( @position + inc, limit )
  draw? and display
end
time_remaining() click to toggle source
# File lib/monocle-print/progress.rb, line 175
def time_remaining
  @position < 2 and return( 0 )
  sec_per_step = duration / @position
  ( @total - @position ) * sec_per_step
end
title=(t) click to toggle source
# File lib/monocle-print/progress.rb, line 105
def title= t
  title = Line( t.to_s )
  if title != @title
    @title = title
    @draw = true
  end
end
title_width() click to toggle source
# File lib/monocle-print/progress.rb, line 146
def title_width
  width * 0.3
end
to_s() click to toggle source
# File lib/monocle-print/progress.rb, line 156
def to_s
  title_width = ( width * 0.4 ).round
  title = @title.align( :center, title_width )[ 0, title_width ]

  hour, r  = time_remaining.divmod( 3600 )
  min, sec = r.divmod( 60 )
  sec = sec.round
  eta = '%02i:%02i:%02i' % [ hour, min, sec ]

  fill_width = width - title_width - eta.length - 9
  filled = ( fill_width * @position / @total ).round
  title << ' |' << ( '*' * filled ).ljust( fill_width ) <<
    '| ' << ( '%3i%% ' % progress ) << eta
end