Class: Sus::Output::Bar

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/output/bar.rb

Overview

Represents a progress bar for displaying test execution progress.

Constant Summary collapse

BLOCK =

Unicode block characters for drawing the progress bar.

[
	" ",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
]
MINIMUM_WIDTH =

The minimum width for the progress bar.

8
MESSAGE_SUFFIX =

The suffix to append to messages.

": "

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current = 0, total = 0, message = nil) ⇒ Bar

Initialize a new progress bar.



27
28
29
30
31
32
33
# File 'lib/sus/output/bar.rb', line 27

def initialize(current = 0, total = 0, message = nil)
	@maximum_message_width = 0
	
	@current = current
	@total = total
	@message = message
end

Class Method Details

.register(output) ⇒ Object

Register progress bar styling with an output handler.



47
48
49
# File 'lib/sus/output/bar.rb', line 47

def self.register(output)
	output[:progress_bar] ||= output.style(:blue, :white)
end

Instance Method Details

Print the progress bar to the output.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sus/output/bar.rb', line 59

def print(output)
	width = output.width
	
	unless @total.zero?
		value = @current.to_f / @total.to_f
	else
		value = 0.0
	end
	
	if @message
		message = @message + MESSAGE_SUFFIX
		if message.size > @maximum_message_width
			@maximum_message_width = message.size
		end
		
		if @maximum_message_width < (width - MINIMUM_WIDTH)
			width -= @maximum_message_width
			message = message.rjust(@maximum_message_width)
		else
			@maximum_message_width = 0
			message = nil
		end
	end
	
	if message
		output.write(message)
	end
	
	output.write(
		:progress_bar, draw(value, width), :reset,
	)
	
	output.puts
end

#update(current, total, message) ⇒ Object

Update the progress bar values.



39
40
41
42
43
# File 'lib/sus/output/bar.rb', line 39

def update(current, total, message)
	@current = current
	@total = total
	@message = message
end