Class: Anima::Spinner

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

Overview

Braille spinner for long-running CLI operations. Animates in a background thread while a block executes in the calling thread, then shows a success/failure indicator.

Examples:

result = Spinner.run("Installing...") { system("make install") }

Constant Summary collapse

FRAMES =
%w[         ].freeze
FRAME_DELAY =
0.08
SUCCESS_ICON =
"\u2713"
FAILURE_ICON =
"\u2717"
JOIN_TIMEOUT =
2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, output: $stdout) ⇒ Spinner

Returns a new instance of Spinner.

Parameters:

  • message (String)

    status text shown beside the spinner

  • output (IO) (defaults to: $stdout)

    output stream



29
30
31
32
33
34
# File 'lib/anima/spinner.rb', line 29

def initialize(message, output: $stdout)
  @message = message
  @output = output
  @mutex = Mutex.new
  @running = false
end

Class Method Details

.run(message, output: $stdout) { ... } ⇒ Object

Run a block with an animated spinner beside a status message.

Parameters:

  • message (String)

    status text shown beside the spinner

  • output (IO) (defaults to: $stdout)

    output stream (defaults to $stdout)

Yields:

  • the operation to run

Returns:

  • (Object)

    the block’s return value



23
24
25
# File 'lib/anima/spinner.rb', line 23

def self.run(message, output: $stdout, &block)
  new(message, output: output).run(&block)
end

Instance Method Details

#run { ... } ⇒ Object

Returns the block’s return value.

Yields:

  • operation to run while the spinner animates

Returns:

  • (Object)

    the block’s return value



38
39
40
41
42
43
44
45
46
# File 'lib/anima/spinner.rb', line 38

def run
  thread = start_animation
  result = yield
  stop_animation(thread, success: !!result)
  result
rescue
  stop_animation(thread, success: false)
  raise
end