ruby-progressbar 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG.md +10 -0
  3. data/README.md +77 -0
  4. data/lib/ruby-progressbar.rb +6 -13
  5. data/lib/{progress_bar → ruby-progressbar}/base.rb +43 -40
  6. data/lib/ruby-progressbar/components.rb +6 -0
  7. data/lib/ruby-progressbar/components/bar.rb +57 -0
  8. data/lib/{progress_bar → ruby-progressbar}/components/elapsed_timer.rb +0 -0
  9. data/lib/{progress_bar → ruby-progressbar}/components/estimated_timer.rb +0 -0
  10. data/lib/{progress_bar → ruby-progressbar}/components/progressable.rb +3 -2
  11. data/lib/{progress_bar → ruby-progressbar}/components/throttle.rb +0 -0
  12. data/lib/{progress_bar → ruby-progressbar}/components/timer.rb +1 -1
  13. data/lib/ruby-progressbar/format.rb +2 -0
  14. data/lib/{progress_bar → ruby-progressbar}/format/base.rb +1 -0
  15. data/lib/{progress_bar → ruby-progressbar}/format/molecule.rb +0 -0
  16. data/lib/{progress_bar → ruby-progressbar}/formatter.rb +0 -0
  17. data/lib/{progress_bar → ruby-progressbar}/length_calculator.rb +0 -0
  18. data/lib/{progress_bar → ruby-progressbar}/running_average_calculator.rb +0 -0
  19. data/lib/{progress_bar → ruby-progressbar}/time.rb +0 -0
  20. data/lib/{progress_bar → ruby-progressbar}/version.rb +1 -1
  21. data/ruby-progressbar.gemspec +1 -1
  22. data/spec/fixtures/benchmark.rb +8 -2
  23. data/spec/{progress_bar → lib/ruby-progressbar}/base_spec.rb +109 -9
  24. data/spec/{progress_bar → lib/ruby-progressbar}/components/bar_spec.rb +0 -0
  25. data/spec/{progress_bar → lib/ruby-progressbar}/components/elapsed_timer_spec.rb +0 -0
  26. data/spec/{progress_bar → lib/ruby-progressbar}/components/estimated_timer_spec.rb +1 -1
  27. data/spec/{progress_bar → lib/ruby-progressbar}/components/progressable_spec.rb +0 -0
  28. data/spec/{progress_bar → lib/ruby-progressbar}/components/throttle_spec.rb +0 -0
  29. data/spec/{progress_bar → lib/ruby-progressbar}/format/molecule_spec.rb +0 -0
  30. data/spec/{progress_bar → lib/ruby-progressbar}/running_average_calculator_spec.rb +0 -0
  31. data/spec/{progress_bar → lib/ruby-progressbar}/time_spec.rb +0 -0
  32. data/spec/spec_helper.rb +3 -1
  33. metadata +37 -38
  34. data/lib/progress_bar/components.rb +0 -6
  35. data/lib/progress_bar/components/bar.rb +0 -42
  36. data/lib/progress_bar/depreciable.rb +0 -121
  37. data/lib/progress_bar/format.rb +0 -2
  38. data/lib/progressbar.rb +0 -10
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ tags
2
3
  *.rbc
3
4
  .bundle
4
5
  .config
@@ -10,3 +11,4 @@ lib/bundler/man
10
11
  pkg
11
12
  spec/reports
12
13
  tmp
14
+ tags
@@ -0,0 +1,10 @@
1
+ Changelog
2
+ ================================================================================
3
+
4
+ v1.2.0
5
+ --------------------------------------------------------------------------------
6
+ * Finally removed deprecation warning and made folder structure consistent
7
+ * Add #log which will properly handle the display of the bar when sending
8
+ a string of output to the screen.
9
+ * Add unknown progress. See the README for more details.
10
+ * Add proper output for non-TTY IO streams
data/README.md CHANGED
@@ -102,6 +102,8 @@ The following are the list of options you can use:
102
102
  * `:length` - _(Defaults to full width if possible, otherwise `80`)_ The preferred width of the entire progress bar including any format options.
103
103
  * `:output` - _(Defaults to `STDOUT`)_ All output will be sent to this object. Can be any object which responds to `.print`.
104
104
  * `:smoothing` - _(Defaults to `0.1`)_ See [**Smoothing Out Estimated Time Jitters**](#smoothing-out-estimated-time-jitters) below.
105
+ * `:throttle_rate` - _(Defaults to `0.01`)_ See [**Throttling**](#throttling) below.
106
+ * `:unknown_progress_animation_steps` - _(Defaults to `['=---', '-=--', '--=-', '---=']`)_ See [**Unknown Progress**](#unknown-progress) The graphical elements used to cycle when progress is changed but the total amount of items being processed is unknown.
105
107
 
106
108
  ### Changing Progress
107
109
 
@@ -110,6 +112,7 @@ The following are the list of options you can use:
110
112
  * `#progress +=`: Will allow you to increment by a relative amount.
111
113
  * `#progress -=`: Will allow you to decrement by a relative amount.
112
114
  * `#progress=`: Will allow you to jump the amount of progress directly to whatever value you would like. _Note: This will almost always mess up your estimated time if you're using it._
115
+ * `#total=`: Will change the total number of items being processed by the bar. This can be anything (even nil) but cannot be less than the amount of progress already accumulated by the bar.
113
116
 
114
117
  ### Stopping
115
118
 
@@ -130,6 +133,64 @@ _Note: The bar will be finished automatically if the current value ever becomes
130
133
 
131
134
  * If you need to have the bar be redisplayed to give your users more of a "real-time" feel, you can call `#refresh` which will not affect the current position but will update the elapsed and estimated timers.
132
135
 
136
+ ### Unknown Progress
137
+
138
+ Sometimes when processing work, you don't know at the beginning of the job exactly how many items you will be processing. Maybe this might be because you're downloading a chunked file or processing a set of jobs that hasn't fully loaded yet.
139
+
140
+ In times like these, you can set total to `nil` and continue to increment the bar as usual. The bar will display an 'unknown' animation which will change every time you increment. This will give the appearance (by default) that the bar is processing work even though there is no "progress".
141
+
142
+ ```ruby
143
+ progressbar = ProgressBar.create(:starting_at => 20, :total => nil)
144
+ ```
145
+
146
+ Will output:
147
+
148
+ Progress: |=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---|
149
+
150
+ Calling
151
+
152
+ ```ruby
153
+ progressbar.increment
154
+ ```
155
+
156
+ once more will output:
157
+
158
+ Progress: |-=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=---=--|
159
+
160
+ ad infinitum.
161
+
162
+ At whatever point you discover the total that you will be processing, you can call:
163
+
164
+ ```ruby
165
+ progressbar.total = 100
166
+ ```
167
+
168
+ And the bar will magically transform into its typical state:
169
+
170
+ Progress: |======== |
171
+
172
+ ### Logging
173
+
174
+ Many times while using the progress bar, you may wish to log some output for the user. If you attempt to do this using a standard `puts` statement, you'll find that the text will overwrite that which makes up the bar. For example if you were to `puts "hello"` after progress has already begun, you may get something like this:
175
+
176
+ helloess: |======= |
177
+ Progress: |======== |
178
+
179
+ The reason is that ruby-progressbar has to keep redrawing itself every time you change the progress. It's a limitation of terminal output. Using `puts` messes that up because `puts` adds a newline which moves the cursor to the next line, then when ruby-progressbar updates, it does so on the following line.
180
+
181
+ To circumvent this, use `#log` instead.
182
+
183
+ ```ruby
184
+ progressbar = ProgressBar.create
185
+ progressbar.progress = 20
186
+ progressbar.log 'hello'
187
+ ```
188
+
189
+ hello
190
+ Progress: |============= |
191
+
192
+ `#log` will automatically clear the bar, print your desired text and then redraw the bar on the following line. Notice that we did not get a bar **above** the logged output. If you consistently use `#log`, you should only every see one bar on the screen at any time.
193
+
133
194
  Formatting
134
195
  --------------------------------
135
196
 
@@ -244,6 +305,22 @@ The above progress bar will output at most 10 times a second.
244
305
 
245
306
  The default throttling rate if none is specified is 100 times per second (or 0.01)
246
307
 
308
+ ### Custom Unknown Progress Animations
309
+
310
+ Following up on [unknown progress](#unknown-progress), you may wish to update the unknown progress animation to suit your specific needs. This can be easily done by passing in the `:unknown_progress_animation_steps` option.
311
+
312
+ This item should be an array of strings representing each step of the animation. The specific step used for a given progress is determined by the current progress of the bar. For example:
313
+
314
+ ```ruby
315
+ progressbar = ProgressBar.create(:unknown_progress_animation_steps => ['==>', '>==', '=>='])
316
+ ```
317
+
318
+ Would use element 0 ('==>') for a progress of 1, 4, 7, 10, etc. It would use element 3 for a progress of 3, 6, 9, 12, etc.
319
+
320
+ You can have an array of as many elements as you'd like and they will be used in the same manner. For example if you have an array of 50 animation steps, element 0 would only be used for every 50th progress (eg: 1, 51, 101, etc).
321
+
322
+ Whatever element is chosen is repeated along the entire 'incomplete' portion of the bar.
323
+
247
324
  Road Map
248
325
  --------------------------------
249
326
  We're planning on adding a bunch of really nice features to this gem over the next few weeks. We want to keep the simple usage simple but allow for powerful features if they're needed. Our `1.0` release is the first step in that direction.
@@ -1,18 +1,11 @@
1
- require 'progress_bar/depreciable'
2
- require 'progress_bar/length_calculator'
3
- require 'progress_bar/running_average_calculator'
4
- require 'progress_bar/formatter'
5
- require 'progress_bar/components'
6
- require 'progress_bar/format'
7
- require 'progress_bar/base'
1
+ require 'ruby-progressbar/length_calculator'
2
+ require 'ruby-progressbar/running_average_calculator'
3
+ require 'ruby-progressbar/formatter'
4
+ require 'ruby-progressbar/components'
5
+ require 'ruby-progressbar/format'
6
+ require 'ruby-progressbar/base'
8
7
 
9
8
  class ProgressBar
10
- def self.new(*args)
11
- puts "DEPRECATION WARNING: Calling `ProgressBar.new` is deprecated and will be removed on or after #{ProgressBar::Depreciable::DEPRECATION_DATE}. Please use `ProgressBar.create` instead."
12
-
13
- create *args
14
- end
15
-
16
9
  def self.create(*args)
17
10
  ProgressBar::Base.new *args
18
11
  end
@@ -2,21 +2,18 @@ class ProgressBar
2
2
  class Base
3
3
  include ProgressBar::LengthCalculator
4
4
  include ProgressBar::Formatter
5
- include ProgressBar::Depreciable
6
5
 
7
- DEFAULT_OUTPUT_STREAM = STDOUT
6
+ DEFAULT_OUTPUT_STREAM = $stdout
8
7
 
9
- def initialize(*args)
10
- options = args.empty? ? {} : backwards_compatible_args_to_options_conversion(args)
11
-
12
- self.output = options[:output] || DEFAULT_OUTPUT_STREAM
8
+ def initialize(options = {})
9
+ self.output = options[:output] || DEFAULT_OUTPUT_STREAM
13
10
 
14
11
  super(options)
15
12
 
16
- @bar = Components::Bar.new(options)
17
- @estimated_time = Components::EstimatedTimer.new(options)
18
- @elapsed_time = Components::ElapsedTimer.new
19
- @throttle = Components::Throttle.new(options)
13
+ @bar = Components::Bar.new(options)
14
+ @estimated_time = Components::EstimatedTimer.new(options)
15
+ @elapsed_time = Components::ElapsedTimer.new
16
+ @throttle = Components::Throttle.new(options)
20
17
 
21
18
  start :at => options[:starting_at]
22
19
  end
@@ -48,6 +45,10 @@ class ProgressBar
48
45
  with_update { with_progressables(:progress=, new_progress) }
49
46
  end
50
47
 
48
+ def total=(new_total)
49
+ with_update { with_progressables(:total=, new_total) }
50
+ end
51
+
51
52
  ###
52
53
  # Stopping The Bar
53
54
  #
@@ -75,7 +76,7 @@ class ProgressBar
75
76
  end
76
77
 
77
78
  def stopped?
78
- @estimated_time.stopped? && @elapsed_time.stopped?
79
+ (@estimated_time.stopped? && @elapsed_time.stopped?) || finished?
79
80
  end
80
81
 
81
82
  alias :paused? :stopped?
@@ -99,13 +100,23 @@ class ProgressBar
99
100
  # Output
100
101
  #
101
102
  def clear
102
- output.print clear_string
103
+ if output.tty?
104
+ output.print clear_string
105
+ output.print "\r"
106
+ end
103
107
  end
104
108
 
105
109
  def refresh
106
110
  update
107
111
  end
108
112
 
113
+ def log(string)
114
+ clear
115
+ output.puts string
116
+
117
+ update(:force => true) unless stopped?
118
+ end
119
+
109
120
  def to_s(format_string = nil)
110
121
  format_string ||= @format_string
111
122
 
@@ -113,34 +124,24 @@ class ProgressBar
113
124
  end
114
125
 
115
126
  def inspect
116
- "#<ProgressBar:#{progress}/#{total}>"
127
+ "#<ProgressBar:#{progress}/#{total || 'unknown'}>"
117
128
  end
118
129
 
119
130
  private
120
131
  attr_accessor :output
121
132
 
122
133
  def clear_string
123
- "#{" " * length}\r"
134
+ "#{" " * length}"
124
135
  end
125
136
 
126
- def with_progressables(action, *args)
127
- if args.empty?
128
- @bar.send(action)
129
- @estimated_time.send(action)
130
- else
131
- @bar.send(action, *args)
132
- @estimated_time.send(action, *args)
133
- end
137
+ def with_progressables(*args)
138
+ @bar.send(*args)
139
+ @estimated_time.send(*args)
134
140
  end
135
141
 
136
- def with_timers(action, *args)
137
- if args.empty?
138
- @estimated_time.send(action)
139
- @elapsed_time.send(action)
140
- else
141
- @estimated_time.send(action, *args)
142
- @elapsed_time.send(action, *args)
143
- end
142
+ def with_timers(*args)
143
+ @estimated_time.send(*args)
144
+ @elapsed_time.send(*args)
144
145
  end
145
146
 
146
147
  def with_update
@@ -148,22 +149,24 @@ class ProgressBar
148
149
  update
149
150
  end
150
151
 
151
- def update
152
- with_timers(:stop) if finished?
152
+ def update(options = {})
153
+ if output.tty? || stopped?
154
+ with_timers(:stop) if finished?
153
155
 
154
- @throttle.choke( finished? ) do
155
- if length_changed?
156
- clear
157
- reset_length
158
- end
156
+ @throttle.choke( stopped? || options[:force] ) do
157
+ if length_changed?
158
+ clear
159
+ reset_length
160
+ end
159
161
 
160
- output.print self.to_s + eol
161
- output.flush
162
+ output.print self.to_s + eol
163
+ output.flush
164
+ end
162
165
  end
163
166
  end
164
167
 
165
168
  def eol
166
- finished? || stopped? ? "\n" : "\r"
169
+ stopped? ? "\n" : "\r"
167
170
  end
168
171
  end
169
172
  end
@@ -0,0 +1,6 @@
1
+ require 'ruby-progressbar/components/timer'
2
+ require 'ruby-progressbar/components/progressable'
3
+ require 'ruby-progressbar/components/bar'
4
+ require 'ruby-progressbar/components/estimated_timer'
5
+ require 'ruby-progressbar/components/elapsed_timer'
6
+ require 'ruby-progressbar/components/throttle'
@@ -0,0 +1,57 @@
1
+ class ProgressBar
2
+ module Components
3
+ class Bar
4
+ include Progressable
5
+
6
+ DEFAULT_PROGRESS_MARK = '='
7
+ DEFAULT_UNKNOWN_PROGRESS_ANIMATION_STEPS = ['=---', '-=--', '--=-', '---=']
8
+
9
+ attr_accessor :progress_mark
10
+ attr_accessor :length
11
+ attr_accessor :unknown_progress_animation_steps
12
+
13
+ def initialize(options = {})
14
+ super
15
+
16
+ self.unknown_progress_animation_steps = options[:unknown_progress_animation_steps] || DEFAULT_UNKNOWN_PROGRESS_ANIMATION_STEPS
17
+ self.progress_mark = options[:progress_mark] || DEFAULT_PROGRESS_MARK
18
+ end
19
+
20
+ def to_s(options = {:format => :standard})
21
+ completed_string = send(:"#{options[:format]}_complete_string")
22
+
23
+ "#{completed_string}#{empty_string}"
24
+ end
25
+
26
+ def integrated_percentage_complete_string
27
+ return standard_complete_string if completed_length < 5
28
+
29
+ " #{percentage_completed} ".to_s.center(completed_length, progress_mark)
30
+ end
31
+
32
+ def standard_complete_string
33
+ progress_mark * completed_length
34
+ end
35
+
36
+ def empty_string
37
+ incomplete_length = (length - completed_length)
38
+
39
+ if total.nil?
40
+ current_animation_step = progress % unknown_progress_animation_steps.size
41
+ animation_graphic = unknown_progress_animation_steps[current_animation_step]
42
+
43
+ unknown_incomplete_string = animation_graphic * ((incomplete_length / unknown_progress_animation_steps.size) + 2)
44
+
45
+ unknown_incomplete_string[0, incomplete_length]
46
+ else
47
+ ' ' * incomplete_length
48
+ end
49
+ end
50
+
51
+ private
52
+ def completed_length
53
+ (length * percentage_completed / 100).floor
54
+ end
55
+ end
56
+ end
57
+ end
@@ -12,7 +12,7 @@ class ProgressBar
12
12
  attr_accessor :smoothing
13
13
 
14
14
  def initialize(options = {})
15
- self.total = options[:total] || DEFAULT_TOTAL
15
+ self.total = options.fetch(:total, DEFAULT_TOTAL)
16
16
  self.smoothing = options[:smoothing] || DEFAULT_SMOOTHING
17
17
 
18
18
  start :at => DEFAULT_BEGINNING_POSITION
@@ -60,6 +60,7 @@ class ProgressBar
60
60
 
61
61
  def percentage_completed
62
62
  return 100 if total == 0
63
+ return 0 if total.nil?
63
64
 
64
65
  # progress / total * 100
65
66
  #
@@ -75,7 +76,7 @@ class ProgressBar
75
76
 
76
77
  private
77
78
  def validate_total(new_total)
78
- (progress.nil? || new_total >= progress) || raise("You can't set the item's total value to be less than the current progress.")
79
+ (progress.nil? || new_total.nil? || new_total >= progress) || raise("You can't set the item's total value to be less than the current progress.")
79
80
  end
80
81
 
81
82
  def validate_progress(new_progress)
@@ -1,4 +1,4 @@
1
- require 'progress_bar/time'
1
+ require 'ruby-progressbar/time'
2
2
 
3
3
  class ProgressBar
4
4
  module Components
@@ -0,0 +1,2 @@
1
+ require 'ruby-progressbar/format/molecule'
2
+ require 'ruby-progressbar/format/base'
@@ -21,6 +21,7 @@ class ProgressBar
21
21
  processed_string.gsub! '%%', '%'
22
22
 
23
23
  leftover_bar_length = environment.send(:length) - processed_string.length + placeholder_length
24
+ leftover_bar_length = leftover_bar_length < 0 ? 0 : leftover_bar_length
24
25
 
25
26
  bar_molecules.each do |molecule|
26
27
  processed_string.gsub!("%#{molecule.key}", environment.send(molecule.method_name, leftover_bar_length).to_s)
@@ -1,3 +1,3 @@
1
1
  class ProgressBar
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "progress_bar/version"
3
+ require "ruby-progressbar/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.rubygems_version = '1.3.5'
@@ -1,5 +1,11 @@
1
1
  # bundle exec ruby-prof --printer=graph_html --file=../results.html --require 'ruby-progressbar' --sort=total ./spec/fixtures/benchmark.rb
2
2
 
3
- bar = ProgressBar.create(:length => 80, :start => 0, :total => 100000)
3
+ total = 10
4
+ # output = File.open('/Users/jfelchner/Downloads/benchmark.txt', 'w+')
5
+ output = $stdout
6
+ bar = ProgressBar.create(:output => output, :length => 80, :start => 0, :total => total)
4
7
 
5
- 100000.times { bar.increment }
8
+ total.times do |i|
9
+ bar.log i
10
+ bar.increment
11
+ end
@@ -4,18 +4,68 @@ require 'stringio'
4
4
  describe ProgressBar::Base do
5
5
  before do
6
6
  @output = StringIO.new('', 'w+')
7
+ @output.stub(:tty?).and_return true
8
+
9
+ @non_tty_output = StringIO.new('', 'w+')
10
+ @non_tty_output.stub(:tty?).and_return false
11
+
7
12
  @progressbar = ProgressBar::Base.new(:output => @output, :length => 80, :throttle_rate => 0.0)
13
+ @output.rewind
8
14
  end
9
15
 
10
- describe 'terminal width dropping below title length' do
11
- it 'should not crash' do
12
- @progressbar = ProgressBar::Base.new(:output => @output)
13
- @progressbar.stub(:terminal_width).and_return(100)
14
- @progressbar.title = 'a'*95
15
- @progressbar.stub(:terminal_width).and_return(60)
16
- expect {
17
- @progressbar.title = 'a'*95
18
- }.to_not raise_error
16
+ context 'when the terminal width is shorter than the string being output' do
17
+ it 'can properly handle outputting the bar when the length changes on the fly to less than the minimum width' do
18
+ IO.stub_chain(:console, :winsize).and_return [1, 30]
19
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => 'a' * 25, :format => '%t%B', :throttle_rate => 0.0)
20
+
21
+ @progressbar.start
22
+
23
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
24
+ @progressbar.increment
25
+
26
+ @output.rewind
27
+ @output.read.should match /\raaaaaaaaaaaaaaaaaaaaaaaaa \r\s+\raaaaaaaaaaaaaaaaaaaaaaaaa\r\z/
28
+ end
29
+
30
+ context 'and the bar length is calculated' do
31
+ it 'returns the proper string' do
32
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
33
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21), :starting_at => 5, :total => 10)
34
+
35
+ @progressbar.to_s('%t%w').should eql '*********************'
36
+ end
37
+ end
38
+
39
+ context 'and the incomplete bar length is calculated' do
40
+ it 'returns the proper string' do
41
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
42
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21))
43
+
44
+ @progressbar.to_s('%t%i').should eql '*********************'
45
+ end
46
+
47
+ it 'returns the proper string' do
48
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
49
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21), :starting_at => 5, :total => 10)
50
+
51
+ @progressbar.to_s('%t%i').should eql '*********************'
52
+ end
53
+ end
54
+
55
+ context 'and the full bar length is calculated (but lacks the space to output the entire bar)' do
56
+ it 'returns the proper string' do
57
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
58
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 19), :starting_at => 5, :total => 10)
59
+
60
+ @progressbar.to_s('%t%B').should eql '******************* '
61
+ end
62
+
63
+ it 'returns the proper string' do
64
+ IO.stub_chain(:console, :winsize).and_return [1, 20]
65
+ @progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 19), :starting_at => 5, :total => 10)
66
+
67
+ @progressbar.to_s('%t%w%i').should eql '******************* '
68
+ end
19
69
  end
20
70
  end
21
71
 
@@ -131,6 +181,31 @@ describe ProgressBar::Base do
131
181
  end
132
182
  end
133
183
  end
184
+
185
+ context 'for a TTY enabled device' do
186
+ it 'can log messages' do
187
+ @progressbar = ProgressBar::Base.new(:output => @output, :length => 20, :starting_at => 3, :total => 6, :throttle_rate => 0.0)
188
+ @progressbar.increment
189
+ @progressbar.log 'We All Float'
190
+ @progressbar.increment
191
+
192
+ @output.rewind
193
+ @output.read.should include "Progress: |==== |\rProgress: |===== |\r \rWe All Float\nProgress: |===== |\rProgress: |====== |\r"
194
+ end
195
+ end
196
+
197
+ context 'for a non-TTY enabled device' do
198
+ it 'can log messages' do
199
+ @progressbar = ProgressBar::Base.new(:output => @non_tty_output, :length => 20, :starting_at => 4, :total => 6, :throttle_rate => 0.0)
200
+ @progressbar.increment
201
+ @progressbar.log 'We All Float'
202
+ @progressbar.increment
203
+ @progressbar.finish
204
+
205
+ @non_tty_output.rewind
206
+ @non_tty_output.read.should include "We All Float\nProgress: |========|\n"
207
+ end
208
+ end
134
209
  end
135
210
 
136
211
  context 'when a bar is about to be completed' do
@@ -152,6 +227,31 @@ describe ProgressBar::Base do
152
227
  end
153
228
  end
154
229
 
230
+ context 'when a bar has an unknown amount to completion' do
231
+ before do
232
+ @progressbar = ProgressBar::Base.new(:total => nil, :output => @output, :length => 80, :unknown_progress_animation_steps => ['=--', '-=-', '--='])
233
+ end
234
+
235
+ it 'is represented correctly' do
236
+ @progressbar.to_s('%i').should eql '=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-'
237
+ end
238
+
239
+ it 'is represented after being incremented once' do
240
+ @progressbar.increment
241
+ @progressbar.to_s('%i').should eql '-=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--='
242
+ end
243
+
244
+ it 'is represented after being incremented twice' do
245
+ @progressbar.increment
246
+ @progressbar.increment
247
+ @progressbar.to_s('%i').should eql '--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--'
248
+ end
249
+
250
+ it 'displays the proper ETA' do
251
+ @progressbar.to_s('%i%e').should eql '=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-- ETA: ??:??:??'
252
+ end
253
+ end
254
+
155
255
  context 'when a bar is started' do
156
256
  before do
157
257
  @progressbar = ProgressBar::Base.new(:starting_at => 0, :total => 100, :output => @output, :length => 80, :throttle_rate => 0.0)
@@ -183,7 +183,7 @@ describe ProgressBar::Components::EstimatedTimer do
183
183
  # Failure/Error: COUNT.times { bar.increment }
184
184
  # NoMethodError:
185
185
  # undefined method `+' for nil:NilClass
186
- # # ./lib/progress_bar/components/progressable.rb:33:in `increment'
186
+ # # ./lib/ruby-progressbar/components/progressable.rb:33:in `increment'
187
187
  #
188
188
  it 'properly delegates' do
189
189
  @output = StringIO.new('', 'w+')
@@ -1,5 +1,7 @@
1
1
  require 'simplecov'
2
- SimpleCov.start
2
+ SimpleCov.start do
3
+ add_filter '/spec/'
4
+ end
3
5
 
4
6
  require 'mathn'
5
7
  require 'rspec'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-progressbar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-08 00:00:00.000000000 Z
13
+ date: 2013-08-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -95,41 +95,40 @@ files:
95
95
  - .rspec
96
96
  - .ruby-version
97
97
  - .travis.yml
98
+ - CHANGELOG.md
98
99
  - Gemfile
99
100
  - Gemfile.lock
100
101
  - LICENSE
101
102
  - README.md
102
103
  - Rakefile
103
- - lib/progress_bar/base.rb
104
- - lib/progress_bar/components.rb
105
- - lib/progress_bar/components/bar.rb
106
- - lib/progress_bar/components/elapsed_timer.rb
107
- - lib/progress_bar/components/estimated_timer.rb
108
- - lib/progress_bar/components/progressable.rb
109
- - lib/progress_bar/components/throttle.rb
110
- - lib/progress_bar/components/timer.rb
111
- - lib/progress_bar/depreciable.rb
112
- - lib/progress_bar/format.rb
113
- - lib/progress_bar/format/base.rb
114
- - lib/progress_bar/format/molecule.rb
115
- - lib/progress_bar/formatter.rb
116
- - lib/progress_bar/length_calculator.rb
117
- - lib/progress_bar/running_average_calculator.rb
118
- - lib/progress_bar/time.rb
119
- - lib/progress_bar/version.rb
120
- - lib/progressbar.rb
121
104
  - lib/ruby-progressbar.rb
105
+ - lib/ruby-progressbar/base.rb
106
+ - lib/ruby-progressbar/components.rb
107
+ - lib/ruby-progressbar/components/bar.rb
108
+ - lib/ruby-progressbar/components/elapsed_timer.rb
109
+ - lib/ruby-progressbar/components/estimated_timer.rb
110
+ - lib/ruby-progressbar/components/progressable.rb
111
+ - lib/ruby-progressbar/components/throttle.rb
112
+ - lib/ruby-progressbar/components/timer.rb
113
+ - lib/ruby-progressbar/format.rb
114
+ - lib/ruby-progressbar/format/base.rb
115
+ - lib/ruby-progressbar/format/molecule.rb
116
+ - lib/ruby-progressbar/formatter.rb
117
+ - lib/ruby-progressbar/length_calculator.rb
118
+ - lib/ruby-progressbar/running_average_calculator.rb
119
+ - lib/ruby-progressbar/time.rb
120
+ - lib/ruby-progressbar/version.rb
122
121
  - ruby-progressbar.gemspec
123
122
  - spec/fixtures/benchmark.rb
124
- - spec/progress_bar/base_spec.rb
125
- - spec/progress_bar/components/bar_spec.rb
126
- - spec/progress_bar/components/elapsed_timer_spec.rb
127
- - spec/progress_bar/components/estimated_timer_spec.rb
128
- - spec/progress_bar/components/progressable_spec.rb
129
- - spec/progress_bar/components/throttle_spec.rb
130
- - spec/progress_bar/format/molecule_spec.rb
131
- - spec/progress_bar/running_average_calculator_spec.rb
132
- - spec/progress_bar/time_spec.rb
123
+ - spec/lib/ruby-progressbar/base_spec.rb
124
+ - spec/lib/ruby-progressbar/components/bar_spec.rb
125
+ - spec/lib/ruby-progressbar/components/elapsed_timer_spec.rb
126
+ - spec/lib/ruby-progressbar/components/estimated_timer_spec.rb
127
+ - spec/lib/ruby-progressbar/components/progressable_spec.rb
128
+ - spec/lib/ruby-progressbar/components/throttle_spec.rb
129
+ - spec/lib/ruby-progressbar/format/molecule_spec.rb
130
+ - spec/lib/ruby-progressbar/running_average_calculator_spec.rb
131
+ - spec/lib/ruby-progressbar/time_spec.rb
133
132
  - spec/spec_helper.rb
134
133
  - spec/support/time.rb
135
134
  homepage: https://github.com/jfelchner/ruby-progressbar
@@ -160,15 +159,15 @@ specification_version: 3
160
159
  summary: Ruby/ProgressBar is a flexible text progress bar library for Ruby.
161
160
  test_files:
162
161
  - spec/fixtures/benchmark.rb
163
- - spec/progress_bar/base_spec.rb
164
- - spec/progress_bar/components/bar_spec.rb
165
- - spec/progress_bar/components/elapsed_timer_spec.rb
166
- - spec/progress_bar/components/estimated_timer_spec.rb
167
- - spec/progress_bar/components/progressable_spec.rb
168
- - spec/progress_bar/components/throttle_spec.rb
169
- - spec/progress_bar/format/molecule_spec.rb
170
- - spec/progress_bar/running_average_calculator_spec.rb
171
- - spec/progress_bar/time_spec.rb
162
+ - spec/lib/ruby-progressbar/base_spec.rb
163
+ - spec/lib/ruby-progressbar/components/bar_spec.rb
164
+ - spec/lib/ruby-progressbar/components/elapsed_timer_spec.rb
165
+ - spec/lib/ruby-progressbar/components/estimated_timer_spec.rb
166
+ - spec/lib/ruby-progressbar/components/progressable_spec.rb
167
+ - spec/lib/ruby-progressbar/components/throttle_spec.rb
168
+ - spec/lib/ruby-progressbar/format/molecule_spec.rb
169
+ - spec/lib/ruby-progressbar/running_average_calculator_spec.rb
170
+ - spec/lib/ruby-progressbar/time_spec.rb
172
171
  - spec/spec_helper.rb
173
172
  - spec/support/time.rb
174
173
  has_rdoc:
@@ -1,6 +0,0 @@
1
- require 'progress_bar/components/timer'
2
- require 'progress_bar/components/progressable'
3
- require 'progress_bar/components/bar'
4
- require 'progress_bar/components/estimated_timer'
5
- require 'progress_bar/components/elapsed_timer'
6
- require 'progress_bar/components/throttle'
@@ -1,42 +0,0 @@
1
- class ProgressBar
2
- module Components
3
- class Bar
4
- include Progressable
5
-
6
- DEFAULT_PROGRESS_MARK = '='
7
-
8
- attr_accessor :progress_mark
9
- attr_accessor :length
10
-
11
- def initialize(options = {})
12
- super
13
-
14
- self.progress_mark = options[:progress_mark] || DEFAULT_PROGRESS_MARK
15
- end
16
-
17
- def to_s(options = {:format => :standard})
18
- completed_string = send(:"#{options[:format]}_complete_string")
19
- completed_string.ljust(length, ' ')
20
- end
21
-
22
- def integrated_percentage_complete_string
23
- return standard_complete_string if completed_length < 5
24
-
25
- " #{percentage_completed} ".to_s.center(completed_length, progress_mark)
26
- end
27
-
28
- def standard_complete_string
29
- progress_mark * completed_length
30
- end
31
-
32
- def empty_string
33
- ' ' * (length - completed_length)
34
- end
35
-
36
- private
37
- def completed_length
38
- length * percentage_completed / 100
39
- end
40
- end
41
- end
42
- end
@@ -1,121 +0,0 @@
1
- class ProgressBar
2
- module Depreciable
3
- DEPRECATION_DATE = "June 30th, 2013"
4
-
5
- def backwards_compatible_args_to_options_conversion(args)
6
- options = {}
7
-
8
- if args.size > 1
9
- puts "DEPRECATION WARNING: Creating progress bars using ProgressBar.new(title, total, output_io) has been deprecated and will be removed on or after #{DEPRECATION_DATE}. Please use ProgressBar.create(:title => title, :total => total, :output => output_io) instead. The full list of options can be found here: https://github.com/jfelchner/ruby-progressbar."
10
- options[:title] = args[0]
11
- options[:total] = args[1]
12
- options[:output] = args[2]
13
- else
14
- options = args[0]
15
- end
16
-
17
- options
18
- end
19
-
20
- def inc(value = nil)
21
- if value.nil?
22
- method_deprecation_message 'inc', 'increment'
23
-
24
- increment
25
- else
26
- method_removal_message 'inc', 'Rather than passing a step increment to the new #increment method, you can simply do `my_progress_bar.progress += step`.'
27
-
28
- progress = progress + value.to_i
29
- end
30
- end
31
-
32
- def set(new_value)
33
- method_deprecation_message 'set', 'progress='
34
-
35
- progress = new_value
36
- end
37
-
38
- def halt
39
- method_deprecation_message 'halt', 'stop'
40
-
41
- stop
42
- end
43
-
44
- def bar_mark=(mark)
45
- method_deprecation_message 'bar_mark=', 'progress_mark='
46
-
47
- progress_mark = mark
48
- end
49
-
50
- def title_width
51
- method_removal_message 'title_width', 'The formatter is now smart enough to handle any title you use. Set the format for the bar as described here: https://github.com/jfelchner/ruby-progressbar'
52
- end
53
-
54
- def title_width=(value)
55
- method_removal_message 'title_width=', 'The formatter is now smart enough to handle any title you use. Set the format for the bar as described here: https://github.com/jfelchner/ruby-progressbar'
56
- end
57
-
58
- def start_time
59
- method_deprecation_message 'start_time'
60
-
61
- @elapsed_time.instance_variable_get(:@started_at)
62
- end
63
-
64
- def start_time=(value)
65
- method_removal_message 'start_time=', 'It is no longer appropriate to set the start time of the bar. Using #start, #stop, #pause and #resume all work as expected.'
66
- end
67
-
68
- def format=(value)
69
- method_removal_message 'format=', 'The formatter has been completely rewriten for v1.0. Please use `#format(format_string)`. See https://github.com/jfelchner/ruby-progressbar for all the formatting options.'
70
- end
71
-
72
- def format_arguments=(value)
73
- method_removal_message 'format_arguments=', 'The formatter has been completely rewriten for v1.0. Please use `#format(format_string)`. See https://github.com/jfelchner/ruby-progressbar for all the formatting options.'
74
- end
75
-
76
- def current
77
- method_deprecation_message 'current', 'progress'
78
-
79
- @bar.progress
80
- end
81
-
82
- def smoothing
83
- method_removal_message 'smoothing'
84
- end
85
-
86
- def smoothing=(value)
87
- method_removal_message 'smoothing=', 'This value can only be set via the options hash when creating a new progress bar like so: ProgressBar.create(:smoothing => 0.2)'
88
- end
89
-
90
- def file_transfer_mode
91
- method_removal_message 'file_transfer_mode', 'We will be implementing a much better file transfer progress bar in the upcoming version however it has been removed for v1.0. If you still require this functionality, you should remain at v0.11.0'
92
- end
93
-
94
- private
95
-
96
- def method_deprecation_message(old_item, new_item = '')
97
- message_has_not_been_shown?(old_item) do
98
- replacement_message = new_item.empty? ? 'There will be no replacement.' : "Please use ##{new_item} instead."
99
- puts "DEPRECATION WARNING: ##{old_item} will be removed on or after #{DEPRECATION_DATE}. #{replacement_message}"
100
- end
101
- end
102
-
103
- def method_removal_message(old_item, message = '')
104
- message_has_not_been_shown?(old_item) do
105
- puts "REMOVAL WARNING: ##{old_item} has been removed. There is no replacement. #{message}"
106
- end
107
- end
108
-
109
- def safe_string(item)
110
- item.gsub('=', '_setter')
111
- end
112
-
113
- def message_has_not_been_shown?(item)
114
- unless instance_variable_get(:"@#{safe_string item}_deprecation_warning")
115
- instance_variable_set(:"@#{safe_string item}_deprecation_warning", true)
116
-
117
- yield
118
- end
119
- end
120
- end
121
- end
@@ -1,2 +0,0 @@
1
- require 'progress_bar/format/molecule'
2
- require 'progress_bar/format/base'
@@ -1,10 +0,0 @@
1
- require 'progress_bar/depreciable'
2
-
3
- # This file is provided for standardization purposes only.
4
- #
5
- # This gem didn't follow standard Ruby naming conventions and therefore, both `ruby-progressbar.rb` and `progressbar.rb` are needed.
6
- # `ruby-progressbar.rb` because the gem name should match the filename under `lib` and `progressbar.rb` because this gem was copied
7
- # directly from another gem and wasn't properly modified. There are other applications which require this file.
8
- #
9
- puts "DEPRECATION WARNING: Requiring ruby-progressbar using `require progressbar` or `gem progressbar` has been deprecated and will be disabled on or after #{ProgressBar::Depreciable::DEPRECATION_DATE}. Please use `require ruby-progressbar` or `gem ruby-progressbar` instead"
10
- require File.join(File.dirname(__FILE__), 'ruby-progressbar')