Class: TTY::ProgressBar::Timer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/progressbar/timer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Used to measure the elapsed time for multiple time intervals

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create Timer



14
15
16
# File 'lib/tty/progressbar/timer.rb', line 14

def initialize
  reset
end

Instance Attribute Details

#start_timeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/tty/progressbar/timer.rb', line 9

def start_time
  @start_time
end

Instance Method Details

#elapsed_timeFloat

Total elapsed time

Returns:

  • (Float)

    the elapsed time in seconds



42
43
44
45
46
47
48
# File 'lib/tty/progressbar/timer.rb', line 42

def elapsed_time
  if running?
    elapsed_until_now + @offset
  else
    @offset
  end
end

#elapsed_until_nowObject

Measure current time interval



53
54
55
56
57
# File 'lib/tty/progressbar/timer.rb', line 53

def elapsed_until_now
  time_so_far = Time.now - @start_time
  # protect against negative time drifting
  time_so_far > 0 ? time_so_far : 0
end

#resetObject

Reset the start time to nil and elapsed time to zero



21
22
23
24
25
# File 'lib/tty/progressbar/timer.rb', line 21

def reset
  @running = false
  @offset = 0
  @start_time = nil
end

#running?Boolean

Check whether or not the timer is running

Returns:

  • (Boolean)


32
33
34
# File 'lib/tty/progressbar/timer.rb', line 32

def running?
  @running
end

#startTime

Start measuring elapsed time for a new interval

Returns:

  • (Time)

    return the start time



65
66
67
68
69
70
# File 'lib/tty/progressbar/timer.rb', line 65

def start
  return @start_time if running?

  @running = true
  @start_time = Time.now
end

#stopFloat

Stop measuring elapsed time for the current interval

Returns:

  • (Float)

    return elapsed time for the stopped interval



78
79
80
81
82
83
84
85
86
# File 'lib/tty/progressbar/timer.rb', line 78

def stop
  return 0 unless running?

  interval = elapsed_until_now
  @offset += interval
  @running = false
  @start_time = nil
  interval
end