class TTY::ProgressBar::Timer

Used to measure the elapsed time for multiple time intervals

@api private

Attributes

start_time[R]

Public Class Methods

new() click to toggle source

Create Timer

@api private

# File lib/tty/progressbar/timer.rb, line 14
def initialize
  reset
end

Public Instance Methods

elapsed_time() click to toggle source

Total elapsed time

@return [Float]

the elapsed time in seconds

@api public

# File lib/tty/progressbar/timer.rb, line 42
def elapsed_time
  if running?
    elapsed_until_now + @offset
  else
    @offset
  end
end
elapsed_until_now() click to toggle source

Measure current time interval

@api public

# 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
reset() click to toggle source

Reset the start time to nil and elapsed time to zero

@api public

# File lib/tty/progressbar/timer.rb, line 21
def reset
  @running = false
  @offset = 0
  @start_time = nil
end
running?() click to toggle source

Check whether or not the timer is running

@return [Boolean]

@api public

# File lib/tty/progressbar/timer.rb, line 32
def running?
  @running
end
start() click to toggle source

Start measuring elapsed time for a new interval

@return [Time]

return the start time

@api public

# File lib/tty/progressbar/timer.rb, line 65
def start
  return @start_time if running?

  @running = true
  @start_time = Time.now
end
stop() click to toggle source

Stop measuring elapsed time for the current interval

@return [Float]

return elapsed time for the stopped interval

@api public

# 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