2005-03-05 21:55:16 -05:00
|
|
|
# == Pretty-printer for Ruby objects.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# = Which seems better?
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# non-pretty-printed output by #p is:
|
|
|
|
# #<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 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# pretty-printed output by #pp is:
|
|
|
|
# #<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 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# I like the latter. If you do too, this library is for you.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# = Usage
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# pp(obj)
|
|
|
|
#
|
|
|
|
# output +obj+ to +$>+ in pretty printed format.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# It returns +nil+.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# = Output Customization
|
|
|
|
# To define your customized pretty printing function for your classes,
|
|
|
|
# redefine a method #pretty_print(+pp+) in the class.
|
|
|
|
# It takes an argument +pp+ which is an instance of the class PP.
|
|
|
|
# The method should use PP#text, PP#breakable, PP#nest, PP#group and
|
|
|
|
# PP#pp to print the object.
|
|
|
|
#
|
|
|
|
# = Author
|
|
|
|
# Tanaka Akira <akr@m17n.org>
|
2001-12-24 12:38:33 -05:00
|
|
|
|
|
|
|
require 'prettyprint'
|
|
|
|
|
|
|
|
module Kernel
|
2006-06-16 13:05:22 -04:00
|
|
|
# returns a pretty printed object as a string.
|
|
|
|
def pretty_inspect
|
|
|
|
PP.pp(self, '')
|
|
|
|
end
|
|
|
|
|
2001-12-24 12:38:33 -05:00
|
|
|
private
|
2006-06-16 13:05:22 -04:00
|
|
|
# prints arguments in pretty form.
|
|
|
|
#
|
|
|
|
# pp returns nil.
|
|
|
|
def pp(*objs) # :doc:
|
2001-12-24 12:38:33 -05:00
|
|
|
objs.each {|obj|
|
|
|
|
PP.pp(obj)
|
|
|
|
}
|
2002-03-25 13:24:46 -05:00
|
|
|
nil
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2003-03-07 08:02:29 -05:00
|
|
|
module_function :pp
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class PP < PrettyPrint
|
2005-03-05 21:55:16 -05:00
|
|
|
# Outputs +obj+ to +out+ in pretty printed format of
|
|
|
|
# +width+ columns in width.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# If +out+ is omitted, +$>+ is assumed.
|
|
|
|
# If +width+ is omitted, 79 is assumed.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# PP.pp returns +out+.
|
2002-09-11 08:29:17 -04:00
|
|
|
def PP.pp(obj, out=$>, width=79)
|
2003-12-16 07:22:15 -05:00
|
|
|
q = PP.new(out, width)
|
|
|
|
q.guard_inspect_key {q.pp obj}
|
|
|
|
q.flush
|
|
|
|
#$pp = q
|
2001-12-24 12:38:33 -05:00
|
|
|
out << "\n"
|
|
|
|
end
|
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# Outputs +obj+ to +out+ like PP.pp but with no indent and
|
|
|
|
# newline.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# PP.singleline_pp returns +out+.
|
2002-12-02 11:16:05 -05:00
|
|
|
def PP.singleline_pp(obj, out=$>)
|
2003-12-16 07:22:15 -05:00
|
|
|
q = SingleLine.new(out)
|
|
|
|
q.guard_inspect_key {q.pp obj}
|
|
|
|
q.flush
|
2002-12-02 11:16:05 -05:00
|
|
|
out
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2006-05-13 03:35:50 -04:00
|
|
|
# :stopdoc:
|
|
|
|
def PP.mcall(obj, mod, meth, *args, &block)
|
|
|
|
mod.instance_method(meth).bind(obj).call(*args, &block)
|
|
|
|
end
|
|
|
|
# :startdoc:
|
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
@sharing_detection = false
|
|
|
|
class << self
|
2005-03-05 21:55:16 -05:00
|
|
|
# Returns the sharing detection flag as a boolean value.
|
|
|
|
# It is false by default.
|
2002-12-02 11:16:05 -05:00
|
|
|
attr_accessor :sharing_detection
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
module PPMethods
|
|
|
|
def guard_inspect_key
|
2005-03-09 09:56:16 -05:00
|
|
|
if Thread.current[:__recursive_key__] == nil
|
|
|
|
Thread.current[:__recursive_key__] = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if Thread.current[:__recursive_key__][:inspect] == nil
|
2007-12-07 01:45:28 -05:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = {}
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2005-03-09 09:56:16 -05:00
|
|
|
save = Thread.current[:__recursive_key__][:inspect]
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
begin
|
2007-12-07 01:45:28 -05:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = {}
|
2002-12-02 11:16:05 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2005-03-09 09:56:16 -05:00
|
|
|
Thread.current[:__recursive_key__][:inspect] = save
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2005-03-06 21:05:08 -05:00
|
|
|
def check_inspect_key(id)
|
2005-03-09 09:56:16 -05:00
|
|
|
Thread.current[:__recursive_key__] &&
|
|
|
|
Thread.current[:__recursive_key__][:inspect] &&
|
|
|
|
Thread.current[:__recursive_key__][:inspect].include?(id)
|
2005-03-06 21:05:08 -05:00
|
|
|
end
|
|
|
|
def push_inspect_key(id)
|
2007-12-07 01:45:28 -05:00
|
|
|
Thread.current[:__recursive_key__][:inspect][id] = true
|
2005-03-06 21:05:08 -05:00
|
|
|
end
|
2007-12-07 01:45:28 -05:00
|
|
|
def pop_inspect_key(id)
|
|
|
|
Thread.current[:__recursive_key__][:inspect].delete id
|
2005-03-06 21:05:08 -05:00
|
|
|
end
|
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# Adds +obj+ to the pretty printing buffer
|
|
|
|
# using Object#pretty_print or Object#pretty_print_cycle.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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 11:16:05 -05:00
|
|
|
def pp(obj)
|
2006-07-11 01:00:02 -04:00
|
|
|
id = obj.object_id
|
2008-09-14 11:18:53 -04:00
|
|
|
|
2005-03-06 21:05:08 -05:00
|
|
|
if check_inspect_key(id)
|
2002-12-02 11:16:05 -05:00
|
|
|
group {obj.pretty_print_cycle self}
|
|
|
|
return
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
begin
|
2005-03-06 21:05:08 -05:00
|
|
|
push_inspect_key(id)
|
2002-12-02 11:16:05 -05:00
|
|
|
group {obj.pretty_print self}
|
|
|
|
ensure
|
2007-12-07 01:45:28 -05:00
|
|
|
pop_inspect_key(id) unless PP.sharing_detection
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# A convenience method which is same as follows:
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# group(1, '#<' + obj.class.name, '>') { ... }
|
|
|
|
def object_group(obj, &block) # :yield:
|
2002-12-02 11:16:05 -05:00
|
|
|
group(1, '#<' + obj.class.name, '>', &block)
|
|
|
|
end
|
2002-06-27 08:01:07 -04:00
|
|
|
|
2005-11-29 23:44:12 -05:00
|
|
|
if 0x100000000.class == Bignum
|
|
|
|
# 32bit
|
|
|
|
PointerMask = 0xffffffff
|
|
|
|
else
|
|
|
|
# 64bit
|
|
|
|
PointerMask = 0xffffffffffffffff
|
|
|
|
end
|
|
|
|
|
2005-11-28 06:26:39 -05:00
|
|
|
case Object.new.inspect
|
|
|
|
when /\A\#<Object:0x([0-9a-f]+)>\z/
|
|
|
|
PointerFormat = "%0#{$1.length}x"
|
|
|
|
else
|
|
|
|
PointerFormat = "%x"
|
|
|
|
end
|
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
def object_address_group(obj, &block)
|
2006-07-11 01:00:02 -04:00
|
|
|
id = PointerFormat % (obj.object_id * 2 & PointerMask)
|
2004-11-13 23:27:39 -05:00
|
|
|
group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
2002-06-28 04:55:10 -04:00
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# A convenience method which is same as follows:
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# text ','
|
|
|
|
# breakable
|
2002-12-02 11:16:05 -05:00
|
|
|
def comma_breakable
|
|
|
|
text ','
|
|
|
|
breakable
|
|
|
|
end
|
2002-06-27 08:01:07 -04:00
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# Adds a separated list.
|
|
|
|
# The list is separated by comma with breakable space, by default.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# If the iteration is zero times, +separator_proc+ is not called at all.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# For example, following 3 code fragments has similar effect.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# q.seplist([1,2,3]) {|v| xxx v }
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2007-11-11 07:48:39 -05:00
|
|
|
# q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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-26 20:46:05 -05:00
|
|
|
sep ||= lambda { comma_breakable }
|
|
|
|
first = true
|
2007-06-05 01:48:45 -04:00
|
|
|
list.__send__(iter_method) {|*v|
|
2004-03-26 20:46:05 -05:00
|
|
|
if first
|
|
|
|
first = false
|
|
|
|
else
|
|
|
|
sep.call
|
|
|
|
end
|
2007-06-05 01:48:45 -04:00
|
|
|
yield(*v)
|
2004-03-26 20:46:05 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
def pp_object(obj)
|
|
|
|
object_address_group(obj) {
|
2004-02-05 09:59:46 -05:00
|
|
|
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
2002-12-02 11:16:05 -05:00
|
|
|
breakable
|
2004-02-05 09:59:46 -05:00
|
|
|
v = v.to_s if Symbol === v
|
2002-12-02 11:16:05 -05:00
|
|
|
text v
|
|
|
|
text '='
|
|
|
|
group(1) {
|
|
|
|
breakable ''
|
|
|
|
pp(obj.instance_eval(v))
|
|
|
|
}
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-12-02 11:16:05 -05:00
|
|
|
def pp_hash(obj)
|
|
|
|
group(1, '{', '}') {
|
2007-10-13 22:14:16 -04:00
|
|
|
seplist(obj, nil, :each_pair) {|k, v|
|
2002-12-02 11:16:05 -05:00
|
|
|
group {
|
|
|
|
pp k
|
|
|
|
text '=>'
|
|
|
|
group(1) {
|
|
|
|
breakable ''
|
|
|
|
pp v
|
|
|
|
}
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
2002-12-02 11:16:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include PPMethods
|
|
|
|
|
|
|
|
class SingleLine < PrettyPrint::SingleLine
|
|
|
|
include PPMethods
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module ObjectMixin
|
|
|
|
# 1. specific pretty_print
|
2003-05-10 16:53:58 -04:00
|
|
|
# 2. specific inspect
|
|
|
|
# 3. specific to_s if instance variable is empty
|
|
|
|
# 4. generic pretty_print
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# A default pretty printing method for general objects.
|
|
|
|
# It calls #pretty_print_instance_variables to list instance variables.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# This module provides predefined #pretty_print methods for some of
|
|
|
|
# the most commonly used built-in classes for convenience.
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
2007-06-07 06:02:58 -04:00
|
|
|
if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text self.inspect
|
2007-06-07 06:02:58 -04:00
|
|
|
elsif /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect && instance_variables.empty?
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text self.to_s
|
2001-12-24 12:38:33 -05:00
|
|
|
else
|
2003-12-16 07:22:15 -05:00
|
|
|
q.pp_object(self)
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# A default pretty printing method for general objects that are
|
|
|
|
# detected as part of a cycle.
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.object_address_group(self) {
|
|
|
|
q.breakable
|
|
|
|
q.text '...'
|
2002-06-28 04:55:10 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2002-06-27 08:01:07 -04:00
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# Returns a sorted array of instance variable names.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05:00
|
|
|
# This method should return an array of names of instance variables as symbols or strings as:
|
|
|
|
# +[:@a, :@b]+.
|
2002-06-27 08:01:07 -04:00
|
|
|
def pretty_print_instance_variables
|
|
|
|
instance_variables.sort
|
|
|
|
end
|
2003-01-20 12:58:24 -05:00
|
|
|
|
2005-03-05 21:55:16 -05:00
|
|
|
# Is #inspect implementation using #pretty_print.
|
|
|
|
# If you implement #pretty_print, it can be used as follows.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2005-03-05 21:55:16 -05: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 12:58:24 -05:00
|
|
|
def pretty_print_inspect
|
2007-06-07 06:02:58 -04:00
|
|
|
if /\(PP::ObjectMixin\)#/ =~ Object.instance_method(:method).bind(self).call(:pretty_print).inspect
|
2006-07-08 19:17:53 -04:00
|
|
|
raise "pretty_print is not overridden for #{self.class}"
|
2003-01-20 12:58:24 -05:00
|
|
|
end
|
|
|
|
PP.singleline_pp(self, '')
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Array
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
|
|
|
q.group(1, '[', ']') {
|
2004-02-05 09:59:46 -05:00
|
|
|
q.seplist(self) {|v|
|
2003-12-16 07:22:15 -05:00
|
|
|
q.pp v
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.text(empty? ? '[]' : '[...]')
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Hash
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
|
|
|
q.pp_hash self
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.text(empty? ? '{}' : '{...}')
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class << ENV
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
2007-10-13 22:14:16 -04:00
|
|
|
h = {}
|
|
|
|
ENV.keys.sort.each {|k|
|
|
|
|
h[k] = ENV[k]
|
|
|
|
}
|
|
|
|
q.pp_hash h
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Struct
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
2006-05-13 03:35:50 -04:00
|
|
|
q.group(1, '#<struct ' + PP.mcall(self, Kernel, :class).name, '>') {
|
|
|
|
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
2003-12-16 07:22:15 -05:00
|
|
|
q.breakable
|
|
|
|
q.text member.to_s
|
|
|
|
q.text '='
|
|
|
|
q.group(1) {
|
|
|
|
q.breakable ''
|
|
|
|
q.pp self[member]
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print_cycle(q)
|
2006-05-13 03:35:50 -04:00
|
|
|
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Range
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
|
|
|
q.pp self.begin
|
|
|
|
q.breakable ''
|
|
|
|
q.text(self.exclude_end? ? '...' : '..')
|
|
|
|
q.breakable ''
|
|
|
|
q.pp self.end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class File
|
|
|
|
class Stat
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
2001-12-24 12:38:33 -05:00
|
|
|
require 'etc.so'
|
2003-12-16 07:22:15 -05: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-02 22:49:42 -04:00
|
|
|
m = self.mode
|
2003-12-16 07:22:15 -05: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-02 22: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 07:22:15 -05: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-02 22:49:42 -04:00
|
|
|
begin
|
2005-03-05 21:41:15 -05:00
|
|
|
pw = Etc.getpwuid(self.uid)
|
2002-07-02 22:49:42 -04:00
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2005-03-05 21:41:15 -05:00
|
|
|
if pw
|
|
|
|
q.breakable; q.text "(#{pw.name})"
|
|
|
|
end
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
|
|
|
q.text "gid="; q.pp self.gid
|
2002-07-02 22:49:42 -04:00
|
|
|
begin
|
2005-03-05 21:41:15 -05:00
|
|
|
gr = Etc.getgrgid(self.gid)
|
2002-07-02 22:49:42 -04:00
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2005-03-05 21:41:15 -05:00
|
|
|
if gr
|
|
|
|
q.breakable; q.text "(#{gr.name})"
|
|
|
|
end
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
|
|
|
q.text sprintf("rdev=0x%x", self.rdev)
|
|
|
|
q.breakable
|
|
|
|
q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05: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-02 22:49:42 -04:00
|
|
|
t = self.atime
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text "atime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
2002-07-02 22:49:42 -04:00
|
|
|
t = self.mtime
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text "mtime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.comma_breakable
|
|
|
|
q.group {
|
2002-07-02 22:49:42 -04:00
|
|
|
t = self.ctime
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text "ctime="; q.pp t
|
|
|
|
q.breakable; q.text "(#{t.tv_sec})"
|
2002-07-02 22:49:42 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-03-07 08:02:29 -05:00
|
|
|
class MatchData
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print(q)
|
2007-12-09 16:44:19 -05:00
|
|
|
nc = []
|
|
|
|
self.regexp.named_captures.each {|name, indexes|
|
|
|
|
indexes.each {|i| nc[i] = name }
|
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.object_group(self) {
|
|
|
|
q.breakable
|
2007-12-09 16:44:19 -05: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 08:02:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2001-12-24 12:38:33 -05:00
|
|
|
class Object
|
|
|
|
include PP::ObjectMixin
|
|
|
|
end
|
|
|
|
|
2003-01-20 14:54:56 -05:00
|
|
|
[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
|
|
|
|
c.class_eval {
|
2003-12-16 07:22:15 -05:00
|
|
|
def pretty_print_cycle(q)
|
|
|
|
q.text inspect
|
2003-01-20 14:54:56 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-01 11:57:47 -05:00
|
|
|
[Numeric, FalseClass, TrueClass, Module].each {|c|
|
|
|
|
c.class_eval {
|
|
|
|
def pretty_print(q)
|
|
|
|
q.text inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|