Skip to content

Commit

Permalink
[ruby/irb] Commands should respect USE_COLORIZE config (ruby/irb#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored and matzbot committed Jun 20, 2022
1 parent c46824d commit 2d4a41d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/irb/cmd/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module IRB
module ExtendCommand
class Ls < Nop
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
locals = arg.empty? ? irb_context.workspace.binding.local_variables : []
Expand Down Expand Up @@ -45,7 +45,8 @@ def class_method_map(classes)
class Output
MARGIN = " "

def initialize(grep: nil)
def initialize(grep: nil, colorable: true)
@colorable = colorable
@grep = grep
@line_width = screen_width - MARGIN.length # right padding
end
Expand All @@ -56,7 +57,7 @@ def dump(name, strs)
return if strs.empty?

# 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)
puts strs.join(MARGIN)
return
Expand Down
3 changes: 2 additions & 1 deletion lib/irb/cmd/nop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ def self.execute(conf, *opts, &block)

def initialize(conf)
@irb_context = conf
@colorable = Color.colorable? && conf.use_colorize
end

attr_reader :irb_context
attr_reader :irb_context, :colorable

def irb
@irb_context.irb
Expand Down
4 changes: 2 additions & 2 deletions lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def show_source(source)
puts
puts "#{bold("From")}: #{source.file}:#{source.first_line}"
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
end
Expand Down Expand Up @@ -78,7 +78,7 @@ def find_end(file, first_line)
end

def bold(str)
Color.colorize(str, [:BOLD])
Color.colorize(str, [:BOLD], colorable: colorable)
end

Source = Struct.new(
Expand Down

0 comments on commit 2d4a41d

Please sign in to comment.