diff --git a/README.mkd b/README.mkd index 707f276..40374f7 100644 --- a/README.mkd +++ b/README.mkd @@ -110,3 +110,20 @@ require 'progress_bar/core_ext/enumerable_with_progress' (1..400).with_progress.select{|i| (i % 2).zero?} ``` + +## Using prefix and suffix overrides to add styling to the progress bar + +If you want to change the style of the progress bar such as its color you can +do so by using the prefix and suffix modifiers + +Let's say you want to the progress_bar to be inverted green which has the style +code 42, then initialize the progress bar: + +```ruby +style_code = 42 +bar = ProgressBar.new(100, prefix: "\e[#{style_code}m", suffix: "\e[0m") +``` + +### Some examples of ASCII styles you can apply + +![style_examples](example_ascii_styles.png) diff --git a/example_ascii_styles.png b/example_ascii_styles.png new file mode 100644 index 0000000..a60f398 Binary files /dev/null and b/example_ascii_styles.png differ diff --git a/lib/progress_bar.rb b/lib/progress_bar.rb index 19149cc..bce351a 100644 --- a/lib/progress_bar.rb +++ b/lib/progress_bar.rb @@ -6,9 +6,10 @@ class ProgressBar Error = Class.new(StandardError) ArgumentError = Class.new(Error) - attr_accessor :count, :max, :meters + attr_accessor :count, :max, :meters, :prefix, :suffix + attr_reader :terminal_width # Useful for prefix and suffix changes - def initialize(*args) + def initialize(*args, prefix: nil, suffix: nil) @count = 0 @max = 100 @@ -19,6 +20,12 @@ def initialize(*args) @meters = args unless args.empty? + raise ArgumentError, 'Prefix must be a valid string' unless prefix.nil? || prefix.is_a?(String) + @prefix = prefix + + raise ArgumentError, 'Suffix must be a valid string' unless suffix.nil? || suffix.is_a?(String) + @suffix = suffix + @last_write = ::Time.at(0) @start = ::Time.now @@ -73,9 +80,12 @@ def eta def to_s self.count = max if count > max - meters.inject("") do |text, meter| + + progress_bar_text = meters.inject("") do |text, meter| text << render(meter) + " " end.strip + + "#{prefix}#{progress_bar_text}#{suffix}" end protected diff --git a/spec/arguments_spec.rb b/spec/arguments_spec.rb index 964dc8b..7446435 100644 --- a/spec/arguments_spec.rb +++ b/spec/arguments_spec.rb @@ -18,6 +18,12 @@ bar.meters.should == @default_meters end + it "should allow prefix and suffix arguments and set their instance variables" do + bar = ProgressBar.new(prefix: "\e[42m", suffix: "\e[0m") + bar.prefix.should == "\e[42m" + bar.suffix.should == "\e[0m" + end + it "should allow specifying just the meters" do bar = ProgressBar.new(:bar, :eta) bar.max.should == @default_max @@ -36,5 +42,15 @@ }.should raise_error(ProgressBar::ArgumentError) end -end + it "should raise an error when initial prefix is nonsense" do + lambda { + bar = ProgressBar.new(prefix: 200) + }.should raise_error(ProgressBar::ArgumentError) + end + it "should raise an error when initial suffix is nonsense" do + lambda { + bar = ProgressBar.new(suffix: 200) + }.should raise_error(ProgressBar::ArgumentError) + end +end diff --git a/spec/progress_bar_spec.rb b/spec/progress_bar_spec.rb index f69f8e6..78ba275 100644 --- a/spec/progress_bar_spec.rb +++ b/spec/progress_bar_spec.rb @@ -42,6 +42,13 @@ it { should == "[##############] [100/100] [100%] [00:10] [00:00] [ 10.00/s]" } end -end - + describe 'at count=100 with style prefix and suffix' do + before do + @progress_bar.count = 100 + @progress_bar.prefix = "\e[42m" + @progress_bar.suffix = "\e[0m" + end + it { should == "\e[42m[##############] [100/100] [100%] [00:10] [00:00] [ 10.00/s]\e[0m" } + end +end