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

current_count[R]
max_count[R]
max_width[R]
output[R]
pointer[R]

Public Class Methods

auto(interval_sec, options={}) { |instance| ... } click to toggle source

@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
new(options={}) click to toggle source

@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
run(options={}) { |instance| ... } click to toggle source

@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

bar() click to toggle source

@return [String]

# File lib/terminal/progressbar.rb, line 78
def bar
  "#{@body_char * current_bar_width}#{bar_padding}"
end
body_char() click to toggle source

@return [String]

# File lib/terminal/progressbar.rb, line 58
def body_char
  @body_char.dup
end
current_bar_width() click to toggle source

@return [Integer]

# File lib/terminal/progressbar.rb, line 68
def current_bar_width
  percentage == 0 ? 0 : (max_bar_width * rational).to_int
end
decrement(step=1) click to toggle source

@param [Integer, to_int] step @return [step]

# File lib/terminal/progressbar.rb, line 128
def decrement(step=1)
  increment(-step)
  step
end
end?()
Alias for: finished?
fast_forward() click to toggle source

@return [void]

# File lib/terminal/progressbar.rb, line 140
def fast_forward
  @pointer = @max_count
  nil
end
Also aliased as: finish
finish()
Alias for: fast_forward
finished?() click to toggle source
# File lib/terminal/progressbar.rb, line 100
def finished?
  @pointer == @max_count
end
Also aliased as: end?
flush() click to toggle source

@return [void]

# File lib/terminal/progressbar.rb, line 88
def flush
  @output.print line
  @output.print(finished? ? EOL : CR)
  @output.flush
end
increment(step=1) click to toggle source

@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
line() click to toggle source

@return [String]

# File lib/terminal/progressbar.rb, line 83
def line
  "#{percentage.to_s.rjust 3}% #{STOP}#{bar}#{STOP}"
end
max_bar_width() click to toggle source

@return [Integer]

# File lib/terminal/progressbar.rb, line 63
def max_bar_width
  max_width - DECORATION_LENGTH
end
percentage() click to toggle source

@return [Fixnum] 1..100

# File lib/terminal/progressbar.rb, line 73
def percentage
  ((rational * (100 / @max_count)) * 100).to_int
end
pointable?(point) click to toggle source

@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
pointer=(point) click to toggle source

@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
rewind() click to toggle source

@return [void]

# File lib/terminal/progressbar.rb, line 134
def rewind
  @pointer = 0
  nil
end

Private Instance Methods

bar_padding() click to toggle source

@return [String]

# File lib/terminal/progressbar.rb, line 159
def bar_padding
  SPACE * (max_bar_width - current_bar_width)
end
rational() click to toggle source

@return [Rational] pointer / max_count

# File lib/terminal/progressbar.rb, line 164
def rational
  Rational @pointer, @max_count
end