Sha256: 0486c53b9cf4636c1ce838d4e01b64fba79b84f5a5b9229c8dc391ff684fd02b

Contents?: true

Size: 1.13 KB

Versions: 20

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2022, by Samuel Williams.

require_relative 'generic'

module Console
	module Event
		class Progress < Generic
			BLOCK = [
				" ",
				"▏",
				"▎",
				"▍",
				"▌",
				"▋",
				"▊",
				"▉",
				"█",
			]
			
			def initialize(current, total)
				@current = current
				@total = total
			end
			
			attr :current
			attr :total
			
			def value
				@current.to_f / @total.to_f
			end
			
			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
			
			def self.register(terminal)
				terminal[:progress_bar] ||= terminal.style(:blue, :white)
			end
			
			def to_h
				{current: @current, total: @total}
			end
			
			def format(output, terminal, verbose)
				output.puts "#{terminal[:progress_bar]}#{self.bar}#{terminal.reset} #{sprintf('%6.2f', self.value * 100)}%"
			end
		end
	end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
console-1.24.0 lib/console/event/progress.rb
console-1.23.7 lib/console/event/progress.rb
console-1.23.6 lib/console/event/progress.rb
console-1.23.5 lib/console/event/progress.rb
console-1.23.4 lib/console/event/progress.rb
console-1.23.3 lib/console/event/progress.rb
console-1.23.2 lib/console/event/progress.rb
console-1.23.1 lib/console/event/progress.rb
console-1.23.0 lib/console/event/progress.rb
console-1.22.0 lib/console/event/progress.rb
console-1.21.0 lib/console/event/progress.rb
console-1.20.0 lib/console/event/progress.rb
console-1.19.0 lib/console/event/progress.rb
console-1.18.0 lib/console/event/progress.rb
console-1.17.4 lib/console/event/progress.rb
console-1.17.3 lib/console/event/progress.rb
console-1.17.2 lib/console/event/progress.rb
console-1.17.1 lib/console/event/progress.rb
console-1.17.0 lib/console/event/progress.rb
console-1.16.2 lib/console/event/progress.rb