Sha256: 2ca5d0d62300a592520c1f61aa321a5df10f84f20326b7f7963590375ef329cf

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module Potassium
  # TODO: I don't know if this is a concern of this gem. Maybe we should move this later
  class TextSpinner
    attr_accessor :wait_condition, :base_message, :interval, :message_continuations,
      :counter, :started

    DEFAULT_ATTRIBUTES = {
      wait_condition: -> { },
      base_message: "",
      interval: 0.4,
      message_continuations: ["", ".", "..", "..."]
    }

    def initialize(attributes = {})
      DEFAULT_ATTRIBUTES.merge(attributes).each do |key, value|
        self.public_send("#{key}=", value)
      end
      self.started = false
      self.counter = 0
    end

    def start
      fail already_started_message if started
      self.started = true

      Thread.new do
        loop do
          break if wait_condition.call(counter)
          print_message
          sleep interval
          self.counter += 1
        end
        clear_message
        self.started = false
      end
    end

    private

    def print_message
      message_continuation = message_continuations[counter] || begin
        message_continuations[counter % message_continuations.size]
      end
      print "\r#{base_message}#{message_continuation}"
    end

    def clear_message
      print "\r#{" " * (base_message.size * 2)}\r"
    end

    def already_started_message
      "Please don't start this text spinner while is running. It can cause race conditions."
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
potassium-1.3.4 lib/potassium/text_spinner.rb
potassium-1.3.3 lib/potassium/text_spinner.rb
potassium-1.3.2 lib/potassium/text_spinner.rb
potassium-1.3.0 lib/potassium/text_spinner.rb