Class: Infobar::Counter

Inherits:
Object show all
Extended by:
Tins::Delegate
Defined in:
lib/infobar/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



8
9
10
# File 'lib/infobar/counter.rb', line 8

def initialize
  reset
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



16
17
18
# File 'lib/infobar/counter.rb', line 16

def as
  @as
end

#currentObject (readonly)

Returns the value of attribute current.



12
13
14
# File 'lib/infobar/counter.rb', line 12

def current
  @current
end

#totalObject (readonly)

Returns the value of attribute total.



14
15
16
# File 'lib/infobar/counter.rb', line 14

def total
  @total
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/infobar/counter.rb', line 58

def done?
  @current >= @total
end

#etaObject



78
79
80
81
82
83
84
# File 'lib/infobar/counter.rb', line 78

def eta
  if @finished
    @finished
  else
    Time.now + time_remaining
  end
end

#finishObject



42
43
44
45
# File 'lib/infobar/counter.rb', line 42

def finish
  @finished = Time.now
  self
end

#finished?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/infobar/counter.rb', line 38

def finished?
  @finished
end

#progress(by: 1, as: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/infobar/counter.rb', line 47

def progress(by: 1, as: nil)
  if !finished? && by >= 1
    now = Time.now
    @start ||= now
    @timer.add(now, by)
    @current += by
    @as[as] += by
  end
  self
end

#progressedObject



101
102
103
104
105
106
107
# File 'lib/infobar/counter.rb', line 101

def progressed
  if @total.zero?
    0.0
  else
    @current / @total.to_f
  end
end

#reset(total: 0, current: 0) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/infobar/counter.rb', line 18

def reset(total: 0, current: 0)
  @current  = current
  @as       = Hash.new(0).update(nil => current)
  @total    = total
  @start    = nil
  @finished = false
  @timer    = Infobar::Timer.new
  self
end

#started?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/infobar/counter.rb', line 34

def started?
  @start
end

#time_elapsedObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/infobar/counter.rb', line 86

def time_elapsed
  case
  when @finished && @start
    @finished - @start
  when @start
    Time.now - @start
  else
    0.0
  end
end

#time_remainingObject



70
71
72
73
74
75
76
# File 'lib/infobar/counter.rb', line 70

def time_remaining
  if @finished
    0.0
  else
    average_time * to_go
  end
end

#to_goObject



62
63
64
# File 'lib/infobar/counter.rb', line 62

def to_go
  [ total - current, 0 ].max
end

#to_go?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/infobar/counter.rb', line 66

def to_go?
  to_go.nonzero?
end

#to_sObject



109
110
111
112
113
# File 'lib/infobar/counter.rb', line 109

def to_s
  n = -> k { k.nil? ? 'default' : k.to_s }
  width = -@as.keys.map(&n).max_by(&:size).to_i
  @as.map { |key, value| "%#{width}s: %d" % [ n.(key), value ] }.join(?\n)
end

#total_timeObject



97
98
99
# File 'lib/infobar/counter.rb', line 97

def total_time
  time_elapsed + time_remaining
end