Class: Charming::Components::Spinner
- Inherits:
-
Charming::Component
- Object
- View
- Charming::Component
- Charming::Components::Spinner
- 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
-
#frames ⇒ Object
readonly
The current frame list, frame index, and optional label string.
-
#index ⇒ Object
readonly
The current frame list, frame index, and optional label string.
-
#label ⇒ Object
readonly
The current frame list, frame index, and optional label string.
Instance Method Summary collapse
-
#initialize(style: :line, frames: nil, index: 0, label: nil) ⇒ Spinner
constructor
style picks a named preset from STYLES (default :line).
-
#render ⇒ Object
Renders the current frame, optionally followed by the label and a space.
-
#tick ⇒ Object
Advances the frame index by one position, wrapping around.
Methods inherited from Charming::Component
Methods inherited from View
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.
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
#frames ⇒ Object (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 |
#index ⇒ Object (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 |
#label ⇒ Object (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
#render ⇒ Object
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 |
#tick ⇒ Object
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 |