From f0b6650385ad577c8679764e0fa23c6b25814149 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 1 Feb 2025 14:56:28 +0900 Subject: [PATCH 1/3] Optional keywords support --- lib/pp.rb | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index e790f49..9876dc1 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -93,23 +93,16 @@ def PP.width_for(out) # width_for). # # PP.pp returns +out+. - def PP.pp(obj, out=$>, width=width_for(out)) - q = new(out, width) - q.guard_inspect_key {q.pp obj} - q.flush - #$pp = q - out << "\n" + def PP.pp(obj, *args, **options) + new(*args, **options).print(obj) end # Outputs +obj+ to +out+ like PP.pp but with no indent and # newline. # # PP.singleline_pp returns +out+. - def PP.singleline_pp(obj, out=$>) - q = SingleLine.new(out) - q.guard_inspect_key {q.pp obj} - q.flush - out + def PP.singleline_pp(obj, *args, **options) + SingleLine.new(*args, **options).print(obj) end # :stopdoc: @@ -141,6 +134,19 @@ class << self # Module that defines helper methods for pretty_print. module PPMethods + # Initialize +out+ option + def initialize(_out=$>, width=nil, out: _out) + super(out, width) + end + + # Pretty print single object + def print(obj) + guard_inspect_key {pp obj} + flush + text(newline) if newline + output + end + # Yields to a block # and preserves the previous set of objects being printed. @@ -341,8 +347,17 @@ def pp_hash_pair(k, v) include PPMethods + # Create pretty print object. + def initialize(_out=$>, _width=nil, out: _out, + width: _width || self.class.width_for(out), **options) + super(out, width, **options) + end + class SingleLine < PrettyPrint::SingleLine # :nodoc: include PPMethods + attr_reader :output + def newline + end end module ObjectMixin # :nodoc: @@ -728,10 +743,17 @@ def pretty_inspect # prints arguments in pretty form. # # +#pp+ returns argument(s). - def pp(*objs) - objs.each {|obj| - PP.pp(obj) - } + def pp(*objs, **options) + if objs.empty? + unless Hash.ruby2_keywords_hash?(options) + # TODO: Remove this block when dropping ruby 2.7 support + PP.new.print(options) + return options + end + return + end + q = PP.new(**options) + objs.each {|obj| q.print(obj)} objs.size <= 1 ? objs.first : objs end module_function :pp From b8c5349418a1620b27ead029fec96afc845ecb53 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 16:33:36 +0900 Subject: [PATCH 2/3] Add `highlight:` --- lib/pp.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 9876dc1..d188105 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -134,9 +134,11 @@ class << self # Module that defines helper methods for pretty_print. module PPMethods - # Initialize +out+ option - def initialize(_out=$>, width=nil, out: _out) + # Initialize +out+ and +highlight+ options + def initialize(_out=$>, width=nil, out: _out, + highlight: ([:keys] if out.respond_to?(:tty?) and out.tty?)) super(out, width) + @highlight = highlight end # Pretty print single object @@ -147,6 +149,12 @@ def print(obj) output end + # Returns strings to turn highlight on and off. + def highlight?(e) + if @highlight&.include?(e) + return "\e[1;4m", "\e[22;24m" + end + end # Yields to a block # and preserves the previous set of objects being printed. @@ -322,7 +330,8 @@ def pp_hash_pair(k, v) if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z]) k = k.to_s.inspect end - text "#{k}:" + h, e = highlight?(:keys) + text "#{h}#{k}#{e}:", k.length+1 else pp k text ' ' From 43fab495bf94bb059caa345f36707debdf5a1f39 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 17:20:56 +0900 Subject: [PATCH 3/3] Pass also the key value to `highlight?` --- lib/pp.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index d188105..194cfab 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -150,7 +150,7 @@ def print(obj) end # Returns strings to turn highlight on and off. - def highlight?(e) + def highlight?(e, k = nil) if @highlight&.include?(e) return "\e[1;4m", "\e[22;24m" end @@ -330,7 +330,7 @@ def pp_hash_pair(k, v) if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z]) k = k.to_s.inspect end - h, e = highlight?(:keys) + h, e = highlight?(:keys, k) text "#{h}#{k}#{e}:", k.length+1 else pp k