Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit (based on Ruby/ProgressBar 0.9).

  • Loading branch information...
commit 7b1c474983158a00c88519d699c167a3789062c4 0 parents
Satoru Takabayashi authored nex3 committed
113 ChangeLog
@@ -0,0 +1,113 @@
+2005-05-21 Satoru Takabayashi <satoru@namazu.org>
+
+ * progressbar.rb (ProgressBar::show): Call IO#flush. Suggestted by
+ "Geert Fannes" <Geert.Fannes at ikanconsulting.com>
+
+2005-01-07 Satoru Takabayashi <satoru@namazu.org>
+
+ * progressbar.rb (ProgressBar::VERSION): Bumped version number to 0.9.
+ (ProgressBar::finished?): New method.
+ (ProgressBar::current): New attribute.
+ (ProgressBar::total): New attribute.
+ (ProgressBar::title): New attribute.
+ (ProgressBar::start_time): New attribute.
+ (ProgressBar::inspect): Change the format.
+ (ProgressBar::show_if_needed): Renamed from show_progress.
+ (ProgressBar::clear): New method.
+ (ProgressBar::initialize): Renamed a field: bar_width ->
+ terminal_width.
+ (ProgressBar::do_percentage): New method.
+ (ProgressBar::percentage): Use it.
+ (ReversedProgressBar): New class.
+ (ProgressBar::initialize): Use #clear.
+ (ProgressBar::fmt_bar): Ditto.
+ (ProgressBar::fmt_percentage): Renamed from percentage.
+ (ProgressBar::fmt_stat): Ditto.
+ (ProgressBar::fmt_stat_for_file_transfer): Ditto.
+ (ProgressBar::fmt_title): Ditto.
+
+ * test.rb: Use Test::Unit.
+
+ * Makefile (docs): Removed.
+ (progressbar.ja.html): Ditto.
+ (progressbar.en.html): Ditto.
+ (dist): New rule.
+ (check): New rule.
+
+2004-02-05 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.8 released!
+
+ * progressbar.rb (ProgressBar::set): Fixed the bug when caused by
+ the invalid count. <http://bugs.debian.org/231009>
+ (ProgressBar::VERSION): Bumped version number to 0.8.
+
+2004-01-19 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.7 released!
+
+ * progressbar.rb (ProgressBar::initialize): Rename a field:
+ @bar_length -> @bar_width.
+ (ProgressBar::initialize): New field: @title_width.
+ (ProgressBar::title): use it.
+ (ProgressBar::initialize): New field: @previous_time.
+ (ProgressBar::show_progress): Use it and update the progress bar
+ if one sec. elapsed.
+ (ProgressBar::initialize): Call show instead of show_progress.
+
+2004-01-16 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.6 released!
+
+ * progressbar.rb (ProgressBar::show): Remove the useless condition
+ after "else". Thanks to Neil Spring <nspring@cs.washington.edu>
+ for the report.
+ <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=227966>
+ (ProgressBar):
+
+2003-07-24 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.5 released!
+
+ * progressbar.rb (ProgressBar::initialize): New parameter: config.
+ (ProgressBar::convert_prefix_multiplier): New method.
+ (ProgressBar::transfer_rate): New method.
+ (ProgressBar::percentage): New method.
+ (ProgressBar::title): New method.
+ (ProgressBar::initialize): New fields: @bar_mark, @format,
+ @arguments.
+ (ProgressBar::get_width): New method.
+ (ProgressBar::stat_for_file_transfer): New method.
+ (ProgressBar::stat): Renamed from time.
+ (ProgressBar::format=): New method.
+ (ProgressBar::format_arguments=): New method.
+ (ProgressBar::convert_bytes): New method.
+ (ProgressBar::file_transfer_mode): New method.
+
+2002-10-21 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.4 released!
+
+ * progressbar.rb (ProgressBar::halt): New method. allowing a
+ "broken" ProgressBar in the event of error.
+ - Suggestted by Austin Ziegler <austin@halostatue.ca>
+
+2002-01-05 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.3 released!
+
+ * progressbar.rb (ProgressBar::show): Shorten @title to fall into
+ the format well.
+
+2001-11-03 Satoru Takabayashi <satoru@namazu.org>
+
+ * Ruby/ProgressBar: Version 0.2 released!
+
+ * progressbar.rb (ProgressBar::eta): Remove an unnecessary condition.
+
+ * progressbar.rb (ProgressBar::eta): Fix the return value bug.
+
+ * progressbar.rb (ProgressBar::inc): Add an optional parameter `step'.
+
+ * Ruby/ProgressBar: Version 0.1 released!
+
103 progressbar.en.rd
@@ -0,0 +1,103 @@
+=begin
+index:Ej
+
+= Ruby/ProgressBar: A Text Progress Bar Library for Ruby
+
+Last Modified: 2005-05-22 00:28:04
+
+--
+
+Ruby/ProgressBar is a text progress bar library for Ruby.
+It can indicate progress with percentage, a progress bar,
+and estimated remaining time.
+
+The latest version of Ruby/ProgressBar is available at
+((<URL:http://namazu.org/~satoru/ruby-progressbar/>))
+.
+
+== Examples
+
+ % irb --simple-prompt -r progressbar
+ >> pbar = ProgressBar.new("test", 100)
+ => (ProgressBar: 0/100)
+ >> 100.times {sleep(0.1); pbar.inc}; pbar.finish
+ test: 100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
+ => nil
+
+ >> pbar = ProgressBar.new("test", 100)
+ => (ProgressBar: 0/100)
+ >> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
+ test: 67% |oooooooooooooooooooooooooo | ETA: 00:00:03
+
+== API
+
+--- ProgressBar#new (title, total, out = STDERR)
+ Display the initial progress bar and return a
+ ProgressBar object. ((|title|)) specifies the title,
+ and ((|total|)) specifies the total cost of processing.
+ Optional parameter ((|out|)) specifies the output IO.
+
+ The display of the progress bar is updated when one or
+ more percent is proceeded or one or more seconds are
+ elapsed from the previous display.
+
+--- ProgressBar#inc (step = 1)
+ Increase the internal counter by ((|step|)) and update
+ the display of the progress bar. Display the estimated
+ remaining time on the right side of the bar. The counter
+ does not go beyond the ((|total|)).
+
+--- ProgressBar#set (count)
+ Set the internal counter to ((|count|)) and update the
+ display of the progress bar. Display the estimated
+ remaining time on the right side of the bar. Raise if
+ ((|count|)) is a negative number or a number more than
+ the ((|total|)).
+
+--- ProgressBar#finish
+ Stop the progress bar and update the display of progress
+ bar. Display the elapsed time on the right side of the bar.
+ The progress bar always stops at 100 % by the method.
+
+--- ProgressBar#halt
+ Stop the progress bar and update the display of progress
+ bar. Display the elapsed time on the right side of the bar.
+ The progress bar stops at the current percentage by the method.
+
+--- ProgressBar#format=
+ Set the format for displaying a progress bar.
+ Default: "%-14s %3d%% %s %s".
+
+--- ProgressBar#format_arguments=
+ Set the methods for displaying a progress bar.
+ Default: [:title, :percentage, :bar, :stat].
+
+--- ProgressBar#file_transfer_mode
+ Use :stat_for_file_transfer instead of :stat to display
+ transfered bytes and transfer rate.
+
+
+ReverseProgressBar class is also available. The
+functionality is identical to ProgressBar but the direction
+of the progress bar is just opposite.
+
+== Limitations
+
+Since the progress is calculated by the proportion to the
+total cost of processing, Ruby/ProgressBar cannot be used if
+the total cost of processing is unknown in advance.
+Moreover, the estimation of remaining time cannot be
+accurately performed if the progress does not flow uniformly.
+
+== Download
+
+Ruby/ProgressBar is a free software with ABSOLUTELY NO WARRANTY
+under the terms of Ruby's license.
+
+ * ((<URL:http://namazu.org/~satoru/ruby-progressbar/ruby-progressbar-0.9.tar.gz>))
+ * ((<URL:http://cvs.namazu.org/ruby-progressbar/>))
+
+--
+
+- ((<Satoru Takabayashi|URL:http://namazu.org/~satoru/>)) -
+=end
100 progressbar.ja.rd
@@ -0,0 +1,100 @@
+=begin
+index:eJ
+
+= Ruby/ProgressBar: プログレスバーをテキストで表示する Ruby用のライブラリ
+
+最終更新日: 2005-05-22 00:28:53
+
+
+--
+
+Ruby/ProgressBar はプログレスバーをテキストで表示する Ruby
+のライブラリです。処理の進捗状況をパーセント、プログレスバー、
+および推定残り時間として表示します。
+
+最新版は
+((<URL:http://namazu.org/~satoru/ruby-progressbar/>))
+から入手可能です
+
+== 使用例
+
+ % irb --simple-prompt -r progressbar
+ >> pbar = ProgressBar.new("test", 100)
+ => (ProgressBar: 0/100)
+ >> 100.times {sleep(0.1); pbar.inc}; pbar.finish
+ test: 100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
+ => nil
+
+ >> pbar = ProgressBar.new("test", 100)
+ => (ProgressBar: 0/100)
+ >> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
+ test: 67% |oooooooooooooooooooooooooo | ETA: 00:00:03
+
+== API
+
+--- ProgressBar#new (title, total, out = STDERR)
+ プログレスバーの初期状態を表示し、新しい ProgressBarオブ
+ ジェクトを返す。((|title|)) で見出しを、((|total|)) で処
+ 理の総計を、((|out|)) で出力先の IO を設定する。
+
+ プログレスバーの表示は、前回の表示から進捗が 1%以上あっ
+ たとき、あるいは 1秒以上経過した場合に更新されます。
+
+--- ProgressBar#inc (step = 1)
+ 内部のカウンタを ((|step|)) 数だけ進めて、プログレスバー
+ の表示を更新する。バーの右側には推定残り時間を表示する。
+ カウンタは ((|total|)) を越えて進むことはない。
+
+--- ProgressBar#set (count)
+ カウンタの値を ((|count|)) に設定し、プログレスバーの
+ 表示を更新する。バーの右側には推定残り時間を表示する。
+ ((|count|)) にマイナスの値あるいは ((|total|)) より大き
+ い値を渡すと例外が発生する。
+
+--- ProgressBar#finish
+ プログレスバーを停止し、プログレスバーの表示を更新する。
+ プログレスバーの右側には経過時間を表示する。
+ このとき、プログレスバーは 100% で終了する。
+
+--- ProgressBar#halt
+ プログレスバーを停止し、プログレスバーの表示を更新する。
+ プログレスバーの右側には経過時間を表示する。
+ このとき、プログレスバーはその時点のパーセンテージで終了する。
+
+--- ProgressBar#format=
+ プログレスバー表示のフォーマットを設定する。
+ 未変更時は "%-14s %3d%% %s %s"
+
+--- ProgressBar#format_arguments=
+ プログレスバー表示に使う関数を設定する。
+ 未変更時は [:title, :percentage, :bar, :stat]
+ ファイル転送時には :stat の変わりに :stat_for_file_transfer
+ を使うと転送バイト数と転送速度を表示できる。
+
+--- ProgressBar#file_transfer_mode
+ プログレスバー表示に :stat の変わりに :stat_for_file_transfer
+ を使い、転送バイト数と転送速度を表示する。
+
+
+ReverseProgressBar というクラスも提供されます。機能は
+ProgressBar とまったく同じですが、プログレスバーの進行方向が
+逆になっています。
+
+== 制限事項
+
+進捗状況を処理の総計に対する割合として計算するため、処理の総
+計が事前にわからない状況では使えません。また、進捗の流れが均
+一でないときには残り時間の推定は正しく行えません。
+
+== ダウンロード
+
+Ruby のライセンスに従ったフリーソフトウェアとして公開します。
+完全に無保証です。
+
+ * ((<URL:http://namazu.org/~satoru/ruby-progressbar/ruby-progressbar-0.9.tar.gz>))
+ * ((<URL:http://cvs.namazu.org/ruby-progressbar/>))
+
+--
+
+- ((<Satoru Takabayashi|URL:http://namazu.org/~satoru/>)) -
+=end
236 progressbar.rb
@@ -0,0 +1,236 @@
+#
+# 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.
+#
+
+class ProgressBar
+ VERSION = "0.9"
+
+ def initialize (title, total, out = STDERR)
+ @title = title
+ @total = total
+ @out = out
+ @terminal_width = 80
+ @bar_mark = "o"
+ @current = 0
+ @previous = 0
+ @finished_p = false
+ @start_time = Time.now
+ @previous_time = @start_time
+ @title_width = 14
+ @format = "%-#{@title_width}s %3d%% %s %s"
+ @format_arguments = [:title, :percentage, :bar, :stat]
+ clear
+ show
+ end
+ attr_reader :title
+ attr_reader :current
+ attr_reader :total
+ attr_accessor :start_time
+
+ private
+ def fmt_bar
+ bar_width = do_percentage * @terminal_width / 100
+ sprintf("|%s%s|",
+ @bar_mark * bar_width,
+ " " * (@terminal_width - bar_width))
+ end
+
+ def fmt_percentage
+ do_percentage
+ end
+
+ def fmt_stat
+ if @finished_p then elapsed else eta end
+ end
+
+ def fmt_stat_for_file_transfer
+ if @finished_p then
+ sprintf("%s %s %s", bytes, transfer_rate, elapsed)
+ else
+ sprintf("%s %s %s", bytes, transfer_rate, eta)
+ end
+ end
+
+ def fmt_title
+ @title[0,(@title_width - 1)] + ":"
+ end
+
+ def convert_bytes (bytes)
+ if bytes < 1024
+ sprintf("%6dB", bytes)
+ elsif bytes < 1024 * 1000 # 1000kb
+ sprintf("%5.1fKB", bytes.to_f / 1024)
+ elsif bytes < 1024 * 1024 * 1000 # 1000mb
+ sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
+ else
+ sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
+ end
+ end
+
+ def transfer_rate
+ bytes_per_second = @current.to_f / (Time.now - @start_time)
+ sprintf("%s/s", convert_bytes(bytes_per_second))
+ end
+
+ def bytes
+ convert_bytes(@current)
+ end
+
+ def format_time (t)
+ t = t.to_i
+ sec = t % 60
+ min = (t / 60) % 60
+ hour = t / 3600
+ sprintf("%02d:%02d:%02d", hour, min, sec);
+ end
+
+ # ETA stands for Estimated Time of Arrival.
+ def eta
+ if @current == 0
+ "ETA: --:--:--"
+ else
+ elapsed = Time.now - @start_time
+ eta = elapsed * @total / @current - elapsed;
+ sprintf("ETA: %s", format_time(eta))
+ end
+ end
+
+ def elapsed
+ elapsed = Time.now - @start_time
+ sprintf("Time: %s", format_time(elapsed))
+ end
+
+ def eol
+ if @finished_p then "\n" else "\r" end
+ end
+
+ def do_percentage
+ if @total.zero?
+ 100
+ else
+ @current * 100 / @total
+ end
+ end
+
+ def get_width
+ # FIXME: I don't know how portable it is.
+ default_width = 80
+ begin
+ tiocgwinsz = 0x5413
+ data = [0, 0, 0, 0].pack("SSSS")
+ if @out.ioctl(tiocgwinsz, data) >= 0 then
+ rows, cols, xpixels, ypixels = data.unpack("SSSS")
+ if cols >= 0 then cols else default_width end
+ else
+ default_width
+ end
+ rescue Exception
+ default_width
+ end
+ end
+
+ def show
+ arguments = @format_arguments.map {|method|
+ method = sprintf("fmt_%s", method)
+ send(method)
+ }
+ line = sprintf(@format, *arguments)
+
+ width = get_width
+ if line.length == width - 1
+ @out.print(line + eol)
+ @out.flush
+ elsif line.length >= width
+ @terminal_width = [@terminal_width - (line.length - width + 1), 0].max
+ if @terminal_width == 0 then @out.print(line + eol) else show end
+ else # line.length < width - 1
+ @terminal_width += width - line.length + 1
+ show
+ end
+ @previous_time = Time.now
+ end
+
+ def show_if_needed
+ if @total.zero?
+ cur_percentage = 100
+ prev_percentage = 0
+ else
+ cur_percentage = (@current * 100 / @total).to_i
+ prev_percentage = (@previous * 100 / @total).to_i
+ end
+
+ # Use "!=" instead of ">" to support negative changes
+ if cur_percentage != prev_percentage ||
+ Time.now - @previous_time >= 1 || @finished_p
+ show
+ end
+ end
+
+ public
+ def clear
+ @out.print "\r"
+ @out.print(" " * (get_width - 1))
+ @out.print "\r"
+ end
+
+ def finish
+ @current = @total
+ @finished_p = true
+ show
+ end
+
+ def finished?
+ @finished_p
+ end
+
+ def file_transfer_mode
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
+ end
+
+ def format= (format)
+ @format = format
+ end
+
+ def format_arguments= (arguments)
+ @format_arguments = arguments
+ end
+
+ def halt
+ @finished_p = true
+ show
+ end
+
+ def inc (step = 1)
+ @current += step
+ @current = @total if @current > @total
+ show_if_needed
+ @previous = @current
+ end
+
+ def set (count)
+ if count < 0 || count > @total
+ raise "invalid count: #{count} (total: #{@total})"
+ end
+ @current = count
+ show_if_needed
+ @previous = @current
+ end
+
+ def inspect
+ "#<ProgressBar:#{@current}/#{@total}>"
+ end
+end
+
+class ReversedProgressBar < ProgressBar
+ def do_percentage
+ 100 - super
+ end
+end
+
105 test.rb
@@ -0,0 +1,105 @@
+require 'test/unit'
+require 'progressbar'
+
+class ProgressBarTest < Test::Unit::TestCase
+ SleepUnit = 0.01
+
+ def do_make_progress_bar (title, total)
+ ProgressBar.new(title, total)
+ end
+
+ def test_bytes
+ total = 1024 * 1024
+ pbar = do_make_progress_bar("test(bytes)", total)
+ pbar.file_transfer_mode
+ 0.step(total, 2**14) {|x|
+ pbar.set(x)
+ sleep(SleepUnit)
+ }
+ pbar.finish
+ end
+
+ def test_clear
+ total = 100
+ pbar = do_make_progress_bar("test(clear)", total)
+ total.times {
+ sleep(SleepUnit)
+ pbar.inc
+ }
+ pbar.clear
+ puts
+ end
+
+ def test_halt
+ total = 100
+ pbar = do_make_progress_bar("test(halt)", total)
+ (total / 2).times {
+ sleep(SleepUnit)
+ pbar.inc
+ }
+ pbar.halt
+ end
+
+ def test_inc
+ total = 100
+ pbar = do_make_progress_bar("test(inc)", total)
+ total.times {
+ sleep(SleepUnit)
+ pbar.inc
+ }
+ pbar.finish
+ end
+
+ def test_inc_x
+ total = File.size("progressbar.rb")
+ pbar = do_make_progress_bar("test(inc(x))", total)
+ File.new("progressbar.rb").each {|line|
+ sleep(SleepUnit)
+ pbar.inc(line.length)
+ }
+ pbar.finish
+ end
+
+ def test_invalid_set
+ total = 100
+ pbar = do_make_progress_bar("test(invalid set)", total)
+ begin
+ pbar.set(200)
+ rescue RuntimeError => e
+ puts e.message
+ end
+ end
+
+ def test_set
+ total = 1000
+ pbar = do_make_progress_bar("test(set)", total)
+ (1..total).find_all {|x| x % 10 == 0}.each {|x|
+ sleep(SleepUnit)
+ pbar.set(x)
+ }
+ pbar.finish
+ end
+
+ def test_slow
+ total = 100000
+ pbar = do_make_progress_bar("test(slow)", total)
+ 0.step(500, 1) {|x|
+ pbar.set(x)
+ sleep(SleepUnit)
+ }
+ pbar.halt
+ end
+
+ def test_total_zero
+ total = 0
+ pbar = do_make_progress_bar("test(total=0)", total)
+ pbar.finish
+ end
+end
+
+class ReversedProgressBarTest < ProgressBarTest
+ def do_make_progress_bar (title, total)
+ ReversedProgressBar.new(title, total)
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.