-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
748 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require File.join(File.dirname(__FILE__), "progress_bar", "options_parser") | ||
require File.join(File.dirname(__FILE__), "progress_bar", "length_calculator") | ||
require File.join(File.dirname(__FILE__), "progress_bar", "formatter") | ||
require File.join(File.dirname(__FILE__), "progress_bar", "components") | ||
require File.join(File.dirname(__FILE__), "progress_bar", "format") | ||
require File.join(File.dirname(__FILE__), "progress_bar", "base") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# | ||
# Ruby/ProgressBar - a text progress bar library | ||
# | ||
# Copyright (C) 2001-2005 Satoru Takabayashi <satoru@namazu.org> | ||
# All rights reserved. | ||
# This is free software with ABSOLUTELY NO WARRANTY. | ||
# | ||
# You can redistribute it and/or modify it under the terms | ||
# of Ruby's license. | ||
# | ||
|
||
module ProgressBar | ||
class Base | ||
include ProgressBar::OptionsParser | ||
include ProgressBar::LengthCalculator | ||
include ProgressBar::Formatter | ||
|
||
DEFAULT_OUTPUT_STREAM = STDERR | ||
DEFAULT_BAR_FORMAT = '%t: |%b|' | ||
|
||
def initialize(options = {}) | ||
@out = options[:output_stream] || DEFAULT_OUTPUT_STREAM | ||
|
||
@length_override = ENV['RUBY_PROGRESS_BAR_LENGTH'] || options[:length] | ||
|
||
@format = options[:format] || DEFAULT_BAR_FORMAT | ||
|
||
@title = ProgressBar::Title.new(title_options_from(options)) | ||
@bar = ProgressBar::Bar.new(bar_options_from(options)) | ||
# @estimated_time = ProgressBar::EstimatedTime | ||
# @elapsed_time = ProgressBar::ElapsedTime | ||
end | ||
|
||
def clear | ||
@out.print clear_string | ||
end | ||
|
||
def start(options = {}) | ||
clear | ||
|
||
@bar.current = options[:at] || @bar.current | ||
# @estimated_time.start | ||
# @elapsed_time.start | ||
|
||
update | ||
end | ||
|
||
def inc | ||
puts "#inc is deprecated. Please use #increment" | ||
increment | ||
end | ||
|
||
def increment | ||
@bar.increment | ||
# @previous_time = Time.now | ||
update | ||
end | ||
|
||
def title | ||
@title.to_s | ||
end | ||
|
||
def to_s(format_string = nil) | ||
format_string ||= @format | ||
|
||
format(format_string) | ||
end | ||
|
||
private | ||
attr_reader :out | ||
|
||
def bar(length) | ||
@bar.to_s(length) | ||
end | ||
|
||
def clear_string | ||
"\r#{" " * length}\r" | ||
end | ||
|
||
def update | ||
# return if finished? | ||
|
||
if length_changed? | ||
clear | ||
reset_length | ||
end | ||
|
||
@out.print self.to_s + "\r" | ||
@out.flush | ||
end | ||
|
||
# def reset | ||
# end | ||
|
||
# def inc(step = 1) | ||
# set(@current + step) | ||
# end | ||
|
||
# def set(progress) | ||
# unless progress_range.include? progress | ||
# raise "#{progress} is an invalid number. It must be between 0 and #{@total}." | ||
# end | ||
|
||
# @previous = @current | ||
# @current = progress | ||
# update | ||
# end | ||
|
||
# def finish | ||
# @current = @total | ||
# update | ||
# end | ||
|
||
# def halt | ||
# stop | ||
# end | ||
|
||
# def stop | ||
# update | ||
# end | ||
|
||
# def pause | ||
# update | ||
# end | ||
|
||
# def finished? | ||
# @current == @total | ||
# end | ||
|
||
# def percentage | ||
# progress_as_percentage(@current) | ||
# end | ||
|
||
# def inspect | ||
# "#<ProgressBar:#{@current}/#{@total}>" | ||
# end | ||
|
||
# private | ||
# def text | ||
# arguments = @format_arguments.map do |method| | ||
# method = sprintf("fmt_%s", method) | ||
# send(method) | ||
# end | ||
|
||
# sprintf(@format, *arguments) + eol | ||
# "PROGRESS BAR!!#{eol}" | ||
# end | ||
|
||
# def eol | ||
# if finished? then "\n" else "\r" end | ||
# end | ||
|
||
# def percentage_changed? | ||
# Use "!=" instead of ">" to support negative changes | ||
# current_percentage != previous_percentage | ||
# end | ||
|
||
# def time_elapsed? | ||
# Time.now - @previous_time >= 1 | ||
# end | ||
|
||
# alias :current_percentage, :percentage | ||
|
||
# def previous_percentage | ||
# progress_as_percentage(@previous) | ||
# end | ||
|
||
# def progress_as_percentage(progress) | ||
# (progress * 100 / @total).to_i | ||
# end | ||
|
||
# def progress_range | ||
# 0..@total | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require File.join(File.dirname(__FILE__), "components", "bar") | ||
require File.join(File.dirname(__FILE__), "components", "title") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module ProgressBar | ||
class Bar | ||
OPTIONS = [:total, :progress_mark, :beginning_position] | ||
|
||
DEFAULT_TOTAL = 100 | ||
DEFAULT_BEGINNING_POSITION = 0 | ||
DEFAULT_PROGRESS_MARK = 'o' | ||
|
||
#TODO These could be private right now. | ||
attr_reader :total | ||
attr_reader :current | ||
attr_reader :progress_mark | ||
|
||
def initialize(options = {}) | ||
@total = options[:total] || DEFAULT_TOTAL | ||
@current = options[:beginning_position] || DEFAULT_BEGINNING_POSITION | ||
|
||
raise "You can't set the bar's current value to be greater than the total." if current > total | ||
|
||
@progress_mark = options[:progress_mark] || DEFAULT_PROGRESS_MARK | ||
end | ||
|
||
def increment | ||
@current += 1 unless current == total | ||
end | ||
|
||
#TODO needs tested | ||
def current=(new_current) | ||
raise "You can't set the bar's current value to be greater than the total." if new_current > total | ||
|
||
@current = new_current | ||
end | ||
|
||
def percentage_completed | ||
# current / total * 100 | ||
# | ||
# Doing this way so we can avoid converting each | ||
# number to a float and then back to an integer. | ||
# | ||
current * 100 / total | ||
end | ||
|
||
def to_s(length) | ||
@length = length | ||
"#{complete_string}#{empty_string}" | ||
end | ||
|
||
private | ||
attr_reader :length | ||
|
||
def complete_string | ||
@progress_mark * completed_length | ||
end | ||
|
||
def completed_length | ||
length * percentage_completed / 100 | ||
end | ||
|
||
def empty_string | ||
" " * (length - completed_length) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module ProgressBar | ||
class Title | ||
OPTIONS = [:title, :title_length] | ||
|
||
DEFAULT_TITLE = 'Progress' | ||
|
||
attr_reader :text | ||
|
||
def initialize(options = {}) | ||
@text = options[:title] || DEFAULT_TITLE | ||
@length_override = options[:title_length] | ||
end | ||
|
||
def to_s | ||
text | ||
end | ||
|
||
# private | ||
# def visible_text | ||
# text.slice(0, visible_text_length) | ||
# end | ||
|
||
# def visible_text_length | ||
# @length_override || text.length | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require File.join(File.dirname(__FILE__), "format", "molecule") | ||
require File.join(File.dirname(__FILE__), "format", "base") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module ProgressBar | ||
module Format | ||
class Base | ||
attr_reader :molecules | ||
|
||
def initialize(format_string) | ||
@molecules = parse(format_string) | ||
end | ||
|
||
def non_bar_molecules | ||
molecules.select { |molecule| !molecule.bar_molecule? } | ||
end | ||
|
||
def bar_molecules | ||
molecules.select { |molecule| molecule.bar_molecule? } | ||
end | ||
|
||
private | ||
def parse(format_string) | ||
molecules = [] | ||
|
||
format_string.scan(/%([tTb])/) do |match| | ||
molecules << Molecule.new(match[0]) | ||
end | ||
|
||
molecules | ||
end | ||
|
||
# @title_width = 14 | ||
# @format = "%-#{@title_width}s %3d%% %s %s" | ||
# @format_arguments = [:title, :percentage, :bar, :stat] | ||
|
||
# def format=(format) | ||
# @format = format | ||
# end | ||
|
||
# def format_arguments=(arguments) | ||
# @format_arguments = arguments | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module ProgressBar | ||
module Format | ||
class Molecule | ||
MOLECULES = { | ||
:t => [:left_justified_title, :title], | ||
:T => [:right_justified_title, :title], | ||
:b => [:bar, :bar] | ||
# :elapsed_time => "%a", | ||
# :estimated_time_with_unknown => "%e", | ||
# :estimated_time_with_greater_than => "%E", | ||
# :force_estimated_time => "%f", | ||
# :percentage_complete_as_integer => "%p", | ||
# :percentage_complete_as_float => "%P", | ||
# :current_capacity => "%c", | ||
# :total_capacity => "%C", | ||
# :bar_with_percentage => "%B", | ||
# :reversed_bar => "%r", | ||
# :reversed_bar_with_percentage => "%R" | ||
} | ||
|
||
BAR_MOLECULES = %w{b} | ||
|
||
attr_reader :key | ||
attr_reader :method_name | ||
|
||
def initialize(letter) | ||
@key = letter | ||
@description, @method_name = MOLECULES[@key.to_sym] | ||
end | ||
|
||
def bar_molecule? | ||
BAR_MOLECULES.include? @key | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module ProgressBar | ||
module Formatter | ||
def format(format_string) | ||
@format_string = format_string | ||
@format = ProgressBar::Format::Base.new(format_string) | ||
process | ||
end | ||
|
||
def process | ||
processed_string = @format_string.dup | ||
|
||
@format.non_bar_molecules.each do |molecule| | ||
processed_string.gsub!("%#{molecule.key}", self.send(molecule.method_name)) | ||
end | ||
|
||
remaining_molecule_match_data = processed_string.match(/%[^%]/) | ||
remaining_molecules = remaining_molecule_match_data ? remaining_molecule_match_data.size : 0 | ||
placeholder_length = remaining_molecules * 2 | ||
|
||
leftover_bar_length = length - processed_string.length + placeholder_length | ||
|
||
@format.bar_molecules.each do |molecule| | ||
processed_string.gsub!("%#{molecule.key}", self.send(molecule.method_name, leftover_bar_length / remaining_molecules)) | ||
end | ||
|
||
processed_string | ||
end | ||
end | ||
end |
Oops, something went wrong.