class Chamomile::Spinner
Animated spinner with configurable frame types and tick-based updates.
Attributes
Public Class Methods
Source
# File lib/chamomile/components/spinner.rb, line 26 def initialize(type: Spinners::LINE) @id = self.class.next_id @spinner_type = type @frame = 0 @tag = 0 end
Source
# File lib/chamomile/components/spinner.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}-#{@next_id}" end end
Public Instance Methods
Source
# File lib/chamomile/components/spinner.rb, line 45 def handle(msg) return unless msg.is_a?(SpinnerTickMsg) return unless msg.id == @id && msg.tag == @tag @frame = (@frame + 1) % @spinner_type.frames.size @tag += 1 tick_cmd end
Advance frame if msg is a matching SpinnerTickMsg; return cmd or nil.
Also aliased as: update
Source
# File lib/chamomile/components/spinner.rb, line 62 def reset @frame = 0 @tag += 1 self end
Reset to first frame, invalidate in-flight ticks.
Source
# File lib/chamomile/components/spinner.rb, line 69 def spinner_type=(type) @spinner_type = type @frame = 0 @tag += 1 end
Change spinner type, reset frame, invalidate in-flight ticks.
Source
# File lib/chamomile/components/spinner.rb, line 34 def tick_cmd captured_id = @id captured_tag = @tag fps = @spinner_type.fps -> { sleep(1.0 / fps) SpinnerTickMsg.new(id: captured_id, tag: captured_tag, time: Time.now) } end
Returns a command that sleeps for the frame interval then produces a SpinnerTickMsg.
Source
# File lib/chamomile/components/spinner.rb, line 57 def view @spinner_type.frames[@frame] end
Current frame string.