Sha256: f58fa7d8ce3e146695a258383c72fe79912bf6db49a84258cd6d0ba5c598e9ef

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

= ANSI::Progressbar

Pretty progress bars are easy to construct.

  require 'ansi/progressbar'

  pbar = ANSI::Progressbar.new("Test Bar", 100)

Running the bar simply requires calling the #inc method during
a loop and calling #finish when done.

  100.times do |i|
    sleep 0.01
    pbar.inc
  end
  pbar.finish

We will use this same rountine in all the examples below, so lets
make a quick macro for it. Notice we have to use #reset first
before reusing the same progress bar.

  def run(pbar)
    pbar.reset
    100.times do |i|
      sleep 0.01
      pbar.inc
    end
    pbar.finish
    puts
  end

The progress bar can be stylized in almost any way.
The #format setter provides control over the parts
that appear on the line. FOr example, by default the
format is:

  pbar.format("%-14s %3d%% %s %s", :title, :percentage, :bar, :stat)

So lets vary it up to demonstrate the case.

  pbar.format("%-14s %3d%% %s %s", :title, :percentage, :stat, :bar)
  run(pbar)

The progress bar has an extra build in format intended for use with
file downloads called #transer_mode.

  pbar.transfer_mode
  run(pbar)

Calling this methods is the same as calling:

  pbar.format("%-14s %3d%% %s %s",:title, :percentage, :bar, :stat_for_file_transfer)
  run(pbar)

The #style setter allows each part of the line be modified with ANSI codes. And the
#bar_mark writer can be used to change the character used to make the bar.

  pbar.standard_mode
  pbar.style(:title => [:red], :bar=>[:blue])
  pbar.bar_mark = "="
  run(pbar)

QED.

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ansi-1.1.0 demo/progressbar.rd
ansi-1.0.1 demo/progressbar.rd
ansi-1.0.0 demo/progressbar.rd