progressbar 0.21.0 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE.txt +19 -0
  5. data/README.md +38 -0
  6. data/Rakefile +2 -14
  7. data/lib/progressbar.rb +14 -284
  8. data/lib/ruby-progressbar/base.rb +161 -0
  9. data/lib/ruby-progressbar/calculators/length.rb +88 -0
  10. data/lib/ruby-progressbar/calculators/length_spec.rb +9 -0
  11. data/lib/ruby-progressbar/calculators/running_average.rb +9 -0
  12. data/lib/ruby-progressbar/components.rb +5 -0
  13. data/lib/ruby-progressbar/components/bar.rb +96 -0
  14. data/lib/ruby-progressbar/components/percentage.rb +29 -0
  15. data/lib/ruby-progressbar/components/rate.rb +43 -0
  16. data/lib/ruby-progressbar/components/time.rb +103 -0
  17. data/lib/ruby-progressbar/components/title.rb +13 -0
  18. data/lib/ruby-progressbar/errors/invalid_progress_error.rb +4 -0
  19. data/lib/ruby-progressbar/format.rb +3 -0
  20. data/lib/ruby-progressbar/format/formatter.rb +27 -0
  21. data/lib/ruby-progressbar/format/molecule.rb +58 -0
  22. data/lib/ruby-progressbar/format/string.rb +36 -0
  23. data/lib/ruby-progressbar/output.rb +61 -0
  24. data/lib/ruby-progressbar/outputs/non_tty.rb +47 -0
  25. data/lib/ruby-progressbar/outputs/tty.rb +32 -0
  26. data/lib/ruby-progressbar/progress.rb +114 -0
  27. data/lib/ruby-progressbar/throttle.rb +25 -0
  28. data/lib/ruby-progressbar/time.rb +30 -0
  29. data/lib/ruby-progressbar/timer.rb +72 -0
  30. data/lib/ruby-progressbar/version.rb +3 -0
  31. data/spec/fixtures/benchmark.rb +28 -0
  32. data/spec/ruby-progressbar/base_spec.rb +949 -0
  33. data/spec/ruby-progressbar/calculators/length_calculator_spec.rb +17 -0
  34. data/spec/ruby-progressbar/calculators/running_average_spec.rb +19 -0
  35. data/spec/ruby-progressbar/components/bar_spec.rb +234 -0
  36. data/spec/ruby-progressbar/components/percentage_spec.rb +9 -0
  37. data/spec/ruby-progressbar/components/rate_spec.rb +9 -0
  38. data/spec/ruby-progressbar/components/throttle_spec.rb +157 -0
  39. data/spec/ruby-progressbar/components/time_spec.rb +307 -0
  40. data/spec/ruby-progressbar/components/title_spec.rb +12 -0
  41. data/spec/ruby-progressbar/format/formatter_spec.rb +9 -0
  42. data/spec/ruby-progressbar/format/molecule_spec.rb +30 -0
  43. data/spec/ruby-progressbar/format/string_spec.rb +9 -0
  44. data/spec/ruby-progressbar/output_spec.rb +7 -0
  45. data/spec/ruby-progressbar/outputs/non_tty_spec.rb +9 -0
  46. data/spec/ruby-progressbar/outputs/tty_spec.rb +9 -0
  47. data/spec/ruby-progressbar/progress_spec.rb +156 -0
  48. data/spec/ruby-progressbar/time_spec.rb +45 -0
  49. data/spec/ruby-progressbar/timer_spec.rb +7 -0
  50. data/spec/spec_helper.rb +6 -0
  51. data/spec/support/time.rb +17 -0
  52. metadata +134 -69
  53. metadata.gz.sig +3 -0
  54. data/.gitignore +0 -23
  55. data/.ruby-version +0 -1
  56. data/.travis.yml +0 -6
  57. data/ChangeLog +0 -113
  58. data/Gemfile +0 -4
  59. data/Gemfile.lock +0 -27
  60. data/LICENSE +0 -1
  61. data/README.rdoc +0 -116
  62. data/progressbar.gemspec +0 -29
  63. data/test/test.rb +0 -125
@@ -0,0 +1,17 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/calculators/running_average'
3
+
4
+ class ProgressBar
5
+ module Calculators
6
+ RSpec.describe Length do
7
+ it 'can properly calculate the length even if IO.console is nil' do
8
+ calculator = Length.new
9
+
10
+ expect(IO).to receive(:console).and_return nil
11
+ expect(calculator).to receive(:dynamic_width_via_system_calls).and_return 123_456
12
+
13
+ expect(calculator.calculate_length).to eql 123_456
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/calculators/running_average'
3
+
4
+ class ProgressBar
5
+ module Calculators
6
+ RSpec.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
+ RSpec.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
@@ -0,0 +1,9 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/components/percentage'
3
+
4
+ class ProgressBar
5
+ module Components
6
+ RSpec.describe Percentage do
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/components/rate'
3
+
4
+ class ProgressBar
5
+ module Components
6
+ RSpec.describe Rate do
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,157 @@
1
+ require 'rspectacular'
2
+ require 'ruby-progressbar/progress'
3
+
4
+ class ProgressBar
5
+ RSpec.describe Throttle do
6
+ let(:timer) { ProgressBar::Timer.new(:time => ::Time) }
7
+
8
+ it 'yields the first time if the throttle rate is given and the timer is not started' do
9
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
10
+ :throttle_timer => timer)
11
+
12
+ yielded = false
13
+
14
+ throttle.choke do
15
+ yielded = true
16
+ end
17
+
18
+ expect(yielded).to be_a TrueClass
19
+ end
20
+
21
+ it 'does not yield after the initial yield if the period has not passed yet' do
22
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
23
+ :throttle_timer => timer)
24
+ timer.start
25
+
26
+ throttle.choke {}
27
+
28
+ yielded = false
29
+
30
+ 9.times do
31
+ Timecop.freeze(1)
32
+
33
+ throttle.choke do
34
+ yielded = true
35
+ end
36
+ end
37
+
38
+ Timecop.return
39
+
40
+ expect(yielded).to be_a FalseClass
41
+ end
42
+
43
+ it 'always yields if forced to, even after the initial yield or if the period ' \
44
+ 'has not passed' do
45
+
46
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
47
+ :throttle_timer => timer)
48
+ timer.start
49
+
50
+ throttle.choke {}
51
+
52
+ yielded = 0
53
+
54
+ 25.times do
55
+ Timecop.freeze(1)
56
+
57
+ throttle.choke(:force_update_if => true) do
58
+ yielded += 1
59
+ end
60
+ end
61
+
62
+ Timecop.return
63
+
64
+ expect(yielded).to eql 25
65
+ end
66
+
67
+ it 'yields if the period has passed, even after the initial yield' do
68
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
69
+ :throttle_timer => timer)
70
+ timer.start
71
+
72
+ throttle.choke {}
73
+
74
+ yielded = false
75
+
76
+ Timecop.freeze(11)
77
+
78
+ throttle.choke do
79
+ yielded = true
80
+ end
81
+
82
+ Timecop.return
83
+
84
+ expect(yielded).to eql true
85
+ end
86
+
87
+ it 'does not yield after a previous yield if the period has not passed yet' do
88
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
89
+ :throttle_timer => timer)
90
+
91
+ Timecop.freeze(0)
92
+
93
+ timer.start
94
+
95
+ Timecop.freeze(15)
96
+
97
+ throttle.choke {}
98
+
99
+ yielded = false
100
+
101
+ 9.times do
102
+ Timecop.freeze(1)
103
+
104
+ throttle.choke do
105
+ yielded = true
106
+ end
107
+
108
+ expect(yielded).to eql false
109
+ end
110
+
111
+ Timecop.return
112
+ end
113
+
114
+ it 'yields after the period has passed, even after a previous yield' do
115
+ throttle = ProgressBar::Throttle.new(:throttle_rate => 10,
116
+ :throttle_timer => timer)
117
+
118
+ Timecop.freeze(0)
119
+
120
+ timer.start
121
+
122
+ Timecop.freeze(15)
123
+
124
+ throttle.choke {}
125
+
126
+ yielded = false
127
+
128
+ Timecop.freeze(10)
129
+
130
+ throttle.choke do
131
+ yielded = true
132
+ end
133
+
134
+ Timecop.return
135
+
136
+ expect(yielded).to eql true
137
+ end
138
+
139
+ it 'does not throttle if no throttle rate is given' do
140
+ throttle = Throttle.new(:throttle_timer => timer,
141
+ :throttle_rate => nil)
142
+ yield_count = 0
143
+
144
+ 25.times do
145
+ Timecop.freeze(1)
146
+
147
+ throttle.choke do
148
+ yield_count += 1
149
+ end
150
+ end
151
+
152
+ Timecop.return
153
+
154
+ expect(yield_count).to eql 25
155
+ end
156
+ end
157
+ end