2015-11-10 14:48:14 +03:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 16:27:48 +03:00
|
|
|
|
2011-10-11 01:10:02 +04:00
|
|
|
require 'prettyprint'
|
|
|
|
|
2013-04-28 18:19:18 +04:00
|
|
|
##
|
|
|
|
# A pretty-printer for Ruby objects.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
##
|
|
|
|
# == What PP Does
|
|
|
|
#
|
|
|
|
# Standard output by #p returns this:
|
2005-03-06 05:55:16 +03:00
|
|
|
# #<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
# Pretty-printed output returns this:
|
2005-03-06 05:55:16 +03:00
|
|
|
# #<PP:0x81fedf0
|
|
|
|
# @buffer=[],
|
|
|
|
# @buffer_width=0,
|
|
|
|
# @genspace=#<Proc:0x81feda0>,
|
|
|
|
# @group_queue=
|
|
|
|
# #<PrettyPrint::GroupQueue:0x81fed3c
|
|
|
|
# @queue=
|
|
|
|
# [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
|
|
|
# []]>,
|
|
|
|
# @group_stack=
|
|
|
|
# [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
|
|
|
# @indent=0,
|
|
|
|
# @maxwidth=79,
|
|
|
|
# @newline="\n",
|
|
|
|
# @output=#<IO:0x8114ee4>,
|
|
|
|
# @output_width=2>
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
##
|
2013-02-10 04:10:11 +04:00
|
|
|
# == Usage
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2011-12-04 07:12:54 +04:00
|
|
|
# pp(obj) #=> obj
|
2013-04-28 18:19:18 +04:00
|
|
|
# pp obj #=> obj
|
2011-12-04 07:12:54 +04:00
|
|
|
# pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
|
|
|
|
# pp() #=> nil
|
2005-03-06 05:55:16 +03:00
|
|
|
#
|
2013-02-10 04:10:11 +04:00
|
|
|
# Output <tt>obj(s)</tt> to <tt>$></tt> in pretty printed format.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-02-10 04:10:11 +04:00
|
|
|
# It returns <tt>obj(s)</tt>.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
##
|
|
|
|
# == Output Customization
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
# To define a customized pretty printing function for your classes,
|
|
|
|
# redefine method <code>#pretty_print(pp)</code> in the class.
|
2023-07-19 11:38:07 +03:00
|
|
|
# Note that <code>require 'pp'</code> is needed before redefining <code>#pretty_print(pp)</code>.
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
# <code>#pretty_print</code> takes the +pp+ argument, which is an instance of the PP class.
|
|
|
|
# The method uses #text, #breakable, #nest, #group and #pp to print the
|
2013-02-10 04:10:11 +04:00
|
|
|
# object.
|
2005-03-06 05:55:16 +03:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
##
|
|
|
|
# == Pretty-Print JSON
|
|
|
|
#
|
|
|
|
# To pretty-print JSON refer to JSON#pretty_generate.
|
|
|
|
#
|
|
|
|
##
|
|
|
|
# == Author
|
2013-02-10 04:10:11 +04:00
|
|
|
# Tanaka Akira <akr@fsij.org>
|
2013-04-28 18:19:18 +04:00
|
|
|
|
2001-12-24 20:38:33 +03:00
|
|
|
class PP < PrettyPrint
|
2023-03-30 07:23:44 +03:00
|
|
|
|
2023-11-07 03:59:53 +03:00
|
|
|
VERSION = "0.5.0"
|
2023-03-30 07:23:44 +03:00
|
|
|
|
2021-12-23 10:09:02 +03:00
|
|
|
# Returns the usable width for +out+.
|
|
|
|
# As the width of +out+:
|
|
|
|
# 1. If +out+ is assigned to a tty device, its width is used.
|
|
|
|
# 2. Otherwise, or it could not get the value, the +COLUMN+
|
|
|
|
# environment variable is assumed to be set to the width.
|
|
|
|
# 3. If +COLUMN+ is not set to a non-zero number, 80 is assumed.
|
|
|
|
#
|
|
|
|
# And finally, returns the above width value - 1.
|
|
|
|
# * This -1 is for Windows command prompt, which moves the cursor to
|
|
|
|
# the next line if it reaches the last column.
|
2021-11-30 05:42:06 +03:00
|
|
|
def PP.width_for(out)
|
|
|
|
begin
|
|
|
|
require 'io/console'
|
|
|
|
_, width = out.winsize
|
2021-11-30 07:46:08 +03:00
|
|
|
rescue LoadError, NoMethodError, SystemCallError
|
2021-11-30 05:42:06 +03:00
|
|
|
end
|
|
|
|
(width || ENV['COLUMNS']&.to_i&.nonzero? || 80) - 1
|
|
|
|
end
|
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Outputs +obj+ to +out+ in pretty printed format of
|
|
|
|
# +width+ columns in width.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-04-28 18:19:18 +04:00
|
|
|
# If +out+ is omitted, <code>$></code> is assumed.
|
2021-12-23 10:09:02 +03:00
|
|
|
# If +width+ is omitted, the width of +out+ is assumed (see
|
|
|
|
# width_for).
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# PP.pp returns +out+.
|
2021-11-30 05:42:06 +03:00
|
|
|
def PP.pp(obj, out=$>, width=width_for(out))
|
2021-09-05 15:51:14 +03:00
|
|
|
q = new(out, width)
|
2003-12-16 15:22:15 +03:00
|
|
|
q.guard_inspect_key {q.pp obj}
|
|
|
|
q.flush
|
|
|
|
#$pp = q
|
2001-12-24 20:38:33 +03:00
|
|
|
out << "\n"
|
|
|
|
end
|
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Outputs +obj+ to +out+ like PP.pp but with no indent and
|
|
|
|
# newline.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# PP.singleline_pp returns +out+.
|
2002-12-02 19:16:05 +03:00
|
|
|
def PP.singleline_pp(obj, out=$>)
|
2003-12-16 15:22:15 +03:00
|
|
|
q = SingleLine.new(out)
|
|
|
|
q.guard_inspect_key {q.pp obj}
|
|
|
|
q.flush
|
2002-12-02 19:16:05 +03:00
|
|
|
out
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2006-05-13 11:35:50 +04:00
|
|
|
# :stopdoc:
|
|
|
|
def PP.mcall(obj, mod, meth, *args, &block)
|
2019-08-30 05:11:55 +03:00
|
|
|
mod.instance_method(meth).bind_call(obj, *args, &block)
|
2006-05-13 11:35:50 +04:00
|
|
|
end
|
|
|
|
# :startdoc:
|
|
|
|
|
2020-12-22 11:07:23 +03:00
|
|
|
if defined? ::Ractor
|
|
|
|
class << self
|
|
|
|
# Returns the sharing detection flag as a boolean value.
|
|
|
|
# It is false (nil) by default.
|
|
|
|
def sharing_detection
|
|
|
|
Ractor.current[:pp_sharing_detection]
|
|
|
|
end
|
2020-12-23 05:13:50 +03:00
|
|
|
# Sets the sharing detection flag to b.
|
2020-12-22 11:07:23 +03:00
|
|
|
def sharing_detection=(b)
|
|
|
|
Ractor.current[:pp_sharing_detection] = b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@sharing_detection = false
|
|
|
|
class << self
|
|
|
|
# Returns the sharing detection flag as a boolean value.
|
|
|
|
# It is false by default.
|
|
|
|
attr_accessor :sharing_detection
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2002-12-02 19:16:05 +03:00
|
|
|
module PPMethods
|
2013-02-10 04:10:11 +04:00
|
|
|
|
|
|
|
# Yields to a block
|
|
|
|
# and preserves the previous set of objects being printed.
|
2002-12-02 19:16:05 +03:00
|
|
|
def guard_inspect_key
|
2005-03-09 17:56:16 +03:00
|
|
|
if Thread.current[:__recursive_key__] == nil
|
2019-09-25 06:59:12 +03:00
|
|
|
Thread.current[:__recursive_key__] = {}.compare_by_identity
|
2005-03-09 17:56:16 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
if Thread.current[:__recursive_key__][:inspect] == nil
|
2019-09-25 06:59:12 +03:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2005-03-09 17:56:16 +03:00
|
|
|
save = Thread.current[:__recursive_key__][:inspect]
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2002-12-02 19:16:05 +03:00
|
|
|
begin
|
2019-09-25 06:59:12 +03:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
|
2002-12-02 19:16:05 +03:00
|
|
|
yield
|
|
|
|
ensure
|
2005-03-09 17:56:16 +03:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = save
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Check whether the object_id +id+ is in the current buffer of objects
|
|
|
|
# to be pretty printed. Used to break cycles in chains of objects to be
|
|
|
|
# pretty printed.
|
2005-03-07 05:05:08 +03:00
|
|
|
def check_inspect_key(id)
|
2005-03-09 17:56:16 +03:00
|
|
|
Thread.current[:__recursive_key__] &&
|
|
|
|
Thread.current[:__recursive_key__][:inspect] &&
|
|
|
|
Thread.current[:__recursive_key__][:inspect].include?(id)
|
2005-03-07 05:05:08 +03:00
|
|
|
end
|
2013-02-10 04:10:11 +04:00
|
|
|
|
|
|
|
# Adds the object_id +id+ to the set of objects being pretty printed, so
|
|
|
|
# as to not repeat objects.
|
2005-03-07 05:05:08 +03:00
|
|
|
def push_inspect_key(id)
|
2007-12-07 09:45:28 +03:00
|
|
|
Thread.current[:__recursive_key__][:inspect][id] = true
|
2005-03-07 05:05:08 +03:00
|
|
|
end
|
2013-02-10 04:10:11 +04:00
|
|
|
|
|
|
|
# Removes an object from the set of objects being pretty printed.
|
2007-12-07 09:45:28 +03:00
|
|
|
def pop_inspect_key(id)
|
|
|
|
Thread.current[:__recursive_key__][:inspect].delete id
|
2005-03-07 05:05:08 +03:00
|
|
|
end
|
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Adds +obj+ to the pretty printing buffer
|
|
|
|
# using Object#pretty_print or Object#pretty_print_cycle.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# Object#pretty_print_cycle is used when +obj+ is already
|
|
|
|
# printed, a.k.a the object reference chain has a cycle.
|
2002-12-02 19:16:05 +03:00
|
|
|
def pp(obj)
|
2017-01-22 04:50:08 +03:00
|
|
|
# If obj is a Delegator then use the object being delegated to for cycle
|
|
|
|
# detection
|
|
|
|
obj = obj.__getobj__ if defined?(::Delegator) and obj.is_a?(::Delegator)
|
|
|
|
|
2019-11-04 07:51:30 +03:00
|
|
|
if check_inspect_key(obj)
|
2002-12-02 19:16:05 +03:00
|
|
|
group {obj.pretty_print_cycle self}
|
|
|
|
return
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2002-12-02 19:16:05 +03:00
|
|
|
begin
|
2019-11-04 07:51:30 +03:00
|
|
|
push_inspect_key(obj)
|
2002-12-02 19:16:05 +03:00
|
|
|
group {obj.pretty_print self}
|
|
|
|
ensure
|
2019-11-04 07:51:30 +03:00
|
|
|
pop_inspect_key(obj) unless PP.sharing_detection
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# A convenience method which is same as follows:
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# group(1, '#<' + obj.class.name, '>') { ... }
|
|
|
|
def object_group(obj, &block) # :yield:
|
2002-12-02 19:16:05 +03:00
|
|
|
group(1, '#<' + obj.class.name, '>', &block)
|
|
|
|
end
|
2002-06-27 16:01:07 +04:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# A convenience method, like object_group, but also reformats the Object's
|
|
|
|
# object_id.
|
2002-12-02 19:16:05 +03:00
|
|
|
def object_address_group(obj, &block)
|
2019-08-30 05:11:55 +03:00
|
|
|
str = Kernel.instance_method(:to_s).bind_call(obj)
|
2013-10-22 13:29:53 +04:00
|
|
|
str.chomp!('>')
|
|
|
|
group(1, str, '>', &block)
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2002-06-28 12:55:10 +04:00
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# A convenience method which is same as follows:
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# text ','
|
|
|
|
# breakable
|
2002-12-02 19:16:05 +03:00
|
|
|
def comma_breakable
|
|
|
|
text ','
|
|
|
|
breakable
|
|
|
|
end
|
2002-06-27 16:01:07 +04:00
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Adds a separated list.
|
|
|
|
# The list is separated by comma with breakable space, by default.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# #seplist iterates the +list+ using +iter_method+.
|
|
|
|
# It yields each object to the block given for #seplist.
|
|
|
|
# The procedure +separator_proc+ is called between each yields.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# If the iteration is zero times, +separator_proc+ is not called at all.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# If +separator_proc+ is nil or not given,
|
|
|
|
# +lambda { comma_breakable }+ is used.
|
|
|
|
# If +iter_method+ is not given, :each is used.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# For example, following 3 code fragments has similar effect.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# q.seplist([1,2,3]) {|v| xxx v }
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2007-11-11 15:48:39 +03:00
|
|
|
# q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# xxx 1
|
|
|
|
# q.comma_breakable
|
|
|
|
# xxx 2
|
|
|
|
# q.comma_breakable
|
|
|
|
# xxx 3
|
|
|
|
def seplist(list, sep=nil, iter_method=:each) # :yield: element
|
2004-03-27 04:46:05 +03:00
|
|
|
sep ||= lambda { comma_breakable }
|
|
|
|
first = true
|
2007-06-05 09:48:45 +04:00
|
|
|
list.__send__(iter_method) {|*v|
|
2004-03-27 04:46:05 +03:00
|
|
|
if first
|
|
|
|
first = false
|
|
|
|
else
|
|
|
|
sep.call
|
|
|
|
end
|
2020-12-28 07:37:11 +03:00
|
|
|
RUBY_VERSION >= "3.0" ? yield(*v, **{}) : yield(*v)
|
2004-03-27 04:46:05 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# A present standard failsafe for pretty printing any given Object
|
2002-12-02 19:16:05 +03:00
|
|
|
def pp_object(obj)
|
|
|
|
object_address_group(obj) {
|
2004-02-05 17:59:46 +03:00
|
|
|
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
2002-12-02 19:16:05 +03:00
|
|
|
breakable
|
2004-02-05 17:59:46 +03:00
|
|
|
v = v.to_s if Symbol === v
|
2002-12-02 19:16:05 +03:00
|
|
|
text v
|
|
|
|
text '='
|
|
|
|
group(1) {
|
|
|
|
breakable ''
|
|
|
|
pp(obj.instance_eval(v))
|
|
|
|
}
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2001-12-24 20:38:33 +03:00
|
|
|
}
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# A pretty print for a Hash
|
2002-12-02 19:16:05 +03:00
|
|
|
def pp_hash(obj)
|
|
|
|
group(1, '{', '}') {
|
2007-10-14 06:14:16 +04:00
|
|
|
seplist(obj, nil, :each_pair) {|k, v|
|
2002-12-02 19:16:05 +03:00
|
|
|
group {
|
2021-09-05 16:19:48 +03:00
|
|
|
pp_hash_pair k, v
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
|
|
|
}
|
2001-12-24 20:38:33 +03:00
|
|
|
}
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
2021-09-05 16:19:48 +03:00
|
|
|
|
|
|
|
# A pretty print for a pair of Hash
|
|
|
|
def pp_hash_pair(k, v)
|
|
|
|
pp k
|
|
|
|
text '=>'
|
|
|
|
group(1) {
|
|
|
|
breakable ''
|
|
|
|
pp v
|
|
|
|
}
|
|
|
|
end
|
2002-12-02 19:16:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
include PPMethods
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class SingleLine < PrettyPrint::SingleLine # :nodoc:
|
2002-12-02 19:16:05 +03:00
|
|
|
include PPMethods
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-05-20 06:52:29 +04:00
|
|
|
module ObjectMixin # :nodoc:
|
2001-12-24 20:38:33 +03:00
|
|
|
# 1. specific pretty_print
|
2003-05-11 00:53:58 +04:00
|
|
|
# 2. specific inspect
|
2012-08-15 15:50:01 +04:00
|
|
|
# 3. generic pretty_print
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# A default pretty printing method for general objects.
|
|
|
|
# It calls #pretty_print_instance_variables to list instance variables.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# If +self+ has a customized (redefined) #inspect method,
|
|
|
|
# the result of self.inspect is used but it obviously has no
|
|
|
|
# line break hints.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# This module provides predefined #pretty_print methods for some of
|
|
|
|
# the most commonly used built-in classes for convenience.
|
2003-12-16 15:22:15 +03:00
|
|
|
def pretty_print(q)
|
2019-08-30 05:11:55 +03:00
|
|
|
umethod_method = Object.instance_method(:method)
|
2009-09-27 08:08:31 +04:00
|
|
|
begin
|
2019-08-30 05:11:55 +03:00
|
|
|
inspect_method = umethod_method.bind_call(self, :inspect)
|
2009-09-27 08:08:31 +04:00
|
|
|
rescue NameError
|
|
|
|
end
|
2017-04-04 15:54:42 +03:00
|
|
|
if inspect_method && inspect_method.owner != Kernel
|
2009-09-27 08:08:31 +04:00
|
|
|
q.text self.inspect
|
|
|
|
elsif !inspect_method && self.respond_to?(:inspect)
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text self.inspect
|
2001-12-24 20:38:33 +03:00
|
|
|
else
|
2003-12-16 15:22:15 +03:00
|
|
|
q.pp_object(self)
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# A default pretty printing method for general objects that are
|
|
|
|
# detected as part of a cycle.
|
2003-12-16 15:22:15 +03:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.object_address_group(self) {
|
|
|
|
q.breakable
|
|
|
|
q.text '...'
|
2002-06-28 12:55:10 +04:00
|
|
|
}
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
2002-06-27 16:01:07 +04:00
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Returns a sorted array of instance variable names.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# This method should return an array of names of instance variables as symbols or strings as:
|
|
|
|
# +[:@a, :@b]+.
|
2002-06-27 16:01:07 +04:00
|
|
|
def pretty_print_instance_variables
|
|
|
|
instance_variables.sort
|
|
|
|
end
|
2003-01-20 20:58:24 +03:00
|
|
|
|
2005-03-06 05:55:16 +03:00
|
|
|
# Is #inspect implementation using #pretty_print.
|
|
|
|
# If you implement #pretty_print, it can be used as follows.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2005-03-06 05:55:16 +03:00
|
|
|
# alias inspect pretty_print_inspect
|
|
|
|
#
|
|
|
|
# However, doing this requires that every class that #inspect is called on
|
|
|
|
# implement #pretty_print, or a RuntimeError will be raised.
|
2003-01-20 20:58:24 +03:00
|
|
|
def pretty_print_inspect
|
2019-08-30 05:11:55 +03:00
|
|
|
if Object.instance_method(:method).bind_call(self, :pretty_print).owner == PP::ObjectMixin
|
2006-07-09 03:17:53 +04:00
|
|
|
raise "pretty_print is not overridden for #{self.class}"
|
2003-01-20 20:58:24 +03:00
|
|
|
end
|
2015-10-05 16:27:48 +03:00
|
|
|
PP.singleline_pp(self, ''.dup)
|
2003-01-20 20:58:24 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class Array # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2003-12-16 15:22:15 +03:00
|
|
|
q.group(1, '[', ']') {
|
2004-02-05 17:59:46 +03:00
|
|
|
q.seplist(self) {|v|
|
2003-12-16 15:22:15 +03:00
|
|
|
q.pp v
|
2001-12-24 20:38:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
def pretty_print_cycle(q) # :nodoc:
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text(empty? ? '[]' : '[...]')
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class Hash # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2003-12-16 15:22:15 +03:00
|
|
|
q.pp_hash self
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
def pretty_print_cycle(q) # :nodoc:
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text(empty? ? '{}' : '{...}')
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class << ENV # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2007-10-14 06:14:16 +04:00
|
|
|
h = {}
|
|
|
|
ENV.keys.sort.each {|k|
|
|
|
|
h[k] = ENV[k]
|
|
|
|
}
|
|
|
|
q.pp_hash h
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class Struct # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2009-04-28 02:13:50 +04:00
|
|
|
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
2006-05-13 11:35:50 +04:00
|
|
|
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
2003-12-16 15:22:15 +03:00
|
|
|
q.breakable
|
|
|
|
q.text member.to_s
|
|
|
|
q.text '='
|
|
|
|
q.group(1) {
|
|
|
|
q.breakable ''
|
|
|
|
q.pp self[member]
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2001-12-24 20:38:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
def pretty_print_cycle(q) # :nodoc:
|
2006-05-13 11:35:50 +04:00
|
|
|
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-10 06:30:35 +03:00
|
|
|
class Data # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2023-11-30 19:10:40 +03:00
|
|
|
class_name = PP.mcall(self, Kernel, :class).name
|
|
|
|
class_name = " #{class_name}" if class_name
|
|
|
|
q.group(1, "#<data#{class_name}", '>') {
|
2023-11-30 19:04:39 +03:00
|
|
|
q.seplist(PP.mcall(self, Kernel, :class).members, lambda { q.text "," }) {|member|
|
2022-10-10 06:30:35 +03:00
|
|
|
q.breakable
|
|
|
|
q.text member.to_s
|
|
|
|
q.text '='
|
|
|
|
q.group(1) {
|
|
|
|
q.breakable ''
|
|
|
|
q.pp public_send(member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_print_cycle(q) # :nodoc:
|
|
|
|
q.text sprintf("#<data %s:...>", PP.mcall(self, Kernel, :class).name)
|
|
|
|
end
|
2023-11-30 19:03:59 +03:00
|
|
|
end if defined?(Data.define)
|
2022-10-10 06:30:35 +03:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class Range # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2023-12-21 04:55:50 +03:00
|
|
|
q.pp self.begin if self.begin
|
2003-12-16 15:22:15 +03:00
|
|
|
q.breakable ''
|
|
|
|
q.text(self.exclude_end? ? '...' : '..')
|
|
|
|
q.breakable ''
|
2018-12-03 04:39:45 +03:00
|
|
|
q.pp self.end if self.end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-10 21:23:33 +03:00
|
|
|
class String # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2016-10-11 10:44:58 +03:00
|
|
|
lines = self.lines
|
|
|
|
if lines.size > 1
|
|
|
|
q.group(0, '', '') do
|
|
|
|
q.seplist(lines, lambda { q.text ' +'; q.breakable }) do |v|
|
|
|
|
q.pp v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
q.text inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class File < IO # :nodoc:
|
|
|
|
class Stat # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2021-09-28 17:37:07 +03:00
|
|
|
require 'etc'
|
2003-12-16 15:22:15 +03:00
|
|
|
q.object_group(self) {
|
|
|
|
q.breakable
|
|
|
|
q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
|
|
|
|
q.text "ino="; q.pp self.ino; q.comma_breakable
|
|
|
|
q.group {
|
2002-07-03 06:49:42 +04:00
|
|
|
m = self.mode
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text sprintf("mode=0%o", m)
|
|
|
|
q.breakable
|
|
|
|
q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
|
2002-07-03 06:49:42 +04:00
|
|
|
self.ftype,
|
|
|
|
(m & 0400 == 0 ? ?- : ?r),
|
|
|
|
(m & 0200 == 0 ? ?- : ?w),
|
|
|
|
(m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
|
|
|
|
(m & 04000 == 0 ? ?x : ?s)),
|
|
|
|
(m & 0040 == 0 ? ?- : ?r),
|
|
|
|
(m & 0020 == 0 ? ?- : ?w),
|
|
|
|
(m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
|
|
|
|
(m & 02000 == 0 ? ?x : ?s)),
|
|
|
|
(m & 0004 == 0 ? ?- : ?r),
|
|
|
|
(m & 0002 == 0 ? ?- : ?w),
|
|
|
|
(m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
|
|
|
|
(m & 01000 == 0 ? ?x : ?t)))
|
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.text "nlink="; q.pp self.nlink; q.comma_breakable
|
|
|
|
q.group {
|
|
|
|
q.text "uid="; q.pp self.uid
|
2002-07-03 06:49:42 +04:00
|
|
|
begin
|
2005-03-06 05:41:15 +03:00
|
|
|
pw = Etc.getpwuid(self.uid)
|
2002-07-03 06:49:42 +04:00
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2005-03-06 05:41:15 +03:00
|
|
|
if pw
|
|
|
|
q.breakable; q.text "(#{pw.name})"
|
|
|
|
end
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
|
|
|
q.text "gid="; q.pp self.gid
|
2002-07-03 06:49:42 +04:00
|
|
|
begin
|
2005-03-06 05:41:15 +03:00
|
|
|
gr = Etc.getgrgid(self.gid)
|
2002-07-03 06:49:42 +04:00
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2005-03-06 05:41:15 +03:00
|
|
|
if gr
|
|
|
|
q.breakable; q.text "(#{gr.name})"
|
|
|
|
end
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
|
|
|
q.text sprintf("rdev=0x%x", self.rdev)
|
2014-12-05 22:35:19 +03:00
|
|
|
if self.rdev_major && self.rdev_minor
|
|
|
|
q.breakable
|
|
|
|
q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
|
|
|
|
end
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.text "size="; q.pp self.size; q.comma_breakable
|
|
|
|
q.text "blksize="; q.pp self.blksize; q.comma_breakable
|
|
|
|
q.text "blocks="; q.pp self.blocks; q.comma_breakable
|
|
|
|
q.group {
|
2002-07-03 06:49:42 +04:00
|
|
|
t = self.atime
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text "atime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
2002-07-03 06:49:42 +04:00
|
|
|
t = self.mtime
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text "mtime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
2002-07-03 06:49:42 +04:00
|
|
|
t = self.ctime
|
2003-12-16 15:22:15 +03:00
|
|
|
q.text "ctime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-03 06:49:42 +04:00
|
|
|
}
|
2001-12-24 20:38:33 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class MatchData # :nodoc:
|
|
|
|
def pretty_print(q) # :nodoc:
|
2007-12-10 00:44:19 +03:00
|
|
|
nc = []
|
|
|
|
self.regexp.named_captures.each {|name, indexes|
|
|
|
|
indexes.each {|i| nc[i] = name }
|
|
|
|
}
|
2003-12-16 15:22:15 +03:00
|
|
|
q.object_group(self) {
|
|
|
|
q.breakable
|
2007-12-10 00:44:19 +03:00
|
|
|
q.seplist(0...self.size, lambda { q.breakable }) {|i|
|
|
|
|
if i == 0
|
|
|
|
q.pp self[i]
|
|
|
|
else
|
|
|
|
if nc[i]
|
|
|
|
q.text nc[i]
|
|
|
|
else
|
|
|
|
q.pp i
|
|
|
|
end
|
|
|
|
q.text ':'
|
|
|
|
q.pp self[i]
|
|
|
|
end
|
2003-03-07 16:02:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 07:57:26 +03:00
|
|
|
if defined?(RubyVM::AbstractSyntaxTree)
|
|
|
|
class RubyVM::AbstractSyntaxTree::Node
|
|
|
|
def pretty_print_children(q, names = [])
|
|
|
|
children.zip(names) do |c, n|
|
|
|
|
if n
|
|
|
|
q.breakable
|
|
|
|
q.text "#{n}:"
|
|
|
|
end
|
|
|
|
q.group(2) do
|
|
|
|
q.breakable
|
|
|
|
q.pp c
|
|
|
|
end
|
2018-12-03 03:24:38 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 07:57:26 +03:00
|
|
|
def pretty_print(q)
|
|
|
|
q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
|
|
|
|
case type
|
|
|
|
when :SCOPE
|
|
|
|
pretty_print_children(q, %w"tbl args body")
|
|
|
|
when :ARGS
|
|
|
|
pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
|
|
|
|
when :DEFN
|
|
|
|
pretty_print_children(q, %w[mid body])
|
|
|
|
when :ARYPTN
|
|
|
|
pretty_print_children(q, %w[const pre rest post])
|
|
|
|
when :HSHPTN
|
|
|
|
pretty_print_children(q, %w[const kw kwrest])
|
|
|
|
else
|
|
|
|
pretty_print_children(q)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2018-12-03 03:24:38 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
class Object < BasicObject # :nodoc:
|
2001-12-24 20:38:33 +03:00
|
|
|
include PP::ObjectMixin
|
|
|
|
end
|
|
|
|
|
2003-01-20 22:54:56 +03:00
|
|
|
[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
|
|
|
|
c.class_eval {
|
2003-12-16 15:22:15 +03:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.text inspect
|
2003-01-20 22:54:56 +03:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-01 19:57:47 +03:00
|
|
|
[Numeric, FalseClass, TrueClass, Module].each {|c|
|
|
|
|
c.class_eval {
|
|
|
|
def pretty_print(q)
|
|
|
|
q.text inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2017-12-01 13:48:29 +03:00
|
|
|
|
|
|
|
module Kernel
|
|
|
|
# Returns a pretty printed object as a string.
|
|
|
|
#
|
|
|
|
# See the PP module for more information.
|
|
|
|
def pretty_inspect
|
|
|
|
PP.pp(self, ''.dup)
|
|
|
|
end
|
|
|
|
|
|
|
|
# prints arguments in pretty form.
|
|
|
|
#
|
|
|
|
# pp returns argument(s).
|
|
|
|
def pp(*objs)
|
|
|
|
objs.each {|obj|
|
|
|
|
PP.pp(obj)
|
|
|
|
}
|
|
|
|
objs.size <= 1 ? objs.first : objs
|
|
|
|
end
|
|
|
|
module_function :pp
|
|
|
|
end
|