class Terminal::ProgressBar
Constants
- CR
- DECORATION_LENGTH
- DEFAULT_WIDTH
auto adjusting terminal size if required io/console
- EOL
- OptArg
@return [Class]
- SPACE
- STOP
- VERSION
Attributes
Public Class Methods
@param [Float] interval_sec @param [Hash] options @yield [instance] @yieldparam [ProgressBar] instance @yieldreturn [void] @return [void]
# File lib/terminal/progressbar/singleton_class.rb, line 31 def auto(interval_sec, options={}) interval_sec = Float interval_sec printing_thread = nil run options do |instance| printing_thread = Thread.new do loop do if instance.finished? break else instance.flush sleep interval_sec end end end yield instance end ensure printing_thread.join if printing_thread nil end
@param [Hash] options @option options [String, to_str] :body_char (also :mark) @option options [Integer, to_int] :max_count @option options [Integer, to_int] :max_width @option options [IO, StringIO, print, flush
] :output
# File lib/terminal/progressbar.rb, line 47 def initialize(options={}) opts = OptArg.parse options @body_char = opts.body_char @max_count = opts.max_count @max_width = opts.max_width @output = opts.output @pointer = 0 end
@param [Hash] options @yield [instance] @yieldparam [ProgressBar] instance @yieldreturn [void] @return [void]
# File lib/terminal/progressbar/singleton_class.rb, line 16 def run(options={}) instance = new options instance.flush yield instance ensure instance.finish! nil end
Public Instance Methods
@return [String]
# File lib/terminal/progressbar.rb, line 78 def bar "#{@body_char * current_bar_width}#{bar_padding}" end
@return [String]
# File lib/terminal/progressbar.rb, line 58 def body_char @body_char.dup end
@return [Integer]
# File lib/terminal/progressbar.rb, line 68 def current_bar_width percentage == 0 ? 0 : (max_bar_width * rational).to_int end
@param [Integer, to_int] step @return [step]
# File lib/terminal/progressbar.rb, line 128 def decrement(step=1) increment(-step) step end
@return [void]
# File lib/terminal/progressbar.rb, line 140 def fast_forward @pointer = @max_count nil end
# File lib/terminal/progressbar.rb, line 100 def finished? @pointer == @max_count end
@return [void]
# File lib/terminal/progressbar.rb, line 88 def flush @output.print line @output.print(finished? ? EOL : CR) @output.flush end
@param [Integer, to_int] step @return [step]
# File lib/terminal/progressbar.rb, line 119 def increment(step=1) new_pointer = @pointer + step.to_int raise InvalidPointingError unless pointable? new_pointer @pointer = new_pointer step end
@return [String]
# File lib/terminal/progressbar.rb, line 83 def line "#{percentage.to_s.rjust 3}% #{STOP}#{bar}#{STOP}" end
@return [Integer]
# File lib/terminal/progressbar.rb, line 63 def max_bar_width max_width - DECORATION_LENGTH end
@return [Fixnum] 1..100
# File lib/terminal/progressbar.rb, line 73 def percentage ((rational * (100 / @max_count)) * 100).to_int end
@param [Integer, to_int] point
# File lib/terminal/progressbar.rb, line 95 def pointable?(point) int = point.to_int (int >= 0) && (int <= @max_count) end
@param [Integer, to_int] point @return [point]
# File lib/terminal/progressbar.rb, line 110 def pointer=(point) int = point.to_int raise InvalidPointingError unless pointable? int @pointer = int point end
@return [void]
# File lib/terminal/progressbar.rb, line 134 def rewind @pointer = 0 nil end
Private Instance Methods
@return [String]
# File lib/terminal/progressbar.rb, line 159 def bar_padding SPACE * (max_bar_width - current_bar_width) end
@return [Rational] pointer / max_count
# File lib/terminal/progressbar.rb, line 164 def rational Rational @pointer, @max_count end