class Chamomile::Stopwatch
Count-up stopwatch with start/stop/reset and tick-based updates.
Attributes
Public Class Methods
Source
# File lib/chamomile/components/stopwatch.rb, line 26 def initialize(interval: 1.0) @id = self.class.next_id @interval = interval @elapsed = 0.0 @tag = 0 @running = false end
Source
# File lib/chamomile/components/stopwatch.rb, line 12 def self.next_id @id_mutex.synchronize do if Process.pid != @id_pid @id_pid = Process.pid @next_id = 0 @id_mutex = Mutex.new end @next_id += 1 "#{@id_pid}-sw-#{@next_id}" end end
Public Instance Methods
Source
# File lib/chamomile/components/stopwatch.rb, line 67 def handle(msg) return unless msg.is_a?(StopwatchTickMsg) return unless msg.id == @id && msg.tag == @tag @elapsed += @interval @tag += 1 tick_cmd end
Also aliased as: update
Source
# File lib/chamomile/components/stopwatch.rb, line 56 def reset @elapsed = 0.0 @running = false @tag += 1 self end
Source
# File lib/chamomile/components/stopwatch.rb, line 34 def start_cmd return nil if @running @running = true tick_cmd end
Source
# File lib/chamomile/components/stopwatch.rb, line 41 def stop @running = false @tag += 1 self end
Source
# File lib/chamomile/components/stopwatch.rb, line 47 def toggle if @running stop nil else start_cmd end end
Source
# File lib/chamomile/components/stopwatch.rb, line 78 def view total = @elapsed.ceil.to_i hours = total / 3600 minutes = (total % 3600) / 60 seconds = total % 60 if hours.positive? format("%d:%02d:%02d", hours, minutes, seconds) else format("%02d:%02d", minutes, seconds) end end