Class: Lingo::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/lingo/progress.rb

Direct Known Subclasses

Database::Progress

Instance Method Summary collapse

Constructor Details

#initialize(obj, max, name = nil, doit = true, text = 'progress', nl = true, &block) ⇒ Progress

Returns a new instance of Progress.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lingo/progress.rb', line 31

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

Instance Method Details

#<<(value) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/lingo/progress.rb', line 78

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) ⇒ Object



71
72
73
74
75
76
# File 'lib/lingo/progress.rb', line 71

def init(max, doit = @doit)
  if max && doit
    @ratio, @next = max / 100.0, 0
    self << @count = 0
  end
end