class Nanospinner

Constants

RING
VERSION

Public Class Methods

new(output_io = $stdout) click to toggle source

Creates a new Spinner printing to the given IO. revolved previously the previous message will be erased.

# File lib/nanospinner.rb, line 7
def initialize(output_io = $stdout)
  @out_io, @counter, @erase_last_n = output_io, 0, 0
end

Public Instance Methods

spin(message) click to toggle source

Revolve the spinner with a message. If the spinner has been revolved previously the previous message will be erased.

# File lib/nanospinner.rb, line 13
def spin(message)
  current = ring[@counter % ring.length]
  @counter += 1
  current_output = " %s %s" % [current, message]
  write_and_flush "\r" * @erase_last_n
  write_and_flush current_output
ensure
  @erase_last_n = current_output.length
end

Private Instance Methods

ring() click to toggle source

Returns the ring of elements displaying the spinner

# File lib/nanospinner.rb, line 26
def ring
  RING
end
write_and_flush(str) click to toggle source

Writes to the @out_io and immediately flushes it

# File lib/nanospinner.rb, line 31
def write_and_flush(str)
  @out_io << str
  @out_io.flush if @out_io.respond_to?(:flush)
end