1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/irb] Commands should respect USE_COLORIZE config (https://github.com/ruby/irb/pull/362)

534688dfc4
This commit is contained in:
Stan Lo 2022-06-20 14:27:12 +01:00 committed by git
parent c46824d094
commit 2d4a41df6b
3 changed files with 8 additions and 6 deletions

View file

@ -10,7 +10,7 @@ module IRB
module ExtendCommand module ExtendCommand
class Ls < Nop class Ls < Nop
def execute(*arg, grep: nil) def execute(*arg, grep: nil)
o = Output.new(grep: grep) o = Output.new(grep: grep, colorable: colorable)
obj = arg.empty? ? irb_context.workspace.main : arg.first obj = arg.empty? ? irb_context.workspace.main : arg.first
locals = arg.empty? ? irb_context.workspace.binding.local_variables : [] locals = arg.empty? ? irb_context.workspace.binding.local_variables : []
@ -45,7 +45,8 @@ module IRB
class Output class Output
MARGIN = " " MARGIN = " "
def initialize(grep: nil) def initialize(grep: nil, colorable: true)
@colorable = colorable
@grep = grep @grep = grep
@line_width = screen_width - MARGIN.length # right padding @line_width = screen_width - MARGIN.length # right padding
end end
@ -56,7 +57,7 @@ module IRB
return if strs.empty? return if strs.empty?
# Attempt a single line # Attempt a single line
print "#{Color.colorize(name, [:BOLD, :BLUE])}: " print "#{Color.colorize(name, [:BOLD, :BLUE], colorable: @colorable)}: "
if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length) if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length)
puts strs.join(MARGIN) puts strs.join(MARGIN)
return return

View file

@ -29,9 +29,10 @@ module IRB
def initialize(conf) def initialize(conf)
@irb_context = conf @irb_context = conf
@colorable = Color.colorable? && conf.use_colorize
end end
attr_reader :irb_context attr_reader :irb_context, :colorable
def irb def irb
@irb_context.irb @irb_context.irb

View file

@ -30,7 +30,7 @@ module IRB
puts puts
puts "#{bold("From")}: #{source.file}:#{source.first_line}" puts "#{bold("From")}: #{source.file}:#{source.first_line}"
puts puts
code = IRB::Color.colorize_code(File.read(source.file)) code = IRB::Color.colorize_code(File.read(source.file), colorable: colorable)
puts code.lines[(source.first_line - 1)...source.last_line].join puts code.lines[(source.first_line - 1)...source.last_line].join
puts puts
end end
@ -78,7 +78,7 @@ module IRB
end end
def bold(str) def bold(str)
Color.colorize(str, [:BOLD]) Color.colorize(str, [:BOLD], colorable: colorable)
end end
Source = Struct.new( Source = Struct.new(