class Lingo::Progress

Public Class Methods

new(obj, max, name = nil, doit = true, text = 'progress', nl = true) { |self| ... } click to toggle source
# File lib/lingo/progress.rb, line 30
def initialize(obj, max, name = nil, doit = true, text = 'progress', nl = true, &block)
  @doit, @out = doit, obj.lingo.config.stderr

  if max && doit
    format = ' [%3d%%]'
    length = (format % 0).length

    erase = "\b" * length
    @format = format + erase

    print name, ': ' if name
    print text

    init(max)

    msg = %w[done aborted]
  end

  suc = false

  res = catch(:cancel) {
    int = trap(:INT) { throw(:cancel) }

    begin
      yield self
    ensure
      trap(:INT, int)
    end

    suc = true
    nil
  }

  print ' ' * length + erase, ' ', msg[suc ? 0 : 1], '.' if msg
  print "\n" if msg && res

  print Array(res).join("\n") if res
  print "\n" if nl && (msg || res)
end

Public Instance Methods

<<(value) click to toggle source
# File lib/lingo/progress.rb, line 77
def <<(value)
  if defined?(@count) && (@count = value) >= @next
    percent = @count / @ratio
    @next = (percent + 1) * @ratio

    print @format % percent if percent.finite?
  end
end
init(max, doit = @doit) click to toggle source
# File lib/lingo/progress.rb, line 70
def init(max, doit = @doit)
  if max && doit
    @ratio, @next = max / 100.0, 0
    self << @count = 0
  end
end

Private Instance Methods

print(*args) click to toggle source