SusSourceSusOutputBar

class Bar

Represents a progress bar for displaying test execution progress.

Definitions

BLOCK = [...]

Unicode block characters for drawing the progress bar.

Implementation

BLOCK = [
	" ",
	"▏",
	"▎",
	"▍",
	"▌",
	"▋",
	"▊",
	"▉",
	"█",
]

def initialize(current = 0, total = 0, message = nil)

Initialize a new progress bar.

Signature

parameter current Integer

The current progress value.

parameter total Integer

The total value.

parameter message String, nil

Optional message to display.

Implementation

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

def update(current, total, message)

Update the progress bar values.

Signature

parameter current Integer

The current progress value.

parameter total Integer

The total value.

parameter message String, nil

Optional message to display.

Implementation

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

def self.register(output)

Register progress bar styling with an output handler.

Signature

parameter output Output

The output handler to register with.

Implementation

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

MINIMUM_WIDTH = 8

The minimum width for the progress bar.

MESSAGE_SUFFIX = ": "

The suffix to append to messages.

def print(output)

Print the progress bar to the output.

Signature

parameter output Output

The output handler.

Implementation

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