Class: BusyIndicator

Inherits:
Object
  • Object
show all
Defined in:
lib/busy_indicator.rb

Overview

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = true, width = nil) ⇒ BusyIndicator

Returns a new instance of BusyIndicator.



22
23
24
25
26
27
# File 'lib/busy_indicator.rb', line 22

def initialize(start = true, width = nil)
  # defaults
  @width = width && width >= 3 ? width : 3
  @sequence = %w"OOO ooo ___ ooo"
  run if start
end

Instance Attribute Details

#width=(value) ⇒ Object (writeonly)

Sets the attribute width

Parameters:

  • value

    the value to set the attribute width to.



21
22
23
# File 'lib/busy_indicator.rb', line 21

def width=(value)
  @width = value
end

Instance Method Details

#runObject

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



36
37
38
# File 'lib/busy_indicator.rb', line 36

def run() 
  @thr = busy_indicator(@width) if !@thr || !@thr.alive?
end

#running?Boolean

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

Returns:

  • (Boolean)


49
50
51
# File 'lib/busy_indicator.rb', line 49

def running?
  @thr.alive?
end

#sequence=(seq) ⇒ Object

change sequence for the following run().



30
31
32
33
# File 'lib/busy_indicator.rb', line 30

def sequence=(seq)
  @sequence = seq
  @width = seq[0].length
end

#stop(comment = nil) ⇒ Object

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



41
42
43
44
45
46
# File 'lib/busy_indicator.rb', line 41

def stop(comment = nil)
  @thr.terminate
  @thr.join(0.1)
  print ("\b" * @width)
  print ("%+#{@width}s\n" %comment) if comment
end

#stopped?Boolean

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/busy_indicator.rb', line 54

def stopped?
  !running?
end