Class: TwirlSpinner

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progress/cli/twirl_spinner.rb

Overview

Minimal spinner implementation used by the Twirl CLI

This small class handles animation frame selection and printing for the Twirl command. It was extracted to keep runtime logic out of the CLI dispatcher module.

Instance Method Summary collapse

Constructor Details

#initialize(message, options = {}) ⇒ TwirlSpinner

Returns a new instance of TwirlSpinner.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby-progress/cli/twirl_spinner.rb', line 11

def initialize(message, options = {})
  @message = message
  @speed = parse_speed(options[:speed] || 'medium')

  style_opt = options[:style].to_s
  if style_opt.start_with?('custom=')
    chars = style_opt.sub('custom=', '')
    @frames = if chars.length >= 3
                chars.chars
              elsif chars.length == 2
                [chars[0], chars[1], chars[1]]
              else
                [chars, chars, chars]
              end
    @style = :custom
  else
    @style = parse_style(style_opt.empty? ? 'dots' : style_opt)
    @frames = RubyProgress::INDICATORS[@style] || RubyProgress::INDICATORS[:dots]
  end
  @start_chars, @end_chars = RubyProgress::Utils.parse_ends(options[:ends])
  @index = 0
end

Instance Method Details

#animateObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-progress/cli/twirl_spinner.rb', line 34

def animate
  @output_capture&.redraw($stderr)
  if @message && !@message.empty?
    $stderr.print "\r\e[2K#{@start_chars}#{@message} #{@frames[@index]}#{@end_chars}"
  else
    $stderr.print "\r\e[2K#{@start_chars}#{@frames[@index]}#{@end_chars}"
  end
  $stderr.flush
  @index = (@index + 1) % @frames.length
  sleep @speed
end