diff --git a/README.mkd b/README.mkd index 707f276..8e84b81 100644 --- a/README.mkd +++ b/README.mkd @@ -20,7 +20,6 @@ lots of progress bar alternatives, and we thank you for using ProgressBar! ## The Easy Way - require 'progress_bar' bar = ProgressBar.new @@ -39,11 +38,16 @@ it.* ## Setting the Max -Usually, the defaults should be fine, the only thing you'll need to -tweak is the max. - bar = ProgressBar.new(1000) +## Setting the progress bar characters + +By default, ProgressBar will use a default bar character to show progress and +side delimiters. You can set any valid character that is supported by your +terminal for either the bar character or the delimiters. + + bar = ProgressBar.new(bar: '█', delimiters: '||') + ## Larger Steps If you want to process several things, and update less often, you can diff --git a/lib/progress_bar.rb b/lib/progress_bar.rb index 19149cc..ca68b82 100644 --- a/lib/progress_bar.rb +++ b/lib/progress_bar.rb @@ -1,4 +1,3 @@ - require 'options' require 'highline' @@ -6,18 +5,26 @@ class ProgressBar Error = Class.new(StandardError) ArgumentError = Class.new(Error) - attr_accessor :count, :max, :meters + DEFAULT_METERS = [:bar, :counter, :percentage, :elapsed, :eta, :rate] - def initialize(*args) + attr_accessor :count, :max, :meters, :bar, :delimiters + def initialize(*args, bar: '#', delimiters: '[]') @count = 0 @max = 100 - @meters = [:bar, :counter, :percentage, :elapsed, :eta, :rate] @max = args.shift if args.first.is_a? Numeric raise ArgumentError, "Max must be a positive integer" unless @max >= 0 - @meters = args unless args.empty? + @meters = args.empty? ? DEFAULT_METERS : args + + @bar = bar # can be an emoji which itself can be a sequence of characters + raise ArgumentError, 'Bar must be a valid string' unless @bar&.is_a?(String) + + @delimiters = delimiters + unless @delimiters&.size == 2 + raise ArgumentError, 'Delimiters must be two characters' + end @last_write = ::Time.at(0) @start = ::Time.now @@ -73,8 +80,8 @@ def eta def to_s self.count = max if count > max - meters.inject("") do |text, meter| - text << render(meter) + " " + meters.inject('') do |text, meter| + text << render(meter) + ' ' end.strip end @@ -98,12 +105,14 @@ def width_of(meter) def render_bar return '' if bar_width < 2 + progress_width = (ratio * (bar_width - 2)).floor remainder_width = bar_width - 2 - progress_width - "[" + - "#" * progress_width + - " " * remainder_width + - "]" + + delimiters[0] + + bar * progress_width + + ' ' * remainder_width + + delimiters[-1] end def render_counter @@ -188,7 +197,6 @@ def format_interval(interval) "%02i:%02i" % [interval/60, interval%60] end end - end require_relative 'progress_bar/with_progress' diff --git a/spec/arguments_spec.rb b/spec/arguments_spec.rb index 964dc8b..824fc83 100644 --- a/spec/arguments_spec.rb +++ b/spec/arguments_spec.rb @@ -6,35 +6,69 @@ @default_meters = [:bar, :counter, :percentage, :elapsed, :eta, :rate] end - it "should set appropriate defaults without any arguments" do + it 'should set appropriate defaults without any arguments' do bar = ProgressBar.new bar.max.should == @default_max bar.meters.should == @default_meters end - it "should allow a single argument specifying the max" do + it 'should allow a single argument specifying the max' do bar = ProgressBar.new(123) bar.max.should == 123 bar.meters.should == @default_meters end - it "should allow specifying just the meters" do + it 'should allow specifying just the meters' do bar = ProgressBar.new(:bar, :eta) bar.max.should == @default_max bar.meters.should == [:bar, :eta] end - it "should allow specyfing the max and meters" do + it 'should allow specifying the max and meters' do bar = ProgressBar.new(123, :bar, :eta) bar.max.should == 123 bar.meters.should == [:bar, :eta] end - it "should raise an error when initial max is nonsense" do + it 'should allow specifying the bar and delimiters' do + bar = ProgressBar.new(bar: '$', delimiters: '||') + bar.bar.should == '$' + bar.delimiters.should == '||' + end + + it 'should raise an error when initial max is nonsense' do lambda { - bar = ProgressBar.new(-1) + _bar = ProgressBar.new(-1) }.should raise_error(ProgressBar::ArgumentError) end -end + it 'should raise an error when bar is not a string' do + lambda { + _bar = ProgressBar.new(bar: 5) + }.should raise_error(ProgressBar::ArgumentError) + end + + it 'should raise an error when bar is empty' do + lambda { + _bar = ProgressBar.new(bar: nil) + }.should raise_error(ProgressBar::ArgumentError) + end + + it 'should raise an error when delimiters is less than two characters' do + lambda { + _bar = ProgressBar.new(delimiters: '|') + }.should raise_error(ProgressBar::ArgumentError) + end + it 'should raise an error when delimiters is more than two characters' do + lambda { + _bar = ProgressBar.new(delimiters: '|||') + }.should raise_error(ProgressBar::ArgumentError) + end + + it 'should raise an error when delimiters is empty' do + lambda { + _bar = ProgressBar.new(delimiters: '') + }.should raise_error(ProgressBar::ArgumentError) + end +end diff --git a/spec/bar_spec.rb b/spec/bar_spec.rb index e8866c8..8e3c341 100644 --- a/spec/bar_spec.rb +++ b/spec/bar_spec.rb @@ -50,6 +50,4 @@ end end end - end - diff --git a/spec/counter_spec.rb b/spec/counter_spec.rb index 65a6acc..0c294e2 100644 --- a/spec/counter_spec.rb +++ b/spec/counter_spec.rb @@ -46,5 +46,4 @@ it { should == '[ 0/4242]' } end - end diff --git a/spec/elapsed_spec.rb b/spec/elapsed_spec.rb index 4128e37..3eaf409 100644 --- a/spec/elapsed_spec.rb +++ b/spec/elapsed_spec.rb @@ -1,4 +1,3 @@ - require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) describe 'ProgressBar elapsed output' do @@ -43,6 +42,4 @@ it { should == '[02:00:00]' } end - end - diff --git a/spec/eta_spec.rb b/spec/eta_spec.rb index 5c6bacf..c65837f 100644 --- a/spec/eta_spec.rb +++ b/spec/eta_spec.rb @@ -43,8 +43,4 @@ it { should == '[02:00:00]' } end - end - - - diff --git a/spec/percentage_spec.rb b/spec/percentage_spec.rb index 6465f12..e48e158 100644 --- a/spec/percentage_spec.rb +++ b/spec/percentage_spec.rb @@ -39,6 +39,4 @@ it { should == '[ 50.00%]' } end - end - diff --git a/spec/progress_bar_characters_spec.rb b/spec/progress_bar_characters_spec.rb new file mode 100644 index 0000000..6330007 --- /dev/null +++ b/spec/progress_bar_characters_spec.rb @@ -0,0 +1,53 @@ +require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) + +describe 'ProgressBar bar output with custom characters' do + before do + Timecop.freeze Time.utc(2010, 3, 10, 0, 0, 0) + @progress_bar = ProgressBar.new(100, bar: '█', delimiters: '▕▏') + @progress_bar.stub(:terminal_width) { 60 } + Timecop.freeze Time.utc(2010, 3, 10, 0, 0, 10) # 10 seconds later + end + + subject { @progress_bar.to_s } + + describe 'at count=0' do + before do + @progress_bar.count = 0 + end + + it { should == '▕ ▏ [ 0/100] [ 0%] [00:10] [00:00] [ 0.00/s]' } + end + + describe 'at count=50' do + before do + @progress_bar.count = 50 + end + + it { should == '▕███████ ▏ [ 50/100] [ 50%] [00:10] [00:10] [ 5.00/s]' } + end + + describe 'at count=100' do + before do + @progress_bar.count = 100 + end + + it { should == '▕██████████████▏ [100/100] [100%] [00:10] [00:00] [ 10.00/s]' } + end + + describe 'at count=105' do + before do + @progress_bar.count = 105 + end + + it { should == '▕██████████████▏ [100/100] [100%] [00:10] [00:00] [ 10.00/s]' } + end + + describe 'at count=100 with a multi-sequence emoji' do + before do + @progress_bar.count = 100 + @progress_bar.bar = "🏴‍☠️" + end + + it { should == '▕🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️🏴‍☠️▏ [100/100] [100%] [00:10] [00:00] [ 10.00/s]' } + end +end diff --git a/spec/progress_bar_spec.rb b/spec/progress_bar_spec.rb index f69f8e6..c4d8126 100644 --- a/spec/progress_bar_spec.rb +++ b/spec/progress_bar_spec.rb @@ -41,7 +41,4 @@ it { should == "[##############] [100/100] [100%] [00:10] [00:00] [ 10.00/s]" } end - end - - diff --git a/spec/rate_spec.rb b/spec/rate_spec.rb index 8171c04..53ce38e 100644 --- a/spec/rate_spec.rb +++ b/spec/rate_spec.rb @@ -54,10 +54,4 @@ it { should == '[ 2.10/s]' } end - end - - - - - diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 63cc043..773b3c6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,3 @@ - require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib/progress_bar')) require 'rspec' @@ -9,4 +8,3 @@ Timecop.return end end -