Class: TuiTui::Widget::Spinner

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/widget/spinner.rb

Overview

A tiny frame-cycling state machine for "working..." indicators. It knows nothing about time: the app advances it (typically once per TickEvent) and draws the current glyph wherever it likes.

Constant Summary collapse

FRAMES_ASCII =

Default frames are ASCII so every glyph is guaranteed 1 column wide (README N7). FRAMES_BRAILLE is offered for terminals with good glyph coverage — whether to use it is the caller's judgement, not ours.

%w[- \\ | /].freeze
FRAMES_BRAILLE =
%w[         ].freeze

Instance Method Summary collapse

Constructor Details

#initialize(frames: FRAMES_ASCII) ⇒ Spinner

Returns a new instance of Spinner.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/tui_tui/widget/spinner.rb', line 15

def initialize(frames: FRAMES_ASCII)
  raise ArgumentError, "spinner needs at least one frame" if frames.empty?

  @frames = frames
  @index = 0
end

Instance Method Details

#advanceObject

Step to the next frame (wrapping). Returns self for chaining.



23
24
25
26
# File 'lib/tui_tui/widget/spinner.rb', line 23

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

#draw(canvas, row, col, style: nil) ⇒ Object



31
32
33
34
# File 'lib/tui_tui/widget/spinner.rb', line 31

def draw(canvas, row, col, style: nil)
  canvas.text(row, col, glyph, style)
  canvas
end

#glyphObject

The current frame's string.



29
# File 'lib/tui_tui/widget/spinner.rb', line 29

def glyph = @frames[@index]