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

Centralize coloring control #374

Merged
merged 6 commits into from Jun 28, 2022

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Jun 26, 2022

When deciding whether to color the output, we should always consider 2 things:

  1. Coloring is supported on the current mode (tty) and platform
  2. The user has coloring enabled

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 as Context#use_colorize.

Test Result

Colorized

截圖 2022-06-26 17 21 04

Non-colorized

(The weird floating "s"s were caused by my Neovim + Floaterm setup.)

截圖 2022-06-26 17 21 49

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.
@st0012 st0012 marked this pull request as ready for review June 26, 2022 16:22
@st0012 st0012 force-pushed the centralize-coloring-logic branch 3 times, most recently from 6bfaba8 to 44a78d1 Compare June 27, 2022 19:51
@st0012
Copy link
Member Author

st0012 commented Jun 27, 2022

cc @peterzhu2118

test/irb/test_context.rb Outdated Show resolved Hide resolved
test/irb/test_context.rb Outdated Show resolved Hide resolved
@st0012 st0012 force-pushed the centralize-coloring-logic branch 2 times, most recently from 18f3406 to 8716892 Compare June 27, 2022 20:33
@st0012 st0012 force-pushed the centralize-coloring-logic branch from 8716892 to 6be5a08 Compare June 27, 2022 20:40
[: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|
Copy link
Member Author

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?

Copy link
Member

Choose a reason for hiding this comment

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

👍 I like it

Copy link
Member

@peterzhu2118 peterzhu2118 left a 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!

@peterzhu2118 peterzhu2118 merged commit 1c53023 into ruby:master Jun 28, 2022
matzbot pushed a commit to ruby/ruby that referenced this pull request Jun 28, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants