-
-
Notifications
You must be signed in to change notification settings - Fork 49
Feature: add in custom bar character and delimiters #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feature: add in custom bar character and delimiters #45
Conversation
spec/arguments_spec.rb
Outdated
| end | ||
|
|
||
| end | ||
| it 'should raise an error when bar is more than one character' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a test that it allows multibyte UTF characters? Like ║.
| 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: '▕▏') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, there is kind of a test for multibyte characters I guess. Why are there two separate spec files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasnt certain the arguments_spec really covered the use case. I was more of an interface positive negative check. I wanted a test that specifically checked the feature and its output
…d-in-custom-bar-character
|
@paul Is this PR fine to merge? Or is there more you would like me to change to get it mergeable? |
| @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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to remove the character limit because lots of new emojis are multi-sequence characters. This isn't supported everywhere but in modern terminals it is. I dont see a reason to limit this functionality and its up to the developer using this Gem to make sure the character doesnt mess up for their use case if they select a multi-character sequence
|
So totally ridiculous but Ive made a Nyan cat loading bar. A colleague of mine joked that it'd be funny if I got something like that to work I had to do some outside overriding but it's pretty funny that this is possible. It does use a few zero width joiners which may hurt if this code is copy-pasted |
|
Hah, I love the nyancat bar. I was thinking along similar lines from our themeing discussion the other day, it would be useful if the theme values were permitted to be strings or functions. Then when the bar prints, it would yield to the function for each one with info about the current progress. So your nyancat could just be: I'd need to play with it some more, i'm not sure what values should be yielded to the function. |
|
Yeah Ive been thinking about that as well. Ideally the interface should be permissive to things like #51 the prefix and suffix stuff but also more advanced lambdas. Since I think a lambda to change the colour seems too complicated but "Nyan" cat or more complex stylings and animations would require something more complex to achieve. Unless only one new interface parameter is added which accepts a lambda and returns some kind of hash / function which overwrites the default character sets, and/or output string (prefix / suffix). And that function gets a uniform interface of things like terminal_width, the bar_width, set default values, position, max, count, time etc ... and can modify values which would include adding conditional logic to the bar character for instance |
|
@TRex22 I've been tinkering: require "paint"
require_relative "../lib/progress_bar"
CAT = "🐱"
# RAINBOW = "🏳️🌈"
RAINBOW = "🌈"
def self.rainbow(ratio)
blue = Math.sin(ratio + 0) * 127 + 128
red = Math.sin(ratio + 2 * Math::PI / 3) * 127 + 128
green = Math.sin(ratio + 4 * Math::PI / 3) * 127 + 128
"#%02X%02X%02X" % [red, green, blue]
end
theme = ProgressBar::Theme.new(
line_prefix: ->(bar, **) {
color = rainbow(bar.ratio * 6)
Paint.color(color)
},
bar_element: ->(bar, pos:, width:) {
if pos + bar.display_width(CAT) >= width
CAT
else
RAINBOW
end
}
)
bar = ProgressBar.new(100, theme: theme)
100.times do
bar.increment!
sleep 0.1
end |
|
I like that approach. Its very clean. Means only one parameter is needed and its up to the dev to decide to add in helper gems or not. In the documentation there could also be example themes like the one above |
|
Hey @paul sorry I let this slip. Just had some life issues, studies, and work get in the way. Ill work on getting a solution out when I can and cleaning up my PRs |

What does this PR do?