Sha256: e40f4a41279b999ee39daa8d5202386717f9f2db3b29a1619761783537f529a9

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require "shell-spinner/version"

module ShellSpinner

  def self.wrap text = nil, &block
    with_message text do
      join_spinner_thread(block)
    end
  end

  private

    def self.with_message text = nil
      require 'colorize'

      begin
        print "#{text}... " unless text.nil?
        yield
        print "done\n".colorize(:green) unless text.nil?
      rescue Exception => e
        print "fail\n".colorize(:red) unless text.nil?
        re_raise_exception e
      end
    end

    def self.join_spinner_thread proc
      chars  = %w{ | / - \\ }
      thread = Thread.new { proc.call }

      while thread.alive?
        print chars[0]
        sleep 0.1
        print "\b"

        chars.push chars.shift
      end

      thread.join
    end

    def self.re_raise_exception e
      raise begin
        new_exception = e.class.new(e.message)
        new_exception.set_backtrace e.backtrace
        new_exception
      end
    end
end

def ShellSpinner text = nil, &block
  ShellSpinner.wrap text, &block
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shell-spinner-1.0.1 lib/shell-spinner.rb