Class: Console::Event::Progress

Inherits:
Generic
  • Object
show all
Defined in:
lib/console/event/progress.rb

Constant Summary collapse

BLOCK =
[
	" ",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#to_json

Constructor Details

#initialize(current, total) ⇒ Progress

Returns a new instance of Progress.



40
41
42
43
# File 'lib/console/event/progress.rb', line 40

def initialize(current, total)
	@current = current
	@total = total
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



45
46
47
# File 'lib/console/event/progress.rb', line 45

def current
  @current
end

#totalObject (readonly)

Returns the value of attribute total.



46
47
48
# File 'lib/console/event/progress.rb', line 46

def total
  @total
end

Class Method Details

.register(terminal) ⇒ Object



64
65
66
# File 'lib/console/event/progress.rb', line 64

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

Instance Method Details

#bar(value = self.value, width = 70) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/console/event/progress.rb', line 52

def bar(value = self.value, width = 70)
	blocks = width * value
	full_blocks = blocks.floor
	partial_block = ((blocks - full_blocks) * BLOCK.size).floor
	
	if partial_block.zero?
		BLOCK.last * full_blocks
	else
		"#{BLOCK.last * full_blocks}#{BLOCK[partial_block]}"
	end.ljust(width)
end

#format(output, terminal, verbose) ⇒ Object



72
73
74
# File 'lib/console/event/progress.rb', line 72

def format(output, terminal, verbose)
	output.puts "#{terminal[:progress_bar]}#{self.bar}#{terminal.reset} #{sprintf('%6.2f', self.value * 100)}%"
end

#to_hObject



68
69
70
# File 'lib/console/event/progress.rb', line 68

def to_h
	{current: @current, total: @total}
end

#valueObject



48
49
50
# File 'lib/console/event/progress.rb', line 48

def value
	@current.to_f / @total.to_f
end