[前][次][番号順一覧][スレッド一覧]

ruby-changes:64653

From: Takashi <ko1@a...>
Date: Tue, 29 Dec 2020 16:01:32 +0900 (JST)
Subject: [ruby-changes:64653] c715fb46c2 (master): [ruby/irb] Enhance colored inspect output

https://git.ruby-lang.org/ruby.git/commit/?id=c715fb46c2

From c715fb46c2d8eab48323a6008c5dbca550ebe2e0 Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Mon, 28 Dec 2020 22:16:08 -0800
Subject: [ruby/irb] Enhance colored inspect output

https://github.com/ruby/irb/commit/dffcdb5269

diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index 0f49291..56f5c4c 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -107,7 +107,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L107
       # If `complete` is false (code is incomplete), this does not warn compile_error.
       # This option is needed to avoid warning a user when the compile_error is happening
       # because the input is not wrong but just incomplete.
-      def colorize_code(code, complete: true)
+      def colorize_code(code, complete: true, ignore_error: false)
         return code unless colorable?
 
         symbol_state = SymbolState.new
@@ -118,7 +118,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L118
           in_symbol = symbol_state.scan_token(token)
           str.each_line do |line|
             line = Reline::Unicode.escape_for_print(line)
-            if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
+            if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol, ignore_error: ignore_error)
               colored << seq.map { |s| "\e[#{s}m" }.join('')
               colored << line.sub(/\Z/, clear)
             else
@@ -183,9 +183,9 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L183
         $VERBOSE = verbose
       end
 
-      def dispatch_seq(token, expr, str, in_symbol:)
+      def dispatch_seq(token, expr, str, in_symbol:, ignore_error:)
         if token == :on_parse_error or token == :compile_error
-          TOKEN_SEQ_EXPRS[token][0]
+          ignore_error ? nil : TOKEN_SEQ_EXPRS[token][0]
         elsif in_symbol
           [YELLOW]
         elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
diff --git a/lib/irb/color_printer.rb b/lib/irb/color_printer.rb
new file mode 100644
index 0000000..187c337
--- /dev/null
+++ b/lib/irb/color_printer.rb
@@ -0,0 +1,22 @@ https://github.com/ruby/ruby/blob/trunk/lib/irb/color_printer.rb#L1
+# frozen_string_literal: true
+require 'pp'
+
+module IRB
+  class ColorPrinter < ::PP
+    def self.pp(obj, out = $>, width = 79)
+      q = ColorPrinter.new(out, width)
+      q.guard_inspect_key {q.pp obj}
+      q.flush
+      out
+    end
+
+    def text(str, width = str.length)
+      case str
+      when /\A#</, '=', '>'
+        super(Color.colorize(str, [:GREEN]), width)
+      else
+        super(Color.colorize_code(str, ignore_error: true), width)
+      end
+    end
+  end
+end
diff --git a/lib/irb/inspector.rb b/lib/irb/inspector.rb
index 671b32b..66837f1 100644
--- a/lib/irb/inspector.rb
+++ b/lib/irb/inspector.rb
@@ -104,7 +104,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/inspector.rb#L104
   end
 
   Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
-  Inspector.def_inspector([true, :p, :inspect]){|v|
+  Inspector.def_inspector([:p, :inspect]){|v|
     begin
       result = v.inspect
       if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
@@ -116,12 +116,12 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/inspector.rb#L116
       ''
     end
   }
-  Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
-    result = v.pretty_inspect.chomp
-    if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
-      result = Color.colorize_code(result)
+  Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
+    if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
+      IRB::ColorPrinter.pp(v, '')
+    else
+      v.pretty_inspect.chomp
     end
-    result
   }
   Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
     begin
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]