Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Binary file added example_ascii_styles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions lib/progress_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt that this value can be useful if someone is now adding prefixes and suffixes they may need this value in outside calculations


def initialize(*args)
def initialize(*args, prefix: nil, suffix: nil)

@count = 0
@max = 100
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion spec/arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 9 additions & 2 deletions spec/progress_bar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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