Skip to content

Commit

Permalink
Add Console::Event::Progress for rendering progress bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 26, 2020
1 parent 09a1a36 commit 73422e6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/console/event.rb
Expand Up @@ -20,3 +20,4 @@

require_relative 'event/spawn'
require_relative 'event/failure'
require_relative 'event/progress'
71 changes: 71 additions & 0 deletions lib/console/event/progress.rb
@@ -0,0 +1,71 @@
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

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, nil, :bold)
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
38 changes: 6 additions & 32 deletions lib/console/measure.rb
Expand Up @@ -20,32 +20,6 @@

module Console
class Measure
module Bar
BLOCK = [
" ",
"▏",
"▎",
"▍",
"▌",
"▋",
"▊",
"▉",
"█",
]

def self.format(value, width)
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
end

def initialize(output, subject, total = 0)
@output = output
@subject = subject
Expand All @@ -60,6 +34,10 @@ def duration
Time.now - @start_time
end

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

def remaining
@total - @current
end
Expand All @@ -79,17 +57,13 @@ def estimated_remaining_time
def increment(amount = 1)
@current += amount

@output.info(@subject, self) {self.formatted_progress_bar}
@output.info(@subject, self) {Event::Progress.new(@current, @total)}

return self
end

def mark(*arguments)
@output.info(@subject, *arguments) {self.formatted_progress_bar}
end

def formatted_progress_bar(width = 70)
"[#{Bar.format(@current.to_f / @total.to_f, width)}]"
@output.info(@subject, *arguments)
end

def to_s
Expand Down

0 comments on commit 73422e6

Please sign in to comment.