Class: TTY::ProgressBar::Timer Private
- Inherits:
-
Object
- Object
- TTY::ProgressBar::Timer
- 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
- #start_time ⇒ Object readonly private
Instance Method Summary collapse
-
#elapsed_time ⇒ Float
Total elapsed time.
-
#elapsed_until_now ⇒ Object
Measure current time interval.
-
#initialize ⇒ Timer
constructor
private
Create Timer.
-
#reset ⇒ Object
Reset the start time to nil and elapsed time to zero.
-
#running? ⇒ Boolean
Check whether or not the timer is running.
-
#start ⇒ Time
Start measuring elapsed time for a new interval.
-
#stop ⇒ Float
Stop measuring elapsed time for the current interval.
Constructor Details
#initialize ⇒ Timer
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_time ⇒ Object (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_time ⇒ Float
Total elapsed time
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_now ⇒ Object
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 |
#reset ⇒ Object
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
32 33 34 |
# File 'lib/tty/progressbar/timer.rb', line 32 def running? @running end |
#start ⇒ Time
Start measuring elapsed time for a new interval
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 |
#stop ⇒ Float
Stop measuring elapsed time for the current 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 |