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.



29
30
31
32
33
34
# 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

Instance Attribute Details

#width=(value) ⇒ Object (writeonly)

Sets the attribute width

Parameters:

  • value

    the value to set the attribute width to.



28
29
30
# File 'lib/busy_indicator.rb', line 28

def width=(value)
  @width = value
end

Instance Method Details

#runObject

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



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

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

#running?Boolean

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/busy_indicator.rb', line 56

def running?
  @thr.alive?
end

#sequence=(seq) ⇒ Object

change sequence for the following run().



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

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

#stop(comment = nil) ⇒ Object

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



48
49
50
51
52
53
# 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?Boolean

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

Returns:

  • (Boolean)


61
62
63
# File 'lib/busy_indicator.rb', line 61

def stopped?
  !running?
end