Skip to content

Commit

Permalink
Add: %W flag for complete_bar_with_percentage
Browse files Browse the repository at this point in the history
Why This Change Is Necessary
========================================================================

- [ ] Bug Fix
- [x] New Feature

Exposes already existing support for complete (full-length) progress bar
with integrated percentage.

Side Effects Caused By This Change
========================================================================

- [ ] This Causes a Breaking Change
- [x] This Does Not Cause Any Known Side Effects

Checklist
========================================================================

- [x] I have run `rubocop` against the codebase
- [x] I have added tests to cover my changes
- [x] All new and existing tests passed

------------------------------------------------------------------------
Github Issue URLs:
  * #146
  • Loading branch information
jfelchner committed Aug 3, 2018
2 parents 7b23c06 + 80b28dd commit a9b739d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/ruby-progressbar/components/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ def bar(length)
def complete_bar(length)
self.length = length

to_s
to_s(:format => :standard)
end

def complete_bar_with_percentage(length)
self.length = length

to_s(:format => :integrated_percentage)
end

def unknown_string
Expand Down
3 changes: 2 additions & 1 deletion lib/ruby-progressbar/format/molecule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Molecule
:f => [:time, :estimated_with_no_oob],
:B => [:bar, :complete_bar],
:b => [:bar, :bar],
:W => [:bar, :complete_bar_with_percentage],
:w => [:bar, :bar_with_percentage],
:i => [:bar, :incomplete_space],
:r => [:rate, :rate_of_change],
:R => [:rate, :rate_of_change_with_precision]
}.freeze

BAR_MOLECULES = %w{w B b i}.freeze
BAR_MOLECULES = %w{W w B b i}.freeze

attr_accessor :key,
:method_name
Expand Down
21 changes: 21 additions & 0 deletions spec/ruby-progressbar/format/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,27 @@ module Format
end
end

context 'the %W flag' do
let(:format) { Format::String.new('%W') }

it 'is the bar with percentage (including incomplete space)' do
progressbar = ProgressBar::Base.new(:length => 100)

expect(Formatter.process(format, 100, progressbar)).to eql ' ' * 100
4.times { progressbar.increment }
expect(Formatter.process(format, 100, progressbar)).to eql "====#{' ' * 96}"
progressbar.increment
expect(Formatter.process(format, 100, progressbar)).to eql "= 5 =#{' ' * 95}"
5.times { progressbar.increment }
expect(Formatter.process(format, 100, progressbar)).to eql "=== 10 ===#{' ' * 90}"
progressbar.decrement
expect(Formatter.process(format, 100, progressbar)).to eql "=== 9 ===#{' ' * 91}"
91.times { progressbar.increment }
expect(Formatter.process(format, 100, progressbar)).to eql \
"#{'=' * 47} 100 #{'=' * 48}"
end
end

context 'the %w flag' do
let(:format) { Format::String.new('%w') }

Expand Down

0 comments on commit a9b739d

Please sign in to comment.