-
Notifications
You must be signed in to change notification settings - Fork 120
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
Centralize coloring control #374
Conversation
There are 2 requirements for coloring output: 1. It's supported on the platform 2. The user wants it: `IRB.conf[:USE_COLORIZE] == true` Right now we check 1 and 2 separately whenever we colorize things. But it's error-prone because while 1 is the default of `colorable` parameter, 2 always need to manually checked. When 2 is overlooked, it causes issues like ruby#362 And there's 0 case where we may want to colorize even when the user disables it. So I think we should merge 2 into `Color.colorable?` so it can be automatically picked up.
6bfaba8
to
44a78d1
Compare
18f3406
to
8716892
Compare
8716892
to
6be5a08
Compare
[:yaml, "BasicObject.new", /\(Object doesn't support #inspect\)\n(=> )?\n/], | ||
[:marshal, "[Object.new, Class.new]", /\(Object doesn't support #inspect\)\n(=> )?\n/] | ||
] | ||
}.each do |scenario, cases| |
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.
@peterzhu2118 I ended up merging all cases together as the test body is the same. Wdyt?
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 like it
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.
Looks good, thank you!
* Use colorable: argument as the only coloring control * Centalize color controling logic at Color.colorable? There are 2 requirements for coloring output: 1. It's supported on the platform 2. The user wants it: `IRB.conf[:USE_COLORIZE] == true` Right now we check 1 and 2 separately whenever we colorize things. But it's error-prone because while 1 is the default of `colorable` parameter, 2 always need to manually checked. When 2 is overlooked, it causes issues like ruby/irb#362 And there's 0 case where we may want to colorize even when the user disables it. So I think we should merge 2 into `Color.colorable?` so it can be automatically picked up. * Add tests for all inspect modes * Simplify inspectors' coloring logic * Replace use_colorize? with Color.colorable? * Remove Context#use_colorize cause it's redundant ruby/irb@1c53023ac4
When deciding whether to color the output, we should always consider 2 things:
If one of them is
false
, we shouldn't color the output.However, the current implementation only checks 1 by default and 2 needs an additional condition.
It makes code unnecessarily complicated and also error-prone. For example #362 was caused by missing 2.
So in this PR, I moved 2 into
Color.colorable?
(where 1 locates) and removes all the manual check on 2, as well asContext#use_colorize
.Test Result
Colorized
Non-colorized
(The weird floating "s"s were caused by my Neovim + Floaterm setup.)