diff options
author | Stan Lo <stan001212@gmail.com> | 2022-06-28 14:30:36 +0100 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-06-28 22:30:42 +0900 |
commit | 44c1316293f80abaa0e76b3818322544b9372a97 (patch) | |
tree | c560d27798f751ab059b4db59de749dbdd09b1de /lib/irb/inspector.rb | |
parent | 5ccdcd81685cfedd31344690fdb0fd9fc001e3ca (diff) |
[ruby/irb] Centralize coloring control (https://github.com/ruby/irb/pull/374)
* 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 https://github.com/ruby/irb/pull/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
https://github.com/ruby/irb/commit/1c53023ac4
Diffstat (limited to 'lib/irb/inspector.rb')
-rw-r--r-- | lib/irb/inspector.rb | 12 |
1 files changed, 2 insertions, 10 deletions
diff --git a/lib/irb/inspector.rb b/lib/irb/inspector.rb index 8c37c0f174..d8c0ba90cf 100644 --- a/lib/irb/inspector.rb +++ b/lib/irb/inspector.rb @@ -108,18 +108,10 @@ module IRB # :nodoc: Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s} Inspector.def_inspector([:p, :inspect]){|v| - result = v.inspect - if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v) - result = Color.colorize_code(result) - end - result + Color.colorize_code(v.inspect, colorable: Color.colorable? && Color.inspect_colorable?(v)) } Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require_relative "color_printer"}){|v| - if IRB.conf[:MAIN_CONTEXT]&.use_colorize? - IRB::ColorPrinter.pp(v, '').chomp - else - v.pretty_inspect.chomp - end + IRB::ColorPrinter.pp(v, '').chomp } Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v| begin |