diff options
author | Stan Lo <[email protected]> | 2022-06-28 14:30:36 +0100 |
---|---|---|
committer | git <[email protected]> | 2022-06-28 22:30:42 +0900 |
commit | 44c1316293f80abaa0e76b3818322544b9372a97 (patch) | |
tree | c560d27798f751ab059b4db59de749dbdd09b1de /lib/irb/cmd | |
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/cmd')
-rw-r--r-- | lib/irb/cmd/ls.rb | 7 | ||||
-rw-r--r-- | lib/irb/cmd/nop.rb | 3 | ||||
-rw-r--r-- | lib/irb/cmd/show_source.rb | 4 |
3 files changed, 6 insertions, 8 deletions
diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index 6a30a28ea0..f4a7348bd1 100644 --- a/lib/irb/cmd/ls.rb +++ b/lib/irb/cmd/ls.rb @@ -10,7 +10,7 @@ module IRB module ExtendCommand class Ls < Nop def execute(*arg, grep: nil) - o = Output.new(grep: grep, colorable: colorable) + o = Output.new(grep: grep) obj = arg.empty? ? irb_context.workspace.main : arg.first locals = arg.empty? ? irb_context.workspace.binding.local_variables : [] @@ -45,8 +45,7 @@ module IRB class Output MARGIN = " " - def initialize(grep: nil, colorable: true) - @colorable = colorable + def initialize(grep: nil) @grep = grep @line_width = screen_width - MARGIN.length # right padding end @@ -57,7 +56,7 @@ module IRB return if strs.empty? # Attempt a single line - print "#{Color.colorize(name, [:BOLD, :BLUE], colorable: @colorable)}: " + print "#{Color.colorize(name, [:BOLD, :BLUE])}: " if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length) puts strs.join(MARGIN) return diff --git a/lib/irb/cmd/nop.rb b/lib/irb/cmd/nop.rb index 17ff2f9b7e..881a736722 100644 --- a/lib/irb/cmd/nop.rb +++ b/lib/irb/cmd/nop.rb @@ -29,10 +29,9 @@ module IRB def initialize(conf) @irb_context = conf - @colorable = Color.colorable? && conf.use_colorize end - attr_reader :irb_context, :colorable + attr_reader :irb_context def irb @irb_context.irb diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb index 7e790c3c93..f8a17822df 100644 --- a/lib/irb/cmd/show_source.rb +++ b/lib/irb/cmd/show_source.rb @@ -30,7 +30,7 @@ module IRB puts puts "#{bold("From")}: #{source.file}:#{source.first_line}" puts - code = IRB::Color.colorize_code(File.read(source.file), colorable: colorable) + code = IRB::Color.colorize_code(File.read(source.file)) puts code.lines[(source.first_line - 1)...source.last_line].join puts end @@ -78,7 +78,7 @@ module IRB end def bold(str) - Color.colorize(str, [:BOLD], colorable: colorable) + Color.colorize(str, [:BOLD]) end Source = Struct.new( |