Skip to content

Commit

Permalink
Add: %u format flag to show ?? if total is unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
bensomers authored and jfelchner committed Dec 10, 2016
1 parent 02a7cbe commit 3aab471
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ruby-progressbar/format/molecule.rb
Expand Up @@ -6,6 +6,7 @@ class Molecule
:T => [:title_comp, :title],
:c => [:progressable, :progress],
:C => [:progressable, :total],
:u => [:progressable, :total_with_unknown_indicator],
:p => [:percentage, :percentage],
:P => [:percentage, :percentage_with_precision],
:j => [:percentage, :justified_percentage],
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby-progressbar/progress.rb
Expand Up @@ -100,6 +100,10 @@ def unknown?
progress.nil? || total.nil?
end

def total_with_unknown_indicator
total || '??'
end

def percentage_completed_with_precision
return 100.0 if total == 0
return 0.0 if total.nil?
Expand Down
12 changes: 12 additions & 0 deletions spec/ruby-progressbar/base_spec.rb
Expand Up @@ -800,6 +800,18 @@
expect(progressbar.to_s('%C')).to match(/^100\z/)
end

it 'displays nothing when the total is unknown and when passed the "%C" format flag' do
progressbar = ProgressBar::Base.new(:total => nil)

expect(progressbar.to_s('%C')).to match(/^\z/)
end

it 'displays ?? when the total is unknown and when passed the "%u" format flag' do
progressbar = ProgressBar::Base.new(:total => nil)

expect(progressbar.to_s('%u')).to match(/^\?\?\z/)
end

it 'displays the percentage complete when passed the "%p" format flag' do
progressbar = ProgressBar::Base.new(:starting_at => 33, :total => 200)

Expand Down

0 comments on commit 3aab471

Please sign in to comment.