Skip to content

Commit

Permalink
Merge branch 'master' into development
Browse files Browse the repository at this point in the history
* master:
  Fix the 'negative argument' problem - Closes #47
  Update a spec that was passing when it shouldn't have been and pend it until we can implement the fix
  Upgrade rspec and fuubar
  When dividing up the remainder of the length and determining how much space a completed bar should take up, round down so that the bar doesn't complete until 100%
  Add tags file to gitignore
  • Loading branch information
jfelchner committed Aug 11, 2013
2 parents f10de69 + d89fd54 commit 5ef5cd6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
*.gem
tags
*.rbc
.bundle
.config
Expand Down
20 changes: 10 additions & 10 deletions Gemfile.lock
@@ -1,29 +1,29 @@
PATH
remote: .
specs:
ruby-progressbar (1.0.2)
ruby-progressbar (1.1.1)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.2.4)
fuubar (1.1.0)
fuubar (1.1.1)
rspec (~> 2.0)
rspec-instafail (~> 0.2.0)
ruby-progressbar (~> 1.0.0)
ruby-progressbar (~> 1.0)
json (1.7.7)
json (1.7.7-java)
multi_json (1.7.3)
rake (10.0.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.4)
rspec-expectations (2.14.1)
diff-lcs (>= 1.1.3, < 2.0)
rspec-instafail (0.2.4)
rspec-mocks (2.13.1)
rspec-mocks (2.14.3)
rspectacular (0.13.0)
fuubar (~> 1.0)
rspec (~> 2.12)
Expand Down
2 changes: 1 addition & 1 deletion lib/progress_bar/components/bar.rb
Expand Up @@ -35,7 +35,7 @@ def empty_string

private
def completed_length
length * percentage_completed / 100
(length * percentage_completed / 100).floor
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/progress_bar/format/base.rb
Expand Up @@ -21,6 +21,7 @@ def process(environment)
processed_string.gsub! '%%', '%'

leftover_bar_length = environment.send(:length) - processed_string.length + placeholder_length
leftover_bar_length = leftover_bar_length < 0 ? 0 : leftover_bar_length

bar_molecules.each do |molecule|
processed_string.gsub!("%#{molecule.key}", environment.send(molecule.method_name, leftover_bar_length).to_s)
Expand Down
63 changes: 54 additions & 9 deletions spec/progress_bar/base_spec.rb
Expand Up @@ -5,17 +5,62 @@
before do
@output = StringIO.new('', 'w+')
@progressbar = ProgressBar::Base.new(:output => @output, :length => 80, :throttle_rate => 0.0)
@output.rewind
end

describe 'terminal width dropping below title length' do
it 'should not crash' do
@progressbar = ProgressBar::Base.new(:output => @output)
@progressbar.stub(:terminal_width).and_return(100)
@progressbar.title = 'a'*95
@progressbar.stub(:terminal_width).and_return(60)
expect {
@progressbar.title = 'a'*95
}.to_not raise_error
context 'when the terminal width is shorter than the string being output' do
it 'can properly handle outputting the bar when the length changes on the fly to less than the minimum width' do
IO.stub_chain(:console, :winsize).and_return [1, 30]
@progressbar = ProgressBar::Base.new(:output => @output, :title => 'a' * 25, :format => '%t%B', :throttle_rate => 0.0)

@progressbar.start

IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar.increment

@output.rewind
@output.read.should match /\raaaaaaaaaaaaaaaaaaaaaaaaa \r\s+\raaaaaaaaaaaaaaaaaaaaaaaaa\r\z/
end

context 'and the bar length is calculated' do
it 'returns the proper string' do
IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21), :starting_at => 5, :total => 10)

@progressbar.to_s('%t%w').should eql '*********************'
end
end

context 'and the incomplete bar length is calculated' do
it 'returns the proper string' do
IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21))

@progressbar.to_s('%t%i').should eql '*********************'
end

it 'returns the proper string' do
IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 21), :starting_at => 5, :total => 10)

@progressbar.to_s('%t%i').should eql '*********************'
end
end

context 'and the full bar length is calculated (but lacks the space to output the entire bar)' do
it 'returns the proper string' do
IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 19), :starting_at => 5, :total => 10)

@progressbar.to_s('%t%B').should eql '******************* '
end

it 'returns the proper string' do
IO.stub_chain(:console, :winsize).and_return [1, 20]
@progressbar = ProgressBar::Base.new(:output => @output, :title => ('*' * 19), :starting_at => 5, :total => 10)

@progressbar.to_s('%t%w%i').should eql '******************* '
end
end
end

Expand Down

0 comments on commit 5ef5cd6

Please sign in to comment.