Class: ZTK::Spinner

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

Overview

Spinner Class

This class can be used to display an "activity indicator" to a user while a task is executed in the supplied block. This indicator takes the form of a spinner.

Author:

Constant Summary

CHARSET =

Spinner Character Set

%w( | / - \\ )

Class Method Summary (collapse)

Class Method Details

+ (Object) spin(options = {}) { ... }

UI Spinner

Displays a "spinner" while executing the supplied block. It is advisable that no output is sent to the console during this time.

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options hash.

Options Hash (options):

  • :step (Float, Integer) — default: 0.1

    How long to sleep for in between cycles, while cycling through the CHARSET.

Yields:

  • Block should execute the tasks for which they wish the user to have a false sense of security in the fact that something is actually taking place "behind the scenes".

Returns:

  • (Object)

    The return value of the block.

Author:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ztk/spinner.rb', line 40

def spin(options={}, &block)
  options = Base.build_config({
    :step => 0.1
  }, options)

  !block_given? and Base.log_and_raise(options.ui.logger, SpinnerError, "You must supply a block!")

  count = 0

  spinner = Thread.new do
    while (count >= 0) do
      options.ui.stdout.print(CHARSET[(count += 1) % CHARSET.length])
      options.ui.stdout.print("\b")
      options.ui.stdout.respond_to?(:flush) and options.ui.stdout.flush
      sleep(options.step)
    end
  end

  yield.tap do
    count = -100
    spinner.join
  end
end