From 6a9e7def14e778d553a35e4ff4eba45de4616c33 Mon Sep 17 00:00:00 2001 From: st0012 Date: Mon, 9 May 2022 11:38:46 +0100 Subject: [PATCH 1/3] ls command should respect colorize config --- lib/irb/cmd/ls.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index f4a7348bd..9ccb34d46 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) + o = Output.new(grep: grep, use_colorize: irb_context.use_colorize) obj = arg.empty? ? irb_context.workspace.main : arg.first locals = arg.empty? ? irb_context.workspace.binding.local_variables : [] @@ -45,7 +45,8 @@ def class_method_map(classes) class Output MARGIN = " " - def initialize(grep: nil) + def initialize(grep: nil, use_colorize: true) + @use_colorize = use_colorize @grep = grep @line_width = screen_width - MARGIN.length # right padding end @@ -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: Color.colorable? && @use_colorize)}: " if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length) puts strs.join(MARGIN) return From 5599e5b2228cf15fe6c769510f4c54de8910cd73 Mon Sep 17 00:00:00 2001 From: st0012 Date: Mon, 9 May 2022 11:58:45 +0100 Subject: [PATCH 2/3] show_source command should respect colorize config --- lib/irb/cmd/show_source.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb index f8a17822d..ba3d9d19a 100644 --- a/lib/irb/cmd/show_source.rb +++ b/lib/irb/cmd/show_source.rb @@ -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: Color.colorable? && irb_context.use_colorize) puts code.lines[(source.first_line - 1)...source.last_line].join puts end @@ -78,7 +78,7 @@ def find_end(file, first_line) end def bold(str) - Color.colorize(str, [:BOLD]) + Color.colorize(str, [:BOLD], colorable: Color.colorable? && irb_context.use_colorize) end Source = Struct.new( From fe728e57e5840983fd690a07ea0184c471834e72 Mon Sep 17 00:00:00 2001 From: st0012 Date: Mon, 9 May 2022 12:08:24 +0100 Subject: [PATCH 3/3] Refactor cmd's colorable logic --- lib/irb/cmd/ls.rb | 8 ++++---- lib/irb/cmd/nop.rb | 3 ++- lib/irb/cmd/show_source.rb | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb index 9ccb34d46..6a30a28ea 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, use_colorize: irb_context.use_colorize) + 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 : [] @@ -45,8 +45,8 @@ def class_method_map(classes) class Output MARGIN = " " - def initialize(grep: nil, use_colorize: true) - @use_colorize = use_colorize + def initialize(grep: nil, colorable: true) + @colorable = colorable @grep = grep @line_width = screen_width - MARGIN.length # right padding end @@ -57,7 +57,7 @@ def dump(name, strs) return if strs.empty? # Attempt a single line - print "#{Color.colorize(name, [:BOLD, :BLUE], colorable: Color.colorable? && @use_colorize)}: " + print "#{Color.colorize(name, [:BOLD, :BLUE], colorable: @colorable)}: " 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 881a73672..17ff2f9b7 100644 --- a/lib/irb/cmd/nop.rb +++ b/lib/irb/cmd/nop.rb @@ -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 diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb index ba3d9d19a..7e790c3c9 100644 --- a/lib/irb/cmd/show_source.rb +++ b/lib/irb/cmd/show_source.rb @@ -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), colorable: Color.colorable? && irb_context.use_colorize) + code = IRB::Color.colorize_code(File.read(source.file), colorable: colorable) puts code.lines[(source.first_line - 1)...source.last_line].join puts end @@ -78,7 +78,7 @@ def find_end(file, first_line) end def bold(str) - Color.colorize(str, [:BOLD], colorable: Color.colorable? && irb_context.use_colorize) + Color.colorize(str, [:BOLD], colorable: colorable) end Source = Struct.new(