Sha256: ed3155c0c4ae7478448c4e059f33302372b3b3a103830c5d8db77be1a85f6e94

Contents?: true

Size: 2 KB

Versions: 59

Compression:

Stored size: 2 KB

Contents

require 'ostruct'

module ZTK

  # Spinner Error Class
  #
  # @author Zachary Patten <zachary AT jovelabs DOT com>
  class SpinnerError < Error; end

  # 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 Zachary Patten <zachary AT jovelabs DOT com>
  class Spinner

    class << self

      # Spinner Character Set
      CHARSET = %w( | / - \\ )

      # UI Spinner
      #
      # Displays a "spinner" while executing the supplied block.  It is
      # advisable that no output is sent to the console during this time.
      #
      # @param [Hash] options Configuration options hash.
      # @option options [Float,Integer] :step (0.1) How long to sleep for in
      #   between cycles, while cycling through the *CHARSET*.
      #
      # @yield 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".
      # @return [Object] The return value of the block.
      #
      # @author Zachary Patten <zachary AT jovelabs DOT com>
      # @author Stephen Nelson-Smith <stephen@atalanta-systems.com>
      def spin(options={}, &block)
        options = Base.build_config({
          :step => 0.1
        }.merge(options))
        options.ui.logger.debug { "options(#{options.send(:table).inspect})" }

        !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

    end

  end

end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
ztk-1.15.0 lib/ztk/spinner.rb
ztk-1.14.1 lib/ztk/spinner.rb
ztk-1.14.0 lib/ztk/spinner.rb
ztk-1.13.0 lib/ztk/spinner.rb
ztk-1.12.0 lib/ztk/spinner.rb
ztk-1.11.1 lib/ztk/spinner.rb
ztk-1.11.0 lib/ztk/spinner.rb
ztk-1.10.6 lib/ztk/spinner.rb
ztk-1.10.5 lib/ztk/spinner.rb
ztk-1.10.4 lib/ztk/spinner.rb
ztk-1.10.3 lib/ztk/spinner.rb
ztk-1.10.2 lib/ztk/spinner.rb
ztk-1.10.1 lib/ztk/spinner.rb
ztk-1.10.0 lib/ztk/spinner.rb
ztk-1.9.1 lib/ztk/spinner.rb
ztk-1.9.0 lib/ztk/spinner.rb
ztk-1.8.0 lib/ztk/spinner.rb
ztk-1.7.1 lib/ztk/spinner.rb
ztk-1.7.0 lib/ztk/spinner.rb
ztk-1.6.30 lib/ztk/spinner.rb