module SpinningCursor

Public Instance Methods

alive?() click to toggle source

Determines whether the cursor thread is still running

# File lib/spinning_cursor.rb, line 100
def alive?
  @spinner and @spinner.alive?
end
run(&block)
Alias for: start
set_banner(banner) click to toggle source

Sets the banner message during execution

# File lib/spinning_cursor.rb, line 120
def set_banner(banner)
  begin
    @parsed.banner banner
  rescue NameError
    raise CursorNotRunning.new "Cursor isn't running... are you sure " +
      "you're calling this from an action block?"
  end
end
set_message(msg) click to toggle source

Sets the finish message (to be used inside the action for non-deterministic output)

# File lib/spinning_cursor.rb, line 108
def set_message(msg)
  begin
    @parsed.message msg
  rescue NameError
    raise CursorNotRunning.new "Cursor isn't running... are you sure " +
      "you're calling this from an action block?"
  end
end
setup(&block) click to toggle source
# File lib/spinning_cursor.rb, line 11
def setup(&block)
  @parsed = Parser.new(&block)
  @cursor = Cursor.new(@parsed)
  @setup = true
  self
end
setup?() click to toggle source
# File lib/spinning_cursor.rb, line 18
def setup?
  @setup
end
start(&block) click to toggle source

Sends passed block to Parser, and starts cursor thread It will execute the action block and kill the cursor thread if an action block is passed.

# File lib/spinning_cursor.rb, line 27
def start(&block)
  setup(&block) if block or not setup?
  stop if alive?

  save_stdout_sync_status
  capture_console
  hide_cursor

  @spinner = Thread.new do
    abort_on_exception = true
    @cursor.spin
  end

  @stop_watch = StopWatch.new

  if @parsed.action
    # The action
    begin
      @stop_watch.measure do
        @parsed.outer_scope_object.instance_eval &@parsed.action
      end
    rescue StandardError => e
      set_message "#{e.message}\n#{e.backtrace.join("\n")}"
      raise
    ensure
      stop
    end
  else
    # record start time
    @stop_watch.start
  end
end
Also aliased as: run
stop() click to toggle source

Kills the cursor thread and prints the finished message Returns execution time

# File lib/spinning_cursor.rb, line 66
def stop
  begin
    @spinner.kill
    # Wait for the cursor to die -- can cause problems otherwise
    @spinner.join
    # Set cursor to nil so set_banner method only works
    # when cursor is actually running.
    @cursor = nil

    restore_stdout_sync_status
    if console_captured?
      $console.print ESC_R_AND_CLR + $stdout.string
      release_console
    end
    show_cursor

    reset_line
    puts @parsed.message
    # Set parsed to nil so set_message method only works
    # when cursor is actually running.
    @parsed = nil
    @setup = false

    # Return execution time
    @stop_watch.stop
    @stop_watch.timing
  rescue NameError
    raise CursorNotRunning.new "Can't stop, no cursor running."
  end
end