From 3aab4711216dbc5e0e0787077cd906b89eab1fc9 Mon Sep 17 00:00:00 2001 From: Ben Somers Date: Mon, 1 Aug 2016 16:56:07 -0700 Subject: [PATCH] Add: %u format flag to show ?? if total is unknown --- lib/ruby-progressbar/format/molecule.rb | 1 + lib/ruby-progressbar/progress.rb | 4 ++++ spec/ruby-progressbar/base_spec.rb | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/lib/ruby-progressbar/format/molecule.rb b/lib/ruby-progressbar/format/molecule.rb index c81aed44..c69b194b 100644 --- a/lib/ruby-progressbar/format/molecule.rb +++ b/lib/ruby-progressbar/format/molecule.rb @@ -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], diff --git a/lib/ruby-progressbar/progress.rb b/lib/ruby-progressbar/progress.rb index 8f4235cb..44941c91 100644 --- a/lib/ruby-progressbar/progress.rb +++ b/lib/ruby-progressbar/progress.rb @@ -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? diff --git a/spec/ruby-progressbar/base_spec.rb b/spec/ruby-progressbar/base_spec.rb index 5de4ce7e..31aa152f 100644 --- a/spec/ruby-progressbar/base_spec.rb +++ b/spec/ruby-progressbar/base_spec.rb @@ -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)