class ConProgressBar

@author Marek K. <m.k@mk16.de, mk16.de> @example

puts "Build ..."
pb = ConProgressBar.new
100.times { |x|
  pb.up!
  sleep 0.01
}
pb.delete
puts "Complete."

@example

require "conprogressbar"
puts "Catch ..."
pb = ConProgressBar.new 0, ".", ">", "<", ",", STDERR
100.times { |x|
  pb.update pb.percent + 1
  sleep 0.01
}
pb.update 0
puts

@note The file conprogessbar.rb is available under the {www.gnu.org/licenses/gpl.txt GNU GPL v3}.

Public Class Methods

new(percent=0, symbol="=", beg="<", en=">", space=" ", stream=STDOUT) click to toggle source

Initializes the class ConProgressBar and thus a new progressbar on the console. @note There should be no other issues in the same stream as long as the ProgressBar is used. @note The arguments symbol, beg, en and space must have a length of one. @param percent [Integer] The percentage with which the progressbar should be initialized. @param symbol [String] The symbol which draws the already loaded area on the Progressbar. @param beg [String] The symbol for the introduction of the Progressbar. @param en [String] The symbol that initiates the end of the progressbar. @param space [String] The symbol that fills the not yet loaded part of the progress bar. @param stream [IO] The stream to which the progressbar should be written. @raise [ArgumentError] Dispatched when one of the arguments has not been specified correctly.

# File lib/conprogressbar.rb, line 66
def initialize percent=0, symbol="=", beg="<", en=">", space=" ", stream=STDOUT
  raise ArgumentError, "Argument 1" if percent < 0 || percent > 100
  raise ArgumentError, "Argument 2" if symbol.length != 1
  raise ArgumentError, "Argument 3" if beg.length != 1
  raise ArgumentError, "Argument 4" if en.length != 1
  raise ArgumentError, "Argument 5" if space.length != 1
  
  @percent = percent
  @symbol = symbol
  @beg = beg
  @en = en
  @space = space
  @stream = stream
  
  print @beg
  percent.times {
    @stream.print @symbol
  }
  (100-percent).times {
    @stream.print @space
  }
  out = percent.to_s
  while out.length < 3
    out = "0" + out
  end
  @stream.print "#{@en} #{out}%"
  @stream.flush
end

Public Instance Methods

delete() click to toggle source

Deletes the progressbar from the stream @return [NilClass] nil

# File lib/conprogressbar.rb, line 153
def delete
  107.times { @stream.print "\b" }
  107.times { @stream.print "\0" }
  107.times { @stream.print "\b" }
  @stream.flush
end
down!() click to toggle source

Decreases the percentage of progressbar by one and updates the prompts bar. @return [NilClass] nil @raise [RuntimeError] Will be triggered when the percentage can not be lowered, because the minimum has already been reached.

# File lib/conprogressbar.rb, line 113
def down!
  raise RuntimeError, "Down" if @percent == 0
  @percent -= 1
  self.update
end
percent() click to toggle source

Returns the current percentage @return [Integer] The current percentage of the progressbar

# File lib/conprogressbar.rb, line 97
def percent
  @percent
end
up!() click to toggle source

Increases the percentage of progressbar by one and updates the prompts bar. @return [NilClass] nil @raise [RuntimeError] Is triggered when the percentage can not be increased, because the maximum has already been reached.

# File lib/conprogressbar.rb, line 104
def up!
  raise RuntimeError, "Down" if @percent == 100
  @percent += 1
  self.update
end
update(percent=nil) click to toggle source

Updates the progressbar. It is possible to specify a percentage that should be set. @param percent [Integer, NilClass] The percentage to which the progressbar should be updated. Without specification (nil) the progress bar is updated to the already defined number. @return [NilClass] nil @raise [ArgumentError] Dispatched when the percent argument is less than 0 or greater than 100.

# File lib/conprogressbar.rb, line 124
def update percent=nil
  if percent == nil
    percent = @percent
  else
    raise ArgumentError, "Argument 1" if percent < 0 || percent > 100
    @percent = percent
  end
  
  (106).times {
    @stream.print "\b"
  }
  
  percent.times {
    @stream.print @symbol
  }
  (100-percent).times {
    @stream.print @space
  }
  out = percent.to_s
  while out.length < 3
    out = "0" + out
  end
  @stream.print "#{@en} #{out}%"
  @stream.flush
  nil
end