class BusyIndicator

BusyIndicator provides a way to show 'activity' in a terminal-window, while the main thread does something else. For a usage example see the test-code at the bottom of this file and execute the file (e.g. ruby ./busy_indicator.rb)

Attributes

width[W]

Public Class Methods

new(start = true, width = nil) click to toggle source
# File lib/busy_indicator.rb, line 29
def initialize(start = true, width = nil)
  # defaults
  @width = width && width >= 3 ? width : 3
  @sequence = %w"OOO ooo ___ ooo"
  run if start
end

Public Instance Methods

run() click to toggle source

start busy-indicator with the current settings for width and sequence.

# File lib/busy_indicator.rb, line 43
def run() 
  @thr = busy_indicator(@width) if !@thr || !@thr.alive?
end
running?() click to toggle source

returns true or false, so that !stopped? == running?.

# File lib/busy_indicator.rb, line 56
def running?
  @thr.alive?
end
sequence=(seq) click to toggle source

change sequence for the following run().

# File lib/busy_indicator.rb, line 37
def sequence=(seq)
  @sequence = seq
  @width = seq[0].length
end
stop(comment = nil) click to toggle source

stop busy-indicator and show comment-string if given.

# File lib/busy_indicator.rb, line 48
def stop(comment = nil)
  @thr.terminate
  @thr.join(0.1)
  print ("\b" * @width)
  print ("%+#{@width}s\n" %comment) if comment
end
stopped?() click to toggle source

returns true or false, so that !stopped? == running?.

# File lib/busy_indicator.rb, line 61
def stopped?
  !running?
end

Private Instance Methods

busy_indicator(width) click to toggle source

does it.

# File lib/busy_indicator.rb, line 68
def busy_indicator(width)
  tobj = Thread.new() do
    loop do 
      @sequence.each do |s|
        print "%+#{width}s" %s
        sleep 0.1
        print ("\b" * width)
      end
    end
  end
end