Sha256: 0c3c425fe3f9277dbc0ac2fec0c40857e5cc7cb5f20e64fc7cbd3eaf7cdf4563

Contents?: true

Size: 822 Bytes

Versions: 1

Compression:

Stored size: 822 Bytes

Contents

module SpinR
  class Spinner
    COLORS = %i[
      black light_black red light_red green light_green yellow light_yellow blue light_blue magenta
      light_magenta cyan light_cyan white light_white default
    ].freeze

    def initialize(spinner = nil, color = nil)
      @spinner = spinner || SpinR::Spinners::TRADITIONAL
      @color = color
    end

    def worker(&block)
      with_spin(&block)
    end

    private

    def with_spin
      require 'colorize'
      chars  = @spinner.clone.dup
      thread = Thread.new { yield }

      while thread.alive?
        spin_text = " #{chars[0]}\r".bold
        spin_text = spin_text.send(@color.to_s) if COLORS.include? @color
        print spin_text
        sleep 0.1
        print "\b\b"
        chars.push chars.shift
      end

      thread.join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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