progressbar 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +113 -0
- data/LICENSE +1 -0
- data/README.en.rd +103 -0
- data/README.ja.rd +100 -0
- data/lib/progressbar.rb +236 -0
- data/test/test.rb +105 -0
- metadata +63 -0
data/ChangeLog
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
2005-05-21 Satoru Takabayashi <satoru@namazu.org>
|
2
|
+
|
3
|
+
* progressbar.rb (ProgressBar::show): Call IO#flush. Suggestted by
|
4
|
+
"Geert Fannes" <Geert.Fannes at ikanconsulting.com>
|
5
|
+
|
6
|
+
2005-01-07 Satoru Takabayashi <satoru@namazu.org>
|
7
|
+
|
8
|
+
* progressbar.rb (ProgressBar::VERSION): Bumped version number to 0.9.
|
9
|
+
(ProgressBar::finished?): New method.
|
10
|
+
(ProgressBar::current): New attribute.
|
11
|
+
(ProgressBar::total): New attribute.
|
12
|
+
(ProgressBar::title): New attribute.
|
13
|
+
(ProgressBar::start_time): New attribute.
|
14
|
+
(ProgressBar::inspect): Change the format.
|
15
|
+
(ProgressBar::show_if_needed): Renamed from show_progress.
|
16
|
+
(ProgressBar::clear): New method.
|
17
|
+
(ProgressBar::initialize): Renamed a field: bar_width ->
|
18
|
+
terminal_width.
|
19
|
+
(ProgressBar::do_percentage): New method.
|
20
|
+
(ProgressBar::percentage): Use it.
|
21
|
+
(ReversedProgressBar): New class.
|
22
|
+
(ProgressBar::initialize): Use #clear.
|
23
|
+
(ProgressBar::fmt_bar): Ditto.
|
24
|
+
(ProgressBar::fmt_percentage): Renamed from percentage.
|
25
|
+
(ProgressBar::fmt_stat): Ditto.
|
26
|
+
(ProgressBar::fmt_stat_for_file_transfer): Ditto.
|
27
|
+
(ProgressBar::fmt_title): Ditto.
|
28
|
+
|
29
|
+
* test.rb: Use Test::Unit.
|
30
|
+
|
31
|
+
* Makefile (docs): Removed.
|
32
|
+
(progressbar.ja.html): Ditto.
|
33
|
+
(progressbar.en.html): Ditto.
|
34
|
+
(dist): New rule.
|
35
|
+
(check): New rule.
|
36
|
+
|
37
|
+
2004-02-05 Satoru Takabayashi <satoru@namazu.org>
|
38
|
+
|
39
|
+
* Ruby/ProgressBar: Version 0.8 released!
|
40
|
+
|
41
|
+
* progressbar.rb (ProgressBar::set): Fixed the bug when caused by
|
42
|
+
the invalid count. <http://bugs.debian.org/231009>
|
43
|
+
(ProgressBar::VERSION): Bumped version number to 0.8.
|
44
|
+
|
45
|
+
2004-01-19 Satoru Takabayashi <satoru@namazu.org>
|
46
|
+
|
47
|
+
* Ruby/ProgressBar: Version 0.7 released!
|
48
|
+
|
49
|
+
* progressbar.rb (ProgressBar::initialize): Rename a field:
|
50
|
+
@bar_length -> @bar_width.
|
51
|
+
(ProgressBar::initialize): New field: @title_width.
|
52
|
+
(ProgressBar::title): use it.
|
53
|
+
(ProgressBar::initialize): New field: @previous_time.
|
54
|
+
(ProgressBar::show_progress): Use it and update the progress bar
|
55
|
+
if one sec. elapsed.
|
56
|
+
(ProgressBar::initialize): Call show instead of show_progress.
|
57
|
+
|
58
|
+
2004-01-16 Satoru Takabayashi <satoru@namazu.org>
|
59
|
+
|
60
|
+
* Ruby/ProgressBar: Version 0.6 released!
|
61
|
+
|
62
|
+
* progressbar.rb (ProgressBar::show): Remove the useless condition
|
63
|
+
after "else". Thanks to Neil Spring <nspring@cs.washington.edu>
|
64
|
+
for the report.
|
65
|
+
<http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=227966>
|
66
|
+
(ProgressBar):
|
67
|
+
|
68
|
+
2003-07-24 Satoru Takabayashi <satoru@namazu.org>
|
69
|
+
|
70
|
+
* Ruby/ProgressBar: Version 0.5 released!
|
71
|
+
|
72
|
+
* progressbar.rb (ProgressBar::initialize): New parameter: config.
|
73
|
+
(ProgressBar::convert_prefix_multiplier): New method.
|
74
|
+
(ProgressBar::transfer_rate): New method.
|
75
|
+
(ProgressBar::percentage): New method.
|
76
|
+
(ProgressBar::title): New method.
|
77
|
+
(ProgressBar::initialize): New fields: @bar_mark, @format,
|
78
|
+
@arguments.
|
79
|
+
(ProgressBar::get_width): New method.
|
80
|
+
(ProgressBar::stat_for_file_transfer): New method.
|
81
|
+
(ProgressBar::stat): Renamed from time.
|
82
|
+
(ProgressBar::format=): New method.
|
83
|
+
(ProgressBar::format_arguments=): New method.
|
84
|
+
(ProgressBar::convert_bytes): New method.
|
85
|
+
(ProgressBar::file_transfer_mode): New method.
|
86
|
+
|
87
|
+
2002-10-21 Satoru Takabayashi <satoru@namazu.org>
|
88
|
+
|
89
|
+
* Ruby/ProgressBar: Version 0.4 released!
|
90
|
+
|
91
|
+
* progressbar.rb (ProgressBar::halt): New method. allowing a
|
92
|
+
"broken" ProgressBar in the event of error.
|
93
|
+
- Suggestted by Austin Ziegler <austin@halostatue.ca>
|
94
|
+
|
95
|
+
2002-01-05 Satoru Takabayashi <satoru@namazu.org>
|
96
|
+
|
97
|
+
* Ruby/ProgressBar: Version 0.3 released!
|
98
|
+
|
99
|
+
* progressbar.rb (ProgressBar::show): Shorten @title to fall into
|
100
|
+
the format well.
|
101
|
+
|
102
|
+
2001-11-03 Satoru Takabayashi <satoru@namazu.org>
|
103
|
+
|
104
|
+
* Ruby/ProgressBar: Version 0.2 released!
|
105
|
+
|
106
|
+
* progressbar.rb (ProgressBar::eta): Remove an unnecessary condition.
|
107
|
+
|
108
|
+
* progressbar.rb (ProgressBar::eta): Fix the return value bug.
|
109
|
+
|
110
|
+
* progressbar.rb (ProgressBar::inc): Add an optional parameter `step'.
|
111
|
+
|
112
|
+
* Ruby/ProgressBar: Version 0.1 released!
|
113
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Ruby/ProgressBar is a free software with ABSOLUTELY NO WARRANTY under the terms of Ruby's license.
|
data/README.en.rd
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
=begin
|
2
|
+
index:Ej
|
3
|
+
|
4
|
+
= Ruby/ProgressBar: A Text Progress Bar Library for Ruby
|
5
|
+
|
6
|
+
Last Modified: 2005-05-22 00:28:04
|
7
|
+
|
8
|
+
--
|
9
|
+
|
10
|
+
Ruby/ProgressBar is a text progress bar library for Ruby.
|
11
|
+
It can indicate progress with percentage, a progress bar,
|
12
|
+
and estimated remaining time.
|
13
|
+
|
14
|
+
The latest version of Ruby/ProgressBar is available at
|
15
|
+
((<URL:http://namazu.org/~satoru/ruby-progressbar/>))
|
16
|
+
.
|
17
|
+
|
18
|
+
== Examples
|
19
|
+
|
20
|
+
% irb --simple-prompt -r progressbar
|
21
|
+
>> pbar = ProgressBar.new("test", 100)
|
22
|
+
=> (ProgressBar: 0/100)
|
23
|
+
>> 100.times {sleep(0.1); pbar.inc}; pbar.finish
|
24
|
+
test: 100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
|
25
|
+
=> nil
|
26
|
+
|
27
|
+
>> pbar = ProgressBar.new("test", 100)
|
28
|
+
=> (ProgressBar: 0/100)
|
29
|
+
>> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
|
30
|
+
test: 67% |oooooooooooooooooooooooooo | ETA: 00:00:03
|
31
|
+
|
32
|
+
== API
|
33
|
+
|
34
|
+
--- ProgressBar#new (title, total, out = STDERR)
|
35
|
+
Display the initial progress bar and return a
|
36
|
+
ProgressBar object. ((|title|)) specifies the title,
|
37
|
+
and ((|total|)) specifies the total cost of processing.
|
38
|
+
Optional parameter ((|out|)) specifies the output IO.
|
39
|
+
|
40
|
+
The display of the progress bar is updated when one or
|
41
|
+
more percent is proceeded or one or more seconds are
|
42
|
+
elapsed from the previous display.
|
43
|
+
|
44
|
+
--- ProgressBar#inc (step = 1)
|
45
|
+
Increase the internal counter by ((|step|)) and update
|
46
|
+
the display of the progress bar. Display the estimated
|
47
|
+
remaining time on the right side of the bar. The counter
|
48
|
+
does not go beyond the ((|total|)).
|
49
|
+
|
50
|
+
--- ProgressBar#set (count)
|
51
|
+
Set the internal counter to ((|count|)) and update the
|
52
|
+
display of the progress bar. Display the estimated
|
53
|
+
remaining time on the right side of the bar. Raise if
|
54
|
+
((|count|)) is a negative number or a number more than
|
55
|
+
the ((|total|)).
|
56
|
+
|
57
|
+
--- ProgressBar#finish
|
58
|
+
Stop the progress bar and update the display of progress
|
59
|
+
bar. Display the elapsed time on the right side of the bar.
|
60
|
+
The progress bar always stops at 100 % by the method.
|
61
|
+
|
62
|
+
--- ProgressBar#halt
|
63
|
+
Stop the progress bar and update the display of progress
|
64
|
+
bar. Display the elapsed time on the right side of the bar.
|
65
|
+
The progress bar stops at the current percentage by the method.
|
66
|
+
|
67
|
+
--- ProgressBar#format=
|
68
|
+
Set the format for displaying a progress bar.
|
69
|
+
Default: "%-14s %3d%% %s %s".
|
70
|
+
|
71
|
+
--- ProgressBar#format_arguments=
|
72
|
+
Set the methods for displaying a progress bar.
|
73
|
+
Default: [:title, :percentage, :bar, :stat].
|
74
|
+
|
75
|
+
--- ProgressBar#file_transfer_mode
|
76
|
+
Use :stat_for_file_transfer instead of :stat to display
|
77
|
+
transfered bytes and transfer rate.
|
78
|
+
|
79
|
+
|
80
|
+
ReverseProgressBar class is also available. The
|
81
|
+
functionality is identical to ProgressBar but the direction
|
82
|
+
of the progress bar is just opposite.
|
83
|
+
|
84
|
+
== Limitations
|
85
|
+
|
86
|
+
Since the progress is calculated by the proportion to the
|
87
|
+
total cost of processing, Ruby/ProgressBar cannot be used if
|
88
|
+
the total cost of processing is unknown in advance.
|
89
|
+
Moreover, the estimation of remaining time cannot be
|
90
|
+
accurately performed if the progress does not flow uniformly.
|
91
|
+
|
92
|
+
== Download
|
93
|
+
|
94
|
+
Ruby/ProgressBar is a free software with ABSOLUTELY NO WARRANTY
|
95
|
+
under the terms of Ruby's license.
|
96
|
+
|
97
|
+
* ((<URL:http://namazu.org/~satoru/ruby-progressbar/ruby-progressbar-0.9.tar.gz>))
|
98
|
+
* ((<URL:http://cvs.namazu.org/ruby-progressbar/>))
|
99
|
+
|
100
|
+
--
|
101
|
+
|
102
|
+
- ((<Satoru Takabayashi|URL:http://namazu.org/~satoru/>)) -
|
103
|
+
=end
|
data/README.ja.rd
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
=begin
|
2
|
+
index:eJ
|
3
|
+
|
4
|
+
= Ruby/ProgressBar: �ץ����쥹�С���ƥ����Ȥ�ɽ������ Ruby�ѤΥ饤�֥��
|
5
|
+
|
6
|
+
�ǽ�������: 2005-05-22 00:28:53
|
7
|
+
|
8
|
+
|
9
|
+
--
|
10
|
+
|
11
|
+
Ruby/ProgressBar �ϥץ����쥹�С���ƥ����Ȥ�ɽ������ Ruby��
|
12
|
+
�Υ饤�֥��Ǥ��������ο�Ľ������ѡ�����ȡ��ץ����쥹�С���
|
13
|
+
����ӿ���Ĥ���֤Ȥ���ɽ�����ޤ���
|
14
|
+
|
15
|
+
�ǿ��Ǥ�
|
16
|
+
((<URL:http://namazu.org/~satoru/ruby-progressbar/>))
|
17
|
+
���������ǽ�Ǥ�
|
18
|
+
|
19
|
+
== ������
|
20
|
+
|
21
|
+
% irb --simple-prompt -r progressbar
|
22
|
+
>> pbar = ProgressBar.new("test", 100)
|
23
|
+
=> (ProgressBar: 0/100)
|
24
|
+
>> 100.times {sleep(0.1); pbar.inc}; pbar.finish
|
25
|
+
test: 100% |oooooooooooooooooooooooooooooooooooooooo| Time: 00:00:10
|
26
|
+
=> nil
|
27
|
+
|
28
|
+
>> pbar = ProgressBar.new("test", 100)
|
29
|
+
=> (ProgressBar: 0/100)
|
30
|
+
>> (1..100).each{|x| sleep(0.1); pbar.set(x)}; pbar.finish
|
31
|
+
test: 67% |oooooooooooooooooooooooooo | ETA: 00:00:03
|
32
|
+
|
33
|
+
== API
|
34
|
+
|
35
|
+
--- ProgressBar#new (title, total, out = STDERR)
|
36
|
+
�ץ����쥹�С��ν�����֤�ɽ������������ ProgressBar����
|
37
|
+
�������Ȥ��֤���((|title|)) �Ǹ��Ф���((|total|)) �ǽ�
|
38
|
+
�������פ�((|out|)) �ǽ������ IO �����ꤹ�롣
|
39
|
+
|
40
|
+
�ץ����쥹�С���ɽ���ϡ������ɽ�������Ľ�� 1%�ʾ夢��
|
41
|
+
���Ȥ������뤤�� 1�ðʾ�вᤷ�����˹�������ޤ���
|
42
|
+
|
43
|
+
--- ProgressBar#inc (step = 1)
|
44
|
+
�����Υ����� ((|step|)) �������ʤ�ơ��ץ����쥹�С�
|
45
|
+
��ɽ�������롣�С��α�¦�ˤϿ���Ĥ���֤�ɽ�����롣
|
46
|
+
������ ((|total|)) ��ۤ��ƿʤळ�ȤϤʤ���
|
47
|
+
|
48
|
+
--- ProgressBar#set (count)
|
49
|
+
�������ͤ� ((|count|)) �����ꤷ���ץ����쥹�С���
|
50
|
+
ɽ�������롣�С��α�¦�ˤϿ���Ĥ���֤�ɽ�����롣
|
51
|
+
((|count|)) �˥ޥ��ʥ����ͤ��뤤�� ((|total|)) ����礭
|
52
|
+
���ͤ��Ϥ����㳰��ȯ�����롣
|
53
|
+
|
54
|
+
--- ProgressBar#finish
|
55
|
+
�ץ����쥹�С�����ߤ����ץ����쥹�С���ɽ�������롣
|
56
|
+
�ץ����쥹�С��α�¦�ˤϷв���֤�ɽ�����롣
|
57
|
+
���ΤȤ����ץ����쥹�С��� 100% �ǽ�λ���롣
|
58
|
+
|
59
|
+
--- ProgressBar#halt
|
60
|
+
�ץ����쥹�С�����ߤ����ץ����쥹�С���ɽ�������롣
|
61
|
+
�ץ����쥹�С��α�¦�ˤϷв���֤�ɽ�����롣
|
62
|
+
���ΤȤ����ץ����쥹�С��Ϥ��λ����Υѡ�����ơ����ǽ�λ���롣
|
63
|
+
|
64
|
+
--- ProgressBar#format=
|
65
|
+
�ץ����쥹�С�ɽ���Υե����ޥåȤ����ꤹ�롣
|
66
|
+
̤�ѹ����� "%-14s %3d%% %s %s"
|
67
|
+
|
68
|
+
--- ProgressBar#format_arguments=
|
69
|
+
�ץ����쥹�С�ɽ���˻Ȥ��ؿ������ꤹ�롣
|
70
|
+
̤�ѹ����� [:title, :percentage, :bar, :stat]
|
71
|
+
�ե�����ž�����ˤ� :stat ���Ѥ��� :stat_for_file_transfer
|
72
|
+
��Ȥ���ž���Х��ȿ���ž��®�٤�ɽ���Ǥ��롣
|
73
|
+
|
74
|
+
--- ProgressBar#file_transfer_mode
|
75
|
+
�ץ����쥹�С�ɽ���� :stat ���Ѥ��� :stat_for_file_transfer
|
76
|
+
��Ȥ���ž���Х��ȿ���ž��®�٤�ɽ�����롣
|
77
|
+
|
78
|
+
|
79
|
+
ReverseProgressBar �Ȥ������饹������ޤ�����ǽ��
|
80
|
+
ProgressBar �Ȥޤä���Ʊ���Ǥ������ץ����쥹�С��οʹ�������
|
81
|
+
�դˤʤäƤ��ޤ���
|
82
|
+
|
83
|
+
== ���»���
|
84
|
+
|
85
|
+
��Ľ��������������פ��Ф�����Ȥ��Ʒ����뤿�ᡢ��������
|
86
|
+
�פ������ˤ狼��ʤ������ǤϻȤ��ޤ��ޤ�����Ľ��ή�줬��
|
87
|
+
��Ǥʤ��Ȥ��ˤϻĤ���֤ο�����������Ԥ��ޤ���
|
88
|
+
|
89
|
+
== �����������
|
90
|
+
|
91
|
+
Ruby �Υ饤���˽��ä��ե���եȥ������Ȥ��Ƹ������ޤ���
|
92
|
+
������̵�ݾڤǤ���
|
93
|
+
|
94
|
+
* ((<URL:http://namazu.org/~satoru/ruby-progressbar/ruby-progressbar-0.9.tar.gz>))
|
95
|
+
* ((<URL:http://cvs.namazu.org/ruby-progressbar/>))
|
96
|
+
|
97
|
+
--
|
98
|
+
|
99
|
+
- ((<Satoru Takabayashi|URL:http://namazu.org/~satoru/>)) -
|
100
|
+
=end
|
data/lib/progressbar.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
#
|
2
|
+
# Ruby/ProgressBar - a text progress bar library
|
3
|
+
#
|
4
|
+
# Copyright (C) 2001-2005 Satoru Takabayashi <satoru@namazu.org>
|
5
|
+
# All rights reserved.
|
6
|
+
# This is free software with ABSOLUTELY NO WARRANTY.
|
7
|
+
#
|
8
|
+
# You can redistribute it and/or modify it under the terms
|
9
|
+
# of Ruby's license.
|
10
|
+
#
|
11
|
+
|
12
|
+
class ProgressBar
|
13
|
+
VERSION = "0.9"
|
14
|
+
|
15
|
+
def initialize (title, total, out = STDERR)
|
16
|
+
@title = title
|
17
|
+
@total = total
|
18
|
+
@out = out
|
19
|
+
@terminal_width = 80
|
20
|
+
@bar_mark = "o"
|
21
|
+
@current = 0
|
22
|
+
@previous = 0
|
23
|
+
@finished_p = false
|
24
|
+
@start_time = Time.now
|
25
|
+
@previous_time = @start_time
|
26
|
+
@title_width = 14
|
27
|
+
@format = "%-#{@title_width}s %3d%% %s %s"
|
28
|
+
@format_arguments = [:title, :percentage, :bar, :stat]
|
29
|
+
clear
|
30
|
+
show
|
31
|
+
end
|
32
|
+
attr_reader :title
|
33
|
+
attr_reader :current
|
34
|
+
attr_reader :total
|
35
|
+
attr_accessor :start_time
|
36
|
+
|
37
|
+
private
|
38
|
+
def fmt_bar
|
39
|
+
bar_width = do_percentage * @terminal_width / 100
|
40
|
+
sprintf("|%s%s|",
|
41
|
+
@bar_mark * bar_width,
|
42
|
+
" " * (@terminal_width - bar_width))
|
43
|
+
end
|
44
|
+
|
45
|
+
def fmt_percentage
|
46
|
+
do_percentage
|
47
|
+
end
|
48
|
+
|
49
|
+
def fmt_stat
|
50
|
+
if @finished_p then elapsed else eta end
|
51
|
+
end
|
52
|
+
|
53
|
+
def fmt_stat_for_file_transfer
|
54
|
+
if @finished_p then
|
55
|
+
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
|
56
|
+
else
|
57
|
+
sprintf("%s %s %s", bytes, transfer_rate, eta)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def fmt_title
|
62
|
+
@title[0,(@title_width - 1)] + ":"
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_bytes (bytes)
|
66
|
+
if bytes < 1024
|
67
|
+
sprintf("%6dB", bytes)
|
68
|
+
elsif bytes < 1024 * 1000 # 1000kb
|
69
|
+
sprintf("%5.1fKB", bytes.to_f / 1024)
|
70
|
+
elsif bytes < 1024 * 1024 * 1000 # 1000mb
|
71
|
+
sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
|
72
|
+
else
|
73
|
+
sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def transfer_rate
|
78
|
+
bytes_per_second = @current.to_f / (Time.now - @start_time)
|
79
|
+
sprintf("%s/s", convert_bytes(bytes_per_second))
|
80
|
+
end
|
81
|
+
|
82
|
+
def bytes
|
83
|
+
convert_bytes(@current)
|
84
|
+
end
|
85
|
+
|
86
|
+
def format_time (t)
|
87
|
+
t = t.to_i
|
88
|
+
sec = t % 60
|
89
|
+
min = (t / 60) % 60
|
90
|
+
hour = t / 3600
|
91
|
+
sprintf("%02d:%02d:%02d", hour, min, sec);
|
92
|
+
end
|
93
|
+
|
94
|
+
# ETA stands for Estimated Time of Arrival.
|
95
|
+
def eta
|
96
|
+
if @current == 0
|
97
|
+
"ETA: --:--:--"
|
98
|
+
else
|
99
|
+
elapsed = Time.now - @start_time
|
100
|
+
eta = elapsed * @total / @current - elapsed;
|
101
|
+
sprintf("ETA: %s", format_time(eta))
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def elapsed
|
106
|
+
elapsed = Time.now - @start_time
|
107
|
+
sprintf("Time: %s", format_time(elapsed))
|
108
|
+
end
|
109
|
+
|
110
|
+
def eol
|
111
|
+
if @finished_p then "\n" else "\r" end
|
112
|
+
end
|
113
|
+
|
114
|
+
def do_percentage
|
115
|
+
if @total.zero?
|
116
|
+
100
|
117
|
+
else
|
118
|
+
@current * 100 / @total
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_width
|
123
|
+
# FIXME: I don't know how portable it is.
|
124
|
+
default_width = 80
|
125
|
+
begin
|
126
|
+
tiocgwinsz = 0x5413
|
127
|
+
data = [0, 0, 0, 0].pack("SSSS")
|
128
|
+
if @out.ioctl(tiocgwinsz, data) >= 0 then
|
129
|
+
rows, cols, xpixels, ypixels = data.unpack("SSSS")
|
130
|
+
if cols >= 0 then cols else default_width end
|
131
|
+
else
|
132
|
+
default_width
|
133
|
+
end
|
134
|
+
rescue Exception
|
135
|
+
default_width
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def show
|
140
|
+
arguments = @format_arguments.map {|method|
|
141
|
+
method = sprintf("fmt_%s", method)
|
142
|
+
send(method)
|
143
|
+
}
|
144
|
+
line = sprintf(@format, *arguments)
|
145
|
+
|
146
|
+
width = get_width
|
147
|
+
if line.length == width - 1
|
148
|
+
@out.print(line + eol)
|
149
|
+
@out.flush
|
150
|
+
elsif line.length >= width
|
151
|
+
@terminal_width = [@terminal_width - (line.length - width + 1), 0].max
|
152
|
+
if @terminal_width == 0 then @out.print(line + eol) else show end
|
153
|
+
else # line.length < width - 1
|
154
|
+
@terminal_width += width - line.length + 1
|
155
|
+
show
|
156
|
+
end
|
157
|
+
@previous_time = Time.now
|
158
|
+
end
|
159
|
+
|
160
|
+
def show_if_needed
|
161
|
+
if @total.zero?
|
162
|
+
cur_percentage = 100
|
163
|
+
prev_percentage = 0
|
164
|
+
else
|
165
|
+
cur_percentage = (@current * 100 / @total).to_i
|
166
|
+
prev_percentage = (@previous * 100 / @total).to_i
|
167
|
+
end
|
168
|
+
|
169
|
+
# Use "!=" instead of ">" to support negative changes
|
170
|
+
if cur_percentage != prev_percentage ||
|
171
|
+
Time.now - @previous_time >= 1 || @finished_p
|
172
|
+
show
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
public
|
177
|
+
def clear
|
178
|
+
@out.print "\r"
|
179
|
+
@out.print(" " * (get_width - 1))
|
180
|
+
@out.print "\r"
|
181
|
+
end
|
182
|
+
|
183
|
+
def finish
|
184
|
+
@current = @total
|
185
|
+
@finished_p = true
|
186
|
+
show
|
187
|
+
end
|
188
|
+
|
189
|
+
def finished?
|
190
|
+
@finished_p
|
191
|
+
end
|
192
|
+
|
193
|
+
def file_transfer_mode
|
194
|
+
@format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
|
195
|
+
end
|
196
|
+
|
197
|
+
def format= (format)
|
198
|
+
@format = format
|
199
|
+
end
|
200
|
+
|
201
|
+
def format_arguments= (arguments)
|
202
|
+
@format_arguments = arguments
|
203
|
+
end
|
204
|
+
|
205
|
+
def halt
|
206
|
+
@finished_p = true
|
207
|
+
show
|
208
|
+
end
|
209
|
+
|
210
|
+
def inc (step = 1)
|
211
|
+
@current += step
|
212
|
+
@current = @total if @current > @total
|
213
|
+
show_if_needed
|
214
|
+
@previous = @current
|
215
|
+
end
|
216
|
+
|
217
|
+
def set (count)
|
218
|
+
if count < 0 || count > @total
|
219
|
+
raise "invalid count: #{count} (total: #{@total})"
|
220
|
+
end
|
221
|
+
@current = count
|
222
|
+
show_if_needed
|
223
|
+
@previous = @current
|
224
|
+
end
|
225
|
+
|
226
|
+
def inspect
|
227
|
+
"#<ProgressBar:#{@current}/#{@total}>"
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
class ReversedProgressBar < ProgressBar
|
232
|
+
def do_percentage
|
233
|
+
100 - super
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
data/test/test.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'progressbar'
|
3
|
+
|
4
|
+
class ProgressBarTest < Test::Unit::TestCase
|
5
|
+
SleepUnit = 0.01
|
6
|
+
|
7
|
+
def do_make_progress_bar (title, total)
|
8
|
+
ProgressBar.new(title, total)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_bytes
|
12
|
+
total = 1024 * 1024
|
13
|
+
pbar = do_make_progress_bar("test(bytes)", total)
|
14
|
+
pbar.file_transfer_mode
|
15
|
+
0.step(total, 2**14) {|x|
|
16
|
+
pbar.set(x)
|
17
|
+
sleep(SleepUnit)
|
18
|
+
}
|
19
|
+
pbar.finish
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_clear
|
23
|
+
total = 100
|
24
|
+
pbar = do_make_progress_bar("test(clear)", total)
|
25
|
+
total.times {
|
26
|
+
sleep(SleepUnit)
|
27
|
+
pbar.inc
|
28
|
+
}
|
29
|
+
pbar.clear
|
30
|
+
puts
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_halt
|
34
|
+
total = 100
|
35
|
+
pbar = do_make_progress_bar("test(halt)", total)
|
36
|
+
(total / 2).times {
|
37
|
+
sleep(SleepUnit)
|
38
|
+
pbar.inc
|
39
|
+
}
|
40
|
+
pbar.halt
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_inc
|
44
|
+
total = 100
|
45
|
+
pbar = do_make_progress_bar("test(inc)", total)
|
46
|
+
total.times {
|
47
|
+
sleep(SleepUnit)
|
48
|
+
pbar.inc
|
49
|
+
}
|
50
|
+
pbar.finish
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_inc_x
|
54
|
+
total = File.size("../lib/progressbar.rb")
|
55
|
+
pbar = do_make_progress_bar("test(inc(x))", total)
|
56
|
+
File.new("../lib/progressbar.rb").each {|line|
|
57
|
+
sleep(SleepUnit)
|
58
|
+
pbar.inc(line.length)
|
59
|
+
}
|
60
|
+
pbar.finish
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_invalid_set
|
64
|
+
total = 100
|
65
|
+
pbar = do_make_progress_bar("test(invalid set)", total)
|
66
|
+
begin
|
67
|
+
pbar.set(200)
|
68
|
+
rescue RuntimeError => e
|
69
|
+
puts e.message
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_set
|
74
|
+
total = 1000
|
75
|
+
pbar = do_make_progress_bar("test(set)", total)
|
76
|
+
(1..total).find_all {|x| x % 10 == 0}.each {|x|
|
77
|
+
sleep(SleepUnit)
|
78
|
+
pbar.set(x)
|
79
|
+
}
|
80
|
+
pbar.finish
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_slow
|
84
|
+
total = 100000
|
85
|
+
pbar = do_make_progress_bar("test(slow)", total)
|
86
|
+
0.step(500, 1) {|x|
|
87
|
+
pbar.set(x)
|
88
|
+
sleep(SleepUnit)
|
89
|
+
}
|
90
|
+
pbar.halt
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_total_zero
|
94
|
+
total = 0
|
95
|
+
pbar = do_make_progress_bar("test(total=0)", total)
|
96
|
+
pbar.finish
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class ReversedProgressBarTest < ProgressBarTest
|
101
|
+
def do_make_progress_bar (title, total)
|
102
|
+
ReversedProgressBar.new(title, total)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: progressbar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Satoru Takabayashi
|
8
|
+
- Jose Peleteiro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-11-24 00:00:00 -02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Ruby/ProgressBar is a text progress bar library for Ruby. It can indicate progress with percentage, a progress bar, and estimated remaining time.
|
18
|
+
email: satoru@0xcc.net
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- ChangeLog
|
25
|
+
- LICENSE
|
26
|
+
- README.en.rd
|
27
|
+
- README.ja.rd
|
28
|
+
files:
|
29
|
+
- lib/progressbar.rb
|
30
|
+
- ChangeLog
|
31
|
+
- LICENSE
|
32
|
+
- README.en.rd
|
33
|
+
- README.ja.rd
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/peleteiro/progressbar
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: Ruby/ProgressBar is a text progress bar library for Ruby.
|
62
|
+
test_files:
|
63
|
+
- test/test.rb
|