ruby-progressbar 1.6.1 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/lib/ruby-progressbar.rb +8 -3
- data/lib/ruby-progressbar/base.rb +121 -177
- data/lib/ruby-progressbar/calculators/length.rb +75 -0
- data/lib/ruby-progressbar/calculators/length_spec.rb +9 -0
- data/lib/ruby-progressbar/calculators/running_average.rb +9 -0
- data/lib/ruby-progressbar/components.rb +3 -5
- data/lib/ruby-progressbar/components/bar.rb +79 -43
- data/lib/ruby-progressbar/components/percentage.rb +29 -0
- data/lib/ruby-progressbar/components/rate.rb +34 -62
- data/lib/ruby-progressbar/components/time.rb +103 -0
- data/lib/ruby-progressbar/components/title.rb +13 -0
- data/lib/ruby-progressbar/format.rb +2 -1
- data/lib/ruby-progressbar/format/formatter.rb +27 -0
- data/lib/ruby-progressbar/format/molecule.rb +55 -37
- data/lib/ruby-progressbar/format/string.rb +36 -0
- data/lib/ruby-progressbar/output.rb +61 -0
- data/lib/ruby-progressbar/outputs/non_tty.rb +47 -0
- data/lib/ruby-progressbar/outputs/tty.rb +32 -0
- data/lib/ruby-progressbar/progress.rb +110 -0
- data/lib/ruby-progressbar/throttle.rb +25 -0
- data/lib/ruby-progressbar/time.rb +20 -17
- data/lib/ruby-progressbar/timer.rb +72 -0
- data/lib/ruby-progressbar/version.rb +2 -2
- data/spec/fixtures/benchmark.rb +21 -4
- data/spec/{lib/ruby-progressbar → ruby-progressbar}/base_spec.rb +55 -62
- data/spec/ruby-progressbar/calculators/running_average_spec.rb +19 -0
- data/spec/ruby-progressbar/components/bar_spec.rb +234 -0
- data/spec/ruby-progressbar/components/percentage_spec.rb +9 -0
- data/spec/ruby-progressbar/components/rate_spec.rb +9 -0
- data/spec/ruby-progressbar/components/throttle_spec.rb +157 -0
- data/spec/ruby-progressbar/components/time_spec.rb +308 -0
- data/spec/ruby-progressbar/components/title_spec.rb +12 -0
- data/spec/ruby-progressbar/format/formatter_spec.rb +9 -0
- data/spec/ruby-progressbar/format/molecule_spec.rb +30 -0
- data/spec/ruby-progressbar/format/string_spec.rb +9 -0
- data/spec/ruby-progressbar/output_spec.rb +7 -0
- data/spec/ruby-progressbar/outputs/non_tty_spec.rb +9 -0
- data/spec/ruby-progressbar/outputs/tty_spec.rb +9 -0
- data/spec/ruby-progressbar/progress_spec.rb +150 -0
- data/spec/ruby-progressbar/time_spec.rb +37 -0
- data/spec/ruby-progressbar/timer_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/support/time.rb +3 -1
- metadata +55 -35
- data/lib/ruby-progressbar/components/elapsed_timer.rb +0 -25
- data/lib/ruby-progressbar/components/estimated_timer.rb +0 -90
- data/lib/ruby-progressbar/components/progressable.rb +0 -112
- data/lib/ruby-progressbar/components/throttle.rb +0 -21
- data/lib/ruby-progressbar/components/timer.rb +0 -69
- data/lib/ruby-progressbar/format/base.rb +0 -55
- data/lib/ruby-progressbar/formatter.rb +0 -112
- data/lib/ruby-progressbar/length_calculator.rb +0 -64
- data/lib/ruby-progressbar/running_average_calculator.rb +0 -7
- data/spec/lib/ruby-progressbar/components/bar_spec.rb +0 -210
- data/spec/lib/ruby-progressbar/components/elapsed_timer_spec.rb +0 -91
- data/spec/lib/ruby-progressbar/components/estimated_timer_spec.rb +0 -241
- data/spec/lib/ruby-progressbar/components/progressable_spec.rb +0 -47
- data/spec/lib/ruby-progressbar/components/throttle_spec.rb +0 -100
- data/spec/lib/ruby-progressbar/format/molecule_spec.rb +0 -22
- data/spec/lib/ruby-progressbar/running_average_calculator_spec.rb +0 -11
- data/spec/lib/ruby-progressbar/time_spec.rb +0 -49
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'ruby-progressbar/time'
|
2
|
+
|
3
|
+
class ProgressBar
|
4
|
+
class Timer
|
5
|
+
attr_accessor :started_at,
|
6
|
+
:stopped_at
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
self.time = options[:time] || Time
|
10
|
+
end
|
11
|
+
|
12
|
+
def start
|
13
|
+
self.started_at = stopped? ? time.now - (stopped_at - started_at) : time.now
|
14
|
+
self.stopped_at = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def stop
|
18
|
+
return unless started?
|
19
|
+
|
20
|
+
self.stopped_at = time.now
|
21
|
+
end
|
22
|
+
|
23
|
+
def pause
|
24
|
+
stop
|
25
|
+
end
|
26
|
+
|
27
|
+
def resume
|
28
|
+
start
|
29
|
+
end
|
30
|
+
|
31
|
+
def started?
|
32
|
+
started_at
|
33
|
+
end
|
34
|
+
|
35
|
+
def stopped?
|
36
|
+
stopped_at
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset
|
40
|
+
self.started_at = nil
|
41
|
+
self.stopped_at = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def reset?
|
45
|
+
!started_at
|
46
|
+
end
|
47
|
+
|
48
|
+
def restart
|
49
|
+
reset
|
50
|
+
start
|
51
|
+
end
|
52
|
+
|
53
|
+
def elapsed_seconds
|
54
|
+
((stopped_at || time.now) - started_at)
|
55
|
+
end
|
56
|
+
|
57
|
+
def elapsed_whole_seconds
|
58
|
+
elapsed_seconds.floor
|
59
|
+
end
|
60
|
+
|
61
|
+
def divide_seconds(seconds)
|
62
|
+
hours, seconds = seconds.divmod(3600)
|
63
|
+
minutes, seconds = seconds.divmod(60)
|
64
|
+
|
65
|
+
[hours, minutes, seconds]
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
attr_accessor :time
|
71
|
+
end
|
72
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
class
|
2
|
-
VERSION = '1.
|
1
|
+
class ProgressBar
|
2
|
+
VERSION = '1.7.0'
|
3
3
|
end
|
data/spec/fixtures/benchmark.rb
CHANGED
@@ -1,11 +1,28 @@
|
|
1
|
-
# bundle exec ruby-prof --printer=graph_html
|
1
|
+
# bundle exec ruby-prof --printer=graph_html
|
2
|
+
# --file=../results.html
|
3
|
+
# --require 'ruby-progressbar'
|
4
|
+
# --sort=total ./spec/fixtures/benchmark.rb
|
2
5
|
|
3
|
-
total =
|
6
|
+
total = 100_000
|
4
7
|
# output = File.open('/Users/jfelchner/Downloads/benchmark.txt', 'w+')
|
5
8
|
output = $stdout
|
6
|
-
|
9
|
+
|
10
|
+
# Progressbar gem
|
11
|
+
# bar = ProgressBar.new('Progress', total)
|
12
|
+
#
|
13
|
+
# total.times do |i|
|
14
|
+
# bar.inc
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# bar.finish
|
18
|
+
|
19
|
+
# Ruby/ProgressBar
|
20
|
+
bar = ProgressBar.create(:output => output,
|
21
|
+
:length => 80,
|
22
|
+
:start => 0,
|
23
|
+
:total => total)
|
7
24
|
|
8
25
|
total.times do |i|
|
9
|
-
bar.log i
|
26
|
+
# bar.log i
|
10
27
|
bar.increment
|
11
28
|
end
|
@@ -21,13 +21,13 @@ describe ProgressBar::Base do
|
|
21
21
|
it 'can properly handle outputting the bar when the length changes on the fly to less than the minimum width' do
|
22
22
|
progressbar = ProgressBar::Base.new(:output => output, :title => 'a' * 25, :format => '%t%B', :throttle_rate => 0.0)
|
23
23
|
|
24
|
-
allow(progressbar).to receive(:terminal_width).
|
25
|
-
|
24
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
25
|
+
and_return 30
|
26
26
|
|
27
27
|
progressbar.start
|
28
28
|
|
29
|
-
allow(progressbar).to receive(:terminal_width).
|
30
|
-
|
29
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
30
|
+
and_return 20
|
31
31
|
|
32
32
|
progressbar.increment
|
33
33
|
|
@@ -39,8 +39,8 @@ describe ProgressBar::Base do
|
|
39
39
|
it 'returns the proper string' do
|
40
40
|
progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 21), :starting_at => 5, :total => 10, :autostart => false)
|
41
41
|
|
42
|
-
allow(progressbar).to receive(:terminal_width).
|
43
|
-
|
42
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
43
|
+
and_return 20
|
44
44
|
|
45
45
|
expect(progressbar.to_s('%t%w')).to eql '*********************'
|
46
46
|
end
|
@@ -50,8 +50,8 @@ describe ProgressBar::Base do
|
|
50
50
|
it 'returns the proper string' do
|
51
51
|
progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 21), :autostart => false)
|
52
52
|
|
53
|
-
allow(progressbar).to receive(:terminal_width).
|
54
|
-
|
53
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
54
|
+
and_return 20
|
55
55
|
|
56
56
|
expect(progressbar.to_s('%t%i')).to eql '*********************'
|
57
57
|
end
|
@@ -59,8 +59,8 @@ describe ProgressBar::Base do
|
|
59
59
|
it 'returns the proper string' do
|
60
60
|
progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 21), :starting_at => 5, :total => 10, :autostart => false)
|
61
61
|
|
62
|
-
allow(progressbar).to receive(:terminal_width).
|
63
|
-
|
62
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
63
|
+
and_return 20
|
64
64
|
|
65
65
|
expect(progressbar.to_s('%t%i')).to eql '*********************'
|
66
66
|
end
|
@@ -70,8 +70,8 @@ describe ProgressBar::Base do
|
|
70
70
|
it 'returns the proper string' do
|
71
71
|
progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 19), :starting_at => 5, :total => 10, :autostart => false)
|
72
72
|
|
73
|
-
allow(progressbar).to receive(:terminal_width).
|
74
|
-
|
73
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
74
|
+
and_return 20
|
75
75
|
|
76
76
|
expect(progressbar.to_s('%t%B')).to eql '******************* '
|
77
77
|
end
|
@@ -79,8 +79,8 @@ describe ProgressBar::Base do
|
|
79
79
|
it 'returns the proper string' do
|
80
80
|
progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 19), :starting_at => 5, :total => 10, :autostart => false)
|
81
81
|
|
82
|
-
allow(progressbar).to receive(:terminal_width).
|
83
|
-
|
82
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).
|
83
|
+
and_return 20
|
84
84
|
|
85
85
|
expect(progressbar.to_s('%t%w%i')).to eql '******************* '
|
86
86
|
end
|
@@ -93,13 +93,13 @@ describe ProgressBar::Base do
|
|
93
93
|
|
94
94
|
describe '#title' do
|
95
95
|
it 'returns the default title' do
|
96
|
-
expect(progressbar.send(:title).to_s).to eql ProgressBar::
|
96
|
+
expect(progressbar.send(:title).to_s).to eql ProgressBar::Components::Title::DEFAULT_TITLE
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
100
100
|
describe '#output' do
|
101
101
|
it 'returns the default output stream' do
|
102
|
-
expect(progressbar.send(:output)).to eql ProgressBar::
|
102
|
+
expect(progressbar.send(:output).send(:stream)).to eql ProgressBar::Output::DEFAULT_OUTPUT_STREAM
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -110,7 +110,7 @@ describe ProgressBar::Base do
|
|
110
110
|
|
111
111
|
it 'returns the length of the environment variable as an integer' do
|
112
112
|
progressbar = ProgressBar::Base.new
|
113
|
-
expect(progressbar.send(:length)).to eql 44
|
113
|
+
expect(progressbar.send(:output).send(:length_calculator).send(:length)).to eql 44
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
@@ -120,21 +120,21 @@ describe ProgressBar::Base do
|
|
120
120
|
context 'but the length option was passed in' do
|
121
121
|
it 'returns the length specified in the option' do
|
122
122
|
progressbar = ProgressBar::Base.new(:length => 88)
|
123
|
-
expect(progressbar.send(:length)).to eql 88
|
123
|
+
expect(progressbar.send(:output).send(:length_calculator).send(:length)).to eql 88
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
127
127
|
context 'and no length option was passed in' do
|
128
128
|
it 'returns the width of the terminal if it is a Unix environment' do
|
129
|
-
allow(progressbar).to receive(:terminal_width).and_return(99)
|
130
|
-
progressbar.send(:reset_length)
|
131
|
-
expect(progressbar.send(:length)).to eql 99
|
129
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:terminal_width).and_return(99)
|
130
|
+
progressbar.send(:output).send(:length_calculator).send(:reset_length)
|
131
|
+
expect(progressbar.send(:output).send(:length_calculator).send(:length)).to eql 99
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'returns 80 if it is not a Unix environment' do
|
135
|
-
allow(progressbar).to receive(:unix?).and_return(false)
|
136
|
-
progressbar.send(:reset_length)
|
137
|
-
expect(progressbar.send(:length)).to eql 80
|
135
|
+
allow(progressbar.send(:output).send(:length_calculator)).to receive(:unix?).and_return(false)
|
136
|
+
progressbar.send(:output).send(:length_calculator).send(:reset_length)
|
137
|
+
expect(progressbar.send(:output).send(:length_calculator).send(:length)).to eql 80
|
138
138
|
end
|
139
139
|
end
|
140
140
|
end
|
@@ -152,13 +152,13 @@ describe ProgressBar::Base do
|
|
152
152
|
|
153
153
|
describe '#output' do
|
154
154
|
it 'returns the overridden output stream' do
|
155
|
-
expect(progressbar.send(:output)).to eql STDOUT
|
155
|
+
expect(progressbar.send(:output).send(:stream)).to eql STDOUT
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
159
159
|
describe '#length' do
|
160
160
|
it 'returns the overridden length' do
|
161
|
-
expect(progressbar.send(:length)).to eql 88
|
161
|
+
expect(progressbar.send(:output).send(:length_calculator).send(:length)).to eql 88
|
162
162
|
end
|
163
163
|
end
|
164
164
|
end
|
@@ -202,16 +202,16 @@ describe ProgressBar::Base do
|
|
202
202
|
|
203
203
|
context 'which includes ANSI SGR codes in the format string' do
|
204
204
|
it 'properly calculates the length of the bar by removing the long version of the ANSI codes from the calculated length' do
|
205
|
-
@color_code
|
206
|
-
@reset_code
|
207
|
-
@progress_mark
|
205
|
+
@color_code = "\e[0m\e[32m\e[7m\e[1m"
|
206
|
+
@reset_code = "\e[0m"
|
207
|
+
@progress_mark = "#{@color_code} #{@reset_code}"
|
208
208
|
progressbar = ProgressBar::Base.new(:format => "#{@color_code}Processing... %b%i#{@reset_code}#{@color_code} %p%%#{@reset_code}",
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
209
|
+
:progress_mark => @progress_mark,
|
210
|
+
:output => output,
|
211
|
+
:length => 24,
|
212
|
+
:starting_at => 3,
|
213
|
+
:total => 6,
|
214
|
+
:throttle_rate => 0.0)
|
215
215
|
|
216
216
|
progressbar.increment
|
217
217
|
progressbar.increment
|
@@ -221,16 +221,16 @@ describe ProgressBar::Base do
|
|
221
221
|
end
|
222
222
|
|
223
223
|
it 'properly calculates the length of the bar by removing the short version of the ANSI codes from the calculated length' do
|
224
|
-
@color_code
|
225
|
-
@reset_code
|
226
|
-
@progress_mark
|
224
|
+
@color_code = "\e[0;32;7;1m"
|
225
|
+
@reset_code = "\e[0m"
|
226
|
+
@progress_mark = "#{@color_code} #{@reset_code}"
|
227
227
|
progressbar = ProgressBar::Base.new(:format => "#{@color_code}Processing... %b%i#{@reset_code}#{@color_code} %p%%#{@reset_code}",
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
228
|
+
:progress_mark => @progress_mark,
|
229
|
+
:output => output,
|
230
|
+
:length => 24,
|
231
|
+
:starting_at => 3,
|
232
|
+
:total => 6,
|
233
|
+
:throttle_rate => 0.0)
|
234
234
|
|
235
235
|
progressbar.increment
|
236
236
|
progressbar.increment
|
@@ -300,7 +300,7 @@ describe ProgressBar::Base do
|
|
300
300
|
|
301
301
|
3.times { progressbar.increment }
|
302
302
|
|
303
|
-
progressbar.title =
|
303
|
+
progressbar.title = 'Testing'
|
304
304
|
progressbar.stop
|
305
305
|
|
306
306
|
non_tty_output.rewind
|
@@ -323,7 +323,7 @@ describe ProgressBar::Base do
|
|
323
323
|
end
|
324
324
|
|
325
325
|
context 'when a bar is about to be completed' do
|
326
|
-
let(:progressbar) { ProgressBar::Base.new(:starting_at => 5, :total => 6, :output => output, :length => 20) }
|
326
|
+
let(:progressbar) { ProgressBar::Base.new(:starting_at => 5, :total => 6, :output => output, :length => 20, :throttle_rate => 0.0) }
|
327
327
|
|
328
328
|
context 'and it is incremented' do
|
329
329
|
before { progressbar.increment }
|
@@ -347,7 +347,7 @@ describe ProgressBar::Base do
|
|
347
347
|
end
|
348
348
|
|
349
349
|
context 'when a bar with autofinish=false is about to be completed' do
|
350
|
-
let(:progressbar) { ProgressBar::Base.new(:autofinish => false, :starting_at => 5, :total => 6, :output => output, :length => 20) }
|
350
|
+
let(:progressbar) { ProgressBar::Base.new(:autofinish => false, :starting_at => 5, :total => 6, :output => output, :length => 20, :throttle_rate => 0.0) }
|
351
351
|
|
352
352
|
context 'and it is incremented' do
|
353
353
|
before { progressbar.increment }
|
@@ -385,7 +385,7 @@ describe ProgressBar::Base do
|
|
385
385
|
|
386
386
|
output.rewind
|
387
387
|
|
388
|
-
expect(output.read).to end_with " \rProgress: |====== |\rProgress: |========|\n"
|
388
|
+
expect(output.read).to end_with " \rProgress: |====== |\rProgress: |========|\rProgress: |========|\n"
|
389
389
|
end
|
390
390
|
end
|
391
391
|
end
|
@@ -509,7 +509,7 @@ describe ProgressBar::Base do
|
|
509
509
|
progressbar.clear
|
510
510
|
|
511
511
|
output.rewind
|
512
|
-
expect(output.read).to match(/^#{progressbar.send(:clear_string)}/)
|
512
|
+
expect(output.read).to match(/^#{progressbar.send(:output).send(:clear_string)}/)
|
513
513
|
end
|
514
514
|
end
|
515
515
|
|
@@ -518,7 +518,7 @@ describe ProgressBar::Base do
|
|
518
518
|
progressbar.start
|
519
519
|
|
520
520
|
output.rewind
|
521
|
-
expect(output.read).to match(/^#{progressbar.send(:clear_string)}/)
|
521
|
+
expect(output.read).to match(/^#{progressbar.send(:output).send(:clear_string)}/)
|
522
522
|
end
|
523
523
|
|
524
524
|
it 'prints the bar for the first time' do
|
@@ -554,7 +554,7 @@ describe ProgressBar::Base do
|
|
554
554
|
let(:progressbar) { ProgressBar::Base.new(:format => '%B %p%%') }
|
555
555
|
|
556
556
|
context 'if called with no arguments' do
|
557
|
-
before { progressbar.format }
|
557
|
+
before { progressbar.format = nil }
|
558
558
|
|
559
559
|
it 'resets the format back to the default' do
|
560
560
|
expect(progressbar.to_s).to match(/^Progress: \|\s+\|\z/)
|
@@ -562,7 +562,7 @@ describe ProgressBar::Base do
|
|
562
562
|
end
|
563
563
|
|
564
564
|
context 'if called with a specific format string' do
|
565
|
-
before { progressbar.format '%t' }
|
565
|
+
before { progressbar.format = '%t' }
|
566
566
|
|
567
567
|
it 'sets it as the new format for the bar' do
|
568
568
|
expect(progressbar.to_s).to match(/^Progress\z/)
|
@@ -841,16 +841,9 @@ describe ProgressBar::Base do
|
|
841
841
|
expect(progressbar.to_s('%%')).to match(/^%\z/)
|
842
842
|
end
|
843
843
|
|
844
|
-
# Autostarting for now. This will be applicable later.
|
845
|
-
# context "when called before #start" do
|
846
|
-
# it "displays unknown time elapsed when using the %a flag" do
|
847
|
-
# expect(progressbar.to_s('%a')).to match(/^Time: --:--:--\z/)
|
848
|
-
# end
|
849
|
-
# end
|
850
|
-
|
851
844
|
context 'when called after #start' do
|
852
845
|
before do
|
853
|
-
Timecop.travel(-
|
846
|
+
Timecop.travel(-3_723) do
|
854
847
|
progressbar.start
|
855
848
|
end
|
856
849
|
end
|
@@ -877,7 +870,7 @@ describe ProgressBar::Base do
|
|
877
870
|
|
878
871
|
context 'when called after #start' do
|
879
872
|
let(:progressbar) do
|
880
|
-
Timecop.travel(-
|
873
|
+
Timecop.travel(-3_723) do
|
881
874
|
progressbar = ProgressBar::Base.new(:starting_at => 0, :output => output, :smoothing => 0.0)
|
882
875
|
progressbar.start
|
883
876
|
progressbar.progress = 50
|
@@ -900,7 +893,7 @@ describe ProgressBar::Base do
|
|
900
893
|
|
901
894
|
context 'when it could take 100 hours or longer to finish' do
|
902
895
|
let(:progressbar) do
|
903
|
-
Timecop.travel(-
|
896
|
+
Timecop.travel(-120_000) do
|
904
897
|
progressbar = ProgressBar::Base.new(:starting_at => 0, :total => 100, :output => output, :smoothing => 0.0)
|
905
898
|
progressbar.start
|
906
899
|
progressbar.progress = 25
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rspectacular'
|
2
|
+
require 'ruby-progressbar/calculators/running_average'
|
3
|
+
|
4
|
+
class ProgressBar
|
5
|
+
module Calculators
|
6
|
+
describe RunningAverage do
|
7
|
+
it 'can properly calculate a running average' do
|
8
|
+
first_average = RunningAverage.calculate(4.5, 12, 0.1)
|
9
|
+
expect(first_average).to be_within(0.001).of 11.25
|
10
|
+
|
11
|
+
second_average = RunningAverage.calculate(8.2, 51, 0.7)
|
12
|
+
expect(second_average).to be_within(0.001).of 21.04
|
13
|
+
|
14
|
+
third_average = RunningAverage.calculate(41.8, 100, 0.59)
|
15
|
+
expect(third_average).to be_within(0.001).of 65.662
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
require 'rspectacular'
|
2
|
+
require 'ruby-progressbar/components/bar'
|
3
|
+
|
4
|
+
class ProgressBar
|
5
|
+
module Components
|
6
|
+
describe Bar do
|
7
|
+
it 'has a default mark when a new bar is created and no parameters are passed' do
|
8
|
+
expect(Bar.new.progress_mark).to eql Bar::DEFAULT_PROGRESS_MARK
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'has a default remainder mark when a new bar is created and no parameters ' \
|
12
|
+
'are passed' do
|
13
|
+
|
14
|
+
expect(Bar.new.remainder_mark).to eql Bar::DEFAULT_REMAINDER_MARK
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns the overridden mark when a new bar is created and options are passed' do
|
18
|
+
progressbar = Bar.new(:progress_mark => 'x')
|
19
|
+
|
20
|
+
expect(progressbar.progress_mark).to eql 'x'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns the overridden remainder mark when a new bar is created and options ' \
|
24
|
+
'are passed' do
|
25
|
+
|
26
|
+
progressbar = Bar.new(:remainder_mark => 'o')
|
27
|
+
|
28
|
+
expect(progressbar.remainder_mark).to eql 'o'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'displays the bar with no indication of progress when just begun' do
|
32
|
+
progress = Progress.new(:total => 50)
|
33
|
+
progressbar = Bar.new(:progress => progress,
|
34
|
+
:length => 100)
|
35
|
+
|
36
|
+
expect(progressbar.to_s).to eql ' ' * 100
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'displays the bar with an indication of progress when nothing has been ' \
|
40
|
+
'completed and the bar is incremented' do
|
41
|
+
|
42
|
+
progress = Progress.new :total => 50
|
43
|
+
progressbar = Bar.new :progress => progress,
|
44
|
+
:length => 100
|
45
|
+
progress.increment
|
46
|
+
|
47
|
+
expect(progressbar.to_s).to eql '==' + (' ' * 98)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'displays the bar with no indication of progress when nothing has been ' \
|
51
|
+
'completed and the bar is incremented' do
|
52
|
+
|
53
|
+
progress = Progress.new :total => 50
|
54
|
+
progressbar = Bar.new :progress => progress,
|
55
|
+
:length => 100
|
56
|
+
|
57
|
+
expect(progressbar.to_s).to eql ' ' * 100
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'displays the bar with no indication of progress when a fraction of a percentage ' \
|
61
|
+
'has been completed' do
|
62
|
+
|
63
|
+
progress = Progress.new :total => 200
|
64
|
+
progressbar = Bar.new :progress => progress,
|
65
|
+
:length => 100
|
66
|
+
progress.start :at => 1
|
67
|
+
|
68
|
+
expect(progressbar.to_s).to eql(' ' * 100)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'displays the bar as 100% complete when completed' do
|
72
|
+
progress = Progress.new :total => 50
|
73
|
+
progressbar = Bar.new :progress => progress,
|
74
|
+
:length => 100
|
75
|
+
progress.start :at => 50
|
76
|
+
progress.increment
|
77
|
+
|
78
|
+
expect(progressbar.to_s).to eql '=' * 100
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'displays the bar as 100% complete when completed and the bar is incremented' do
|
82
|
+
progress = Progress.new :total => 50
|
83
|
+
progressbar = Bar.new :progress => progress,
|
84
|
+
:length => 100
|
85
|
+
progress.start :at => 50
|
86
|
+
progress.increment
|
87
|
+
|
88
|
+
expect(progressbar.to_s).to eql '=' * 100
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'displays the bar as 98% complete when completed and the bar is decremented' do
|
92
|
+
progress = Progress.new :total => 50
|
93
|
+
progressbar = Bar.new :progress => progress,
|
94
|
+
:length => 100
|
95
|
+
progress.start :at => 50
|
96
|
+
progress.decrement
|
97
|
+
|
98
|
+
expect(progressbar.to_s).to eql(('=' * 98) + (' ' * 2))
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'is represented correctly when a bar has an unknown amount to completion' do
|
102
|
+
progress = Progress.new :total => nil
|
103
|
+
progressbar = Bar.new :progress => progress,
|
104
|
+
:length => 80
|
105
|
+
|
106
|
+
expect(progressbar.to_s).to eql('=---' * 20)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'is represented after being incremented once when a bar has an unknown amount ' \
|
110
|
+
'to completion' do
|
111
|
+
|
112
|
+
progress = Progress.new :total => nil
|
113
|
+
progressbar = Bar.new :progress => progress,
|
114
|
+
:length => 80
|
115
|
+
|
116
|
+
progress.increment
|
117
|
+
|
118
|
+
expect(progressbar.to_s).to eql('-=--' * 20)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'is represented after being incremented twice when a bar has an unknown amount ' \
|
122
|
+
'to completion' do
|
123
|
+
|
124
|
+
progress = Progress.new :total => nil
|
125
|
+
progressbar = Bar.new :progress => progress,
|
126
|
+
:length => 80
|
127
|
+
|
128
|
+
2.times { progress.increment }
|
129
|
+
|
130
|
+
expect(progressbar.to_s).to eql('--=-' * 20)
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'is represented correctly when a bar has a customized unknown animation' do
|
134
|
+
progress = Progress.new :total => nil
|
135
|
+
progressbar = Bar.new :progress => progress,
|
136
|
+
:unknown_progress_animation_steps => ['*--', '-*-', '--*'],
|
137
|
+
:length => 80
|
138
|
+
|
139
|
+
expect(progressbar.to_s).to eql(('*--' * 26) + '*-')
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'displays the bar with an integrated percentage properly when empty' do
|
143
|
+
progress = Progress.new :total => 100
|
144
|
+
progressbar = Bar.new :progress => progress,
|
145
|
+
:length => 100
|
146
|
+
|
147
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
148
|
+
expect(bar_text).to eql ' ' * 100
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'displays the bar with an integrated percentage properly just before' \
|
152
|
+
'the percentage is displayed' do
|
153
|
+
|
154
|
+
progress = Progress.new :total => 100
|
155
|
+
progressbar = Bar.new :progress => progress,
|
156
|
+
:length => 100
|
157
|
+
|
158
|
+
4.times { progress.increment }
|
159
|
+
|
160
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
161
|
+
expect(bar_text).to eql '====' + (' ' * 96)
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'displays the bar with an integrated percentage properly immediately after' \
|
165
|
+
'the percentage is displayed' do
|
166
|
+
|
167
|
+
progress = Progress.new :total => 100
|
168
|
+
progressbar = Bar.new :progress => progress,
|
169
|
+
:length => 100
|
170
|
+
|
171
|
+
5.times { progress.increment }
|
172
|
+
|
173
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
174
|
+
expect(bar_text).to eql '= 5 =' + (' ' * 95)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'displays the bar with an integrated percentage properly on double digit' \
|
178
|
+
'percentage' do
|
179
|
+
|
180
|
+
progress = Progress.new :total => 100
|
181
|
+
progressbar = Bar.new :progress => progress,
|
182
|
+
:length => 100
|
183
|
+
|
184
|
+
10.times { progress.increment }
|
185
|
+
|
186
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
187
|
+
expect(bar_text).to eql '=== 10 ===' + (' ' * 90)
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'displays the bar with an integrated percentage properly when finished' do
|
191
|
+
|
192
|
+
progress = Progress.new :total => 100
|
193
|
+
progressbar = Bar.new :progress => progress,
|
194
|
+
:length => 100
|
195
|
+
progress.finish
|
196
|
+
|
197
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
198
|
+
expect(bar_text).to eql(('=' * 47) + ' 100 ' + ('=' * 48))
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'calculates the remaining negative space properly with an integrated percentage ' \
|
202
|
+
'bar of 0 percent' do
|
203
|
+
|
204
|
+
progress = Progress.new :total => 200
|
205
|
+
progressbar = Bar.new :progress => progress,
|
206
|
+
:length => 100
|
207
|
+
|
208
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
209
|
+
expect(bar_text).to eql ' ' * 100
|
210
|
+
|
211
|
+
9.times { progress.increment }
|
212
|
+
|
213
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
214
|
+
expect(bar_text).to eql '====' + (' ' * 96)
|
215
|
+
|
216
|
+
progress.increment
|
217
|
+
|
218
|
+
bar_text = progressbar.to_s(:format => :integrated_percentage)
|
219
|
+
expect(bar_text).to eql '= 5 =' + (' ' * 95)
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'raises an error when attempting to set the current value of the bar to be ' \
|
223
|
+
'greater than the total' do
|
224
|
+
|
225
|
+
progress = Progress.new :total => 10
|
226
|
+
_progressbar = Bar.new :progress => progress
|
227
|
+
|
228
|
+
expect { progress.start :at => 11 }.to \
|
229
|
+
raise_error(InvalidProgressError,
|
230
|
+
"You can't set the item's current value to be greater than the total.")
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|