Class: Charming::Components::Spinner

Inherits:
Charming::Component show all
Defined in:
lib/charming/presentation/components/spinner.rb

Overview

Spinner is a simple rotating-frame indicator. The component cycles through a list of frames on each tick; pair it with a controller timer to drive animation. An optional label is appended after the current frame on each render.

Constant Summary collapse

DEFAULT_FRAMES =

The default frame set: a 4-frame ASCII spinner.

["-", "\\", "|", "/"].freeze
STYLES =

Named frame presets, mirroring the roster popularized by charm.sh's bubbles.

{
  line: DEFAULT_FRAMES,
  dots: %w[         ].freeze,
  mini_dot: %w[       ].freeze,
  jump: %w[      ].freeze,
  pulse: %w[   ].freeze,
  points: ["∙∙∙", "●∙∙", "∙●∙", "∙∙●"].freeze,
  globe: %w[🌍 🌎 🌏].freeze,
  moon: %w[🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘].freeze,
  meter: %w[▱▱▱ ▰▱▱ ▰▰▱ ▰▰▰ ▰▰▱ ▰▱▱].freeze,
  hamburger: %w[   ].freeze,
  ellipsis: ["   ", ".  ", ".. ", "..."].freeze
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Charming::Component

#captures_text?

Methods inherited from View

#focused?, #layout_assigns

Constructor Details

#initialize(style: :line, frames: nil, index: 0, label: nil) ⇒ Spinner

style picks a named preset from STYLES (default :line). frames overrides the preset with any array of frame strings. index is the starting frame index. label is an optional suffix shown after the frame.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/charming/presentation/components/spinner.rb', line 33

def initialize(style: :line, frames: nil, index: 0, label: nil)
  super()
  frames ||= STYLES.fetch(style.to_sym) do
    raise ArgumentError, "unknown spinner style: #{style.inspect} (available: #{STYLES.keys.join(", ")})"
  end
  raise ArgumentError, "frames cannot be empty" if frames.empty?

  @frames = frames
  @index = index
  @label = label
end

Instance Attribute Details

#framesObject (readonly)

The current frame list, frame index, and optional label string.



28
29
30
# File 'lib/charming/presentation/components/spinner.rb', line 28

def frames
  @frames
end

#indexObject (readonly)

The current frame list, frame index, and optional label string.



28
29
30
# File 'lib/charming/presentation/components/spinner.rb', line 28

def index
  @index
end

#labelObject (readonly)

The current frame list, frame index, and optional label string.



28
29
30
# File 'lib/charming/presentation/components/spinner.rb', line 28

def label
  @label
end

Instance Method Details

#renderObject

Renders the current frame, optionally followed by the label and a space.



52
53
54
55
56
# File 'lib/charming/presentation/components/spinner.rb', line 52

def render
  return frame unless label

  "#{frame} #{label}"
end

#tickObject

Advances the frame index by one position, wrapping around. Returns self for chaining.



46
47
48
49
# File 'lib/charming/presentation/components/spinner.rb', line 46

def tick
  @index = (index + 1) % frames.length
  self
end