Class: BusyIndicator

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

Overview

An object of this class indicates activity. It does so asynchronously until interrupted.

Examples: —————————————- # 1) start default-animation immediately

width = 20 2.times do puts “I am busy, pse wait…” thr = BusyIndicator.new(true, width) sleep 3 thr.stop(“Okay”) end # 2) create object, start animation later

bi = BusyIndicator.new(false, width) bi.run sleep 3 bi.stop(“Stopped”)

# 3) change the width of the animation bi.width = 8 bi.run sleep 3 bi.stop(“stopped again”)

# 4) use a diffferent animation sequence

bi = BusyIndicator.new(true, 20, animation=>%w~… ,,, ;;; :

||| 777 >>> _~)

sleep 3 bi.stop

# 5) animate the output of a block

tr = %w“a b c d e f g” i = nil bi = BusyIndicator.new(true, 20 ) do i = (i && i < tr.size ? i + 1 : 0) tr end sleep 3 bi.stop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructor. All arguments are optional; set 'start' to true to start immediately, define the width of the output as a number of visible characters. The variable 'animation' can contain an array of symbols which will be printed sequentially to create an animation.

If a block is given, its output will be printed repeatedly, instead of the animation-symbols. In the block, consider protecting shared ressources with a Mutex.


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/busy_indicator.rb', line 83

def initialize(start = true, width = nil, animation = nil, &b)
	
	# @animation = %w"OOO ooo ___ ooo"
	# @animation = %w"||||||| ||||||( |||||(- |||(-)| ||(-)|| |(-)||| (-)|||| -)||||| )|||||"

	@animation = (animation || %w"||| ||| ((( <<< ––– ––– >>> )))")
	@proc = b if b

	l = @animation.max {|a, b| a.length <=> b.length}.length

	@width = (width && width >= l ? width : l)
	@thr = busy_indicator(@width) if start
end

Instance Attribute Details

#width=(value) ⇒ Object (writeonly)

Sets the attribute width

Parameters:

  • value

    the value to set the attribute width to.


72
73
74
# File 'lib/busy_indicator.rb', line 72

def width=(value)
  @width = value
end

Instance Method Details

#runObject

start the animation


97
98
99
# File 'lib/busy_indicator.rb', line 97

def run() 
	@thr = busy_indicator(@width)
end

#stop(comment = nil) ⇒ Object

stop the animation and print the comment.


101
102
103
104
105
106
# File 'lib/busy_indicator.rb', line 101

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