Skip to content
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

Helpers::Text: Add .colorize(color, text, **opts) #2150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

joallard
Copy link

@joallard joallard commented Aug 6, 2020

Good people of pry-core, hello!

I was in the process of developing some fun console coloring options as part of Pry/StackExplorer and Pry/Rails, and wanting to allow users to customize their colors, I noticed the following:

If the color instructions (coloring, boldness) are offered as data, it is a bit unwieldy to turn that into colored text. For instance, if the config says {color: :red, bold: true}, it's non-obvious to turn that into the right helper invocation.

There's also the caveat that bold needs to be applied before color. (or is it the reverse?) I can never remember.

(FYI, I also have another patch in store that allows for faded text. Stay tuned)

Now this patch is a bit quick and dirty, so have at it. Is there some bandwidth and appetite at maintenance to merge this if the tests and the code are good? Let me know and I would get on that.

@joallard
Copy link
Author

joallard commented Aug 6, 2020

I'm unsure of a lot of things here, one of them is arg order. Does it make more sense to sign (text, color, bold?)?

That way:

config = [color, bold]

# easier
colorize(text, *config)

# versus
colorize(config[0], text, config[1])

# @return [String] Text
def colorize(color, text, bold = false)
text
.then { |it| bold ? bold(it) : it }
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add tests to this? I believe this is breaking due to the override of the bold method by the bold argument.

Copy link
Author

Choose a reason for hiding this comment

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

Yep, done. It's weird because the code was working well on my side.

I've simplified a few things, you can test with puts "\e[1;34mPry\e[0m" (bold blue) if you need

@joallard joallard changed the title Add Pry::Helpers::Text.colorize(color, text, bold?) Helpers::Text: Add .colorize(color, text, **opts) Dec 24, 2020
@joallard
Copy link
Author

joallard commented Dec 24, 2020

I've also added #escape(text, *codes), a lower-level method that stacks SGR escape codes into escaped text.

Rebased on top of master for your utmost enjoyment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants