Bug #7656 » ruby-docs_PrettyPrint-PP_20130206.patch
lib/pp.rb | ||
---|---|---|
end
|
||
module PPMethods
|
||
# Yields to a block
|
||
# and preserves the previous set of objects being printed.
|
||
def guard_inspect_key
|
||
if Thread.current[:__recursive_key__] == nil
|
||
Thread.current[:__recursive_key__] = {}.untrust
|
||
... | ... | |
end
|
||
end
|
||
# 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.
|
||
def check_inspect_key(id)
|
||
Thread.current[:__recursive_key__] &&
|
||
Thread.current[:__recursive_key__][:inspect] &&
|
||
Thread.current[:__recursive_key__][:inspect].include?(id)
|
||
end
|
||
# Adds the object_id +id+ to the set of objects being pretty printed, so
|
||
# as to not repeat objects.
|
||
def push_inspect_key(id)
|
||
Thread.current[:__recursive_key__][:inspect][id] = true
|
||
end
|
||
# Removes an object from the set of objects being pretty printed.
|
||
def pop_inspect_key(id)
|
||
Thread.current[:__recursive_key__][:inspect].delete id
|
||
end
|
||
... | ... | |
group(1, '#<' + obj.class.name, '>', &block)
|
||
end
|
||
# A mask used in formating object_id's into a hexadecimal id
|
||
PointerMask = (1 << ([""].pack("p").size * 8)) - 1
|
||
case Object.new.inspect
|
||
when /\A\#<Object:0x([0-9a-f]+)>\z/
|
||
# String Formating for hexadecimal id
|
||
PointerFormat = "%0#{$1.length}x"
|
||
else
|
||
PointerFormat = "%x"
|
||
end
|
||
def object_address_group(obj, &block)
|
||
# A convenience method, like object_group, but also reformats the Object's
|
||
# object_id.
|
||
def object_address_group(obj, &block) # :yield:
|
||
id = PointerFormat % (obj.object_id * 2 & PointerMask)
|
||
group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
|
||
end
|
||
... | ... | |
}
|
||
end
|
||
# A present standard failsafe for pretty printing any given Object
|
||
def pp_object(obj)
|
||
object_address_group(obj) {
|
||
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
||
... | ... | |
}
|
||
end
|
||
# A pretty print for a Hash
|
||
def pp_hash(obj)
|
||
group(1, '{', '}') {
|
||
seplist(obj, nil, :each_pair) {|k, v|
|
||
... | ... | |
end
|
||
class Array
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
q.group(1, '[', ']') {
|
||
q.seplist(self) {|v|
|
||
q.pp v
|
||
... | ... | |
}
|
||
end
|
||
def pretty_print_cycle(q)
|
||
def pretty_print_cycle(q) # :nodoc:
|
||
q.text(empty? ? '[]' : '[...]')
|
||
end
|
||
end
|
||
class Hash
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
q.pp_hash self
|
||
end
|
||
def pretty_print_cycle(q)
|
||
def pretty_print_cycle(q) # :nodoc:
|
||
q.text(empty? ? '{}' : '{...}')
|
||
end
|
||
end
|
||
class << ENV
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
h = {}
|
||
ENV.keys.sort.each {|k|
|
||
h[k] = ENV[k]
|
||
... | ... | |
end
|
||
class Struct
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
||
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
||
q.breakable
|
||
... | ... | |
}
|
||
end
|
||
def pretty_print_cycle(q)
|
||
def pretty_print_cycle(q) # :nodoc:
|
||
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
||
end
|
||
end
|
||
class Range
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
q.pp self.begin
|
||
q.breakable ''
|
||
q.text(self.exclude_end? ? '...' : '..')
|
||
... | ... | |
class File < IO
|
||
class Stat
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
require 'etc.so'
|
||
q.object_group(self) {
|
||
q.breakable
|
||
... | ... | |
end
|
||
class MatchData
|
||
def pretty_print(q)
|
||
def pretty_print(q) # :nodoc:
|
||
nc = []
|
||
self.regexp.named_captures.each {|name, indexes|
|
||
indexes.each {|i| nc[i] = name }
|
lib/prettyprint.rb | ||
---|---|---|
@group_queue = GroupQueue.new(root_group)
|
||
@indent = 0
|
||
end
|
||
attr_reader :output, :maxwidth, :newline, :genspace
|
||
attr_reader :indent, :group_queue
|
||
# The output object.
|
||
#
|
||
# This defaults to '', and should accept the << method
|
||
attr_reader :output
|
||
# The maximum width of a line, before it is seperated in to a newline
|
||
#
|
||
# This defaults to 79, and should be a Fixnum
|
||
attr_reader :maxwidth
|
||
# The value that is appended to +output+ to add a new line.
|
||
#
|
||
# This defaults to "\n", and should be String
|
||
attr_reader :newline
|
||
# A lambda or Proc, that takes one argument, of a Fixnum, and returns
|
||
# the corresponding number of spaces.
|
||
#
|
||
# By default this is:
|
||
# lambda {|n| ' ' * n}
|
||
attr_reader :genspace
|
||
# The number of spaces to be indented
|
||
attr_reader :indent
|
||
# The PrettyPrint::GroupQueue of groups in stack to be pretty printed
|
||
attr_reader :group_queue
|
||
# Returns the group most recently added to the stack.
|
||
def current_group
|
||
... | ... | |
# may cause 2 results:
|
||
# (break,break), (non-break,non-break).
|
||
#
|
||
# The text sep+ is inserted if a line is not broken at this point.
|
||
# The text +sep+ is inserted if a line is not broken at this point.
|
||
#
|
||
# If +sep+ is not specified, " " is used.
|
||
#
|
||
... | ... | |
# before grouping. If +close_obj+ is specified, <tt>text close_obj,
|
||
# close_width</tt> is called after grouping.
|
||
#
|
||
# Here is a contrived example to see the +depth+ of the group increase:
|
||
#
|
||
# p q.current_group #=> #<PrettyPrint::Group:0x00000000cf0210 @break=false, @breakables=[], @depth=0>
|
||
# q.group(1, '[',']') {
|
||
# p q.current_group
|
||
# }
|
||
# #=> <PrettyPrint::Group:0x00000000d29060 @break=false, @breakables=[], @depth=1>
|
||
#
|
||
def group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length)
|
||
text open_obj, open_width
|
||
group_sub {
|
||
... | ... | |
text close_obj, close_width
|
||
end
|
||
# Takes a &blok and queues a new group that is indented 1 further.
|
||
def group_sub
|
||
group = Group.new(@group_stack.last.depth + 1)
|
||
@group_stack.push group
|
||
... | ... | |
@buffer_width = 0
|
||
end
|
||
# The Text class is the means by which to collect strings from objects.
|
||
#
|
||
# This class is intended for internal use of the PrettyPrint buffers.
|
||
class Text
|
||
# Create a new PrettyPrint::Text object.
|
||
#
|
||
# This contructor takes no arguments. The workflow is to append a Text
|
||
# object to the buffer, and being able to call the buffer.last() to
|
||
# reference it.
|
||
# As there are objects, use PrettyPrint::Text#add to include the objects
|
||
# and the width to utilized by the String version of this object.
|
||
def initialize
|
||
@objs = []
|
||
@width = 0
|
||
end
|
||
# The total width of the objects included in this PrettyPrint::Text object.
|
||
attr_reader :width
|
||
# Render the String text of the objects that have been added to this
|
||
# PrettyPrint::Text. Output the text to +out+, and increment the width
|
||
# to +output_width+
|
||
def output(out, output_width)
|
||
@objs.each {|obj| out << obj}
|
||
output_width + @width
|
||
end
|
||
# Include +obj+ in the objects to be pretty printed, and increment
|
||
# this Text object's total width by +width+
|
||
def add(obj, width)
|
||
@objs << obj
|
||
@width += width
|
||
end
|
||
end
|
||
# The Breakable class is used for breaking up object information
|
||
#
|
||
# This class is intended for internal use of the PrettyPrint buffers.
|
||
class Breakable
|
||
# Create a new Breakable object.
|
||
#
|
||
# Arguments:
|
||
# * +sep+ String of the seperator
|
||
# * +width+ Fixnum width of the +sep+
|
||
# * +q+ parent PrettyPrint object, to base from
|
||
def initialize(sep, width, q)
|
||
@obj = sep
|
||
@width = width
|
||
... | ... | |
@group = q.current_group
|
||
@group.breakables.push self
|
||
end
|
||
attr_reader :obj, :width, :indent
|
||
# holds the seperator String
|
||
#
|
||
# (+sep+ from #new)
|
||
attr_reader :obj
|
||
# the width of +obj+ / +sep+
|
||
attr_reader :width
|
||
# the number of spaces to indent.
|
||
#
|
||
# This is inferred from +q+ PrettyPrint, given in #new
|
||
attr_reader :indent
|
||
# Render the String text of the objects that have been added to this
|
||
# PrettyPrint::Breakable. Output the text to +out+, and increment the width
|
||
# to +output_width+
|
||
def output(out, output_width)
|
||
@group.breakables.shift
|
||
if @group.break?
|
||
... | ... | |
end
|
||
end
|
||
# The Group class is used for making indentation easier.
|
||
#
|
||
# While this class does neither the breaking into newlines nor indentation,
|
||
# it is used in a stack (as well as a queue) within PrettyPrint, to group
|
||
# objects.
|
||
#
|
||
# For information on using groups, see PrettyPrint#group
|
||
#
|
||
# This class is intended for internal use of the PrettyPrint buffers.
|
||
class Group
|
||
# Create a Group object
|
||
#
|
||
# Arguments:
|
||
# * +depth+ - this group's relation to previous groups
|
||
#
|
||
# Contrived example:
|
||
# out = ""
|
||
# => ""
|
||
# q = PrettyPrint.new(out)
|
||
# => #<PrettyPrint:0x82f85c0 @output="", @maxwidth=79, @newline="\n", @genspace=#<Proc:0x82f8368@/home/vbatts/.rvm/rubies/ruby-head/lib/ruby/2.0.0/prettyprint.rb:82 (lambda)>, @output_width=0, @buffer_width=0, @buffer=[], @group_stack=[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>], @group_queue=#<PrettyPrint::GroupQueue:0x82fb7c0 @queue=[[#<PrettyPrint::Group:0x82f8138 @depth=0, @breakables=[], @break=false>]]>, @indent=0>
|
||
# q.group {
|
||
# q.text q.current_group.inspect
|
||
# q.text q.newline
|
||
# q.group(q.current_group.depth + 1) {
|
||
# q.text q.current_group.inspect
|
||
# q.text q.newline
|
||
# q.group(q.current_group.depth + 1) {
|
||
# q.text q.current_group.inspect
|
||
# q.text q.newline
|
||
# q.group(q.current_group.depth + 1) {
|
||
# q.text q.current_group.inspect
|
||
# q.text q.newline
|
||
# }
|
||
# }
|
||
# }
|
||
# }
|
||
# => 284
|
||
# puts out
|
||
# #<PrettyPrint::Group:0x8354758 @depth=1, @breakables=[], @break=false>
|
||
# #<PrettyPrint::Group:0x8354550 @depth=2, @breakables=[], @break=false>
|
||
# #<PrettyPrint::Group:0x83541cc @depth=3, @breakables=[], @break=false>
|
||
# #<PrettyPrint::Group:0x8347e54 @depth=4, @breakables=[], @break=false>
|
||
#
|
||
def initialize(depth)
|
||
@depth = depth
|
||
@breakables = []
|
||
@break = false
|
||
end
|
||
attr_reader :depth, :breakables
|
||
# this group's relation to previous groups
|
||
attr_reader :depth
|
||
# Array to hold the Breakable objects for this Group
|
||
attr_reader :breakables
|
||
# Makes a break for this Group, and returns true
|
||
def break
|
||
@break = true
|
||
end
|
||
# Boolean of whether this Group has made a break
|
||
def break?
|
||
@break
|
||
end
|
||
# Boolean of whether this Group has been queried for being first
|
||
#
|
||
# This is used as a predicate, and ought to be called first.
|
||
def first?
|
||
if defined? @first
|
||
false
|
||
... | ... | |
end
|
||
end
|
||
# The GroupQueue class is used for managing the queue of Group to be pretty
|
||
# printed.
|
||
#
|
||
# This queue groups the Group objects, based on their depth.
|
||
#
|
||
# This class is intended for internal use of the PrettyPrint buffers.
|
||
class GroupQueue
|
||
# Create a GroupQueue object
|
||
#
|
||
# Arguments:
|
||
# * +groups+ - one or more PrettyPrint::Group objects
|
||
def initialize(*groups)
|
||
@queue = []
|
||
groups.each {|g| enq g}
|
||
end
|
||
# Enqueue +group+
|
||
#
|
||
# This does not strictly append the group to the end of the queue,
|
||
# but instead adds it in line, base on the +group.depth+
|
||
def enq(group)
|
||
depth = group.depth
|
||
@queue << [] until depth < @queue.length
|
||
@queue[depth] << group
|
||
end
|
||
# returns the outer group of the queue
|
||
def deq
|
||
@queue.each {|gs|
|
||
(gs.length-1).downto(0) {|i|
|
||
... | ... | |
return nil
|
||
end
|
||
# Remote +group+ from this queue
|
||
def delete(group)
|
||
@queue[group.depth].delete(group)
|
||
end
|
||
end
|
||
# PrettyPrint::SingleLine is used by PrettyPrint.singleline_format
|
||
#
|
||
# It is passed to be similar to a PrettyPrint object itself, by responding to:
|
||
# * #text
|
||
# * #breakable
|
||
# * #nest
|
||
# * #group
|
||
# * #flush
|
||
# * #first?
|
||
#
|
||
# but instead, the output has no line breaks
|
||
#
|
||
class SingleLine
|
||
# Create a PrettyPrint::SingleLine object
|
||
#
|
||
# Arguments:
|
||
# * +output+ - String (or similar) to store rendered text. Needs to respond to '<<'
|
||
# * +maxwidth+ - Argument position expected to be here for compatibility.
|
||
# This argument is a noop.
|
||
# * +newline+ - Argument position expected to be here for compatibility.
|
||
# This argument is a noop.
|
||
def initialize(output, maxwidth=nil, newline=nil)
|
||
@output = output
|
||
@first = [true]
|
||
end
|
||
# Add +obj+ to the text to be outputed.
|
||
#
|
||
# +width+ argument is here for compatibility. It is a noop argument.
|
||
def text(obj, width=nil)
|
||
@output << obj
|
||
end
|
||
# Appends +sep+ to the text to be outputed. By default +sep+ is ' '
|
||
#
|
||
# +width+ argument is here for compatibility. It is a noop argument.
|
||
def breakable(sep=' ', width=nil)
|
||
@output << sep
|
||
end
|
||
# Takes +indent+ arg, but does nothing with it. Yields to a &blok
|
||
def nest(indent)
|
||
yield
|
||
end
|
||
# Opens a &blok for grouping objects to be pretty printed.
|
||
#
|
||
# Arguments:
|
||
# * +indent+ - noop argument. Present for compatibility.
|
||
# * +open_obj+ - text appended before the &blok. Default is ''
|
||
# * +close_obj+ - text appended after the &blok. Default is ''
|
||
# * +open_width+ - noop argument. Present for compatibility.
|
||
# * +close_width+ - noop argument. Present for compatibility.
|
||
def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
|
||
@first.push true
|
||
@output << open_obj
|
||
... | ... | |
@first.pop
|
||
end
|
||
# Method present for compatibility, but is a noop
|
||
def flush
|
||
end
|
||
# This is used as a predicate, and ought to be called first.
|
||
def first?
|
||
result = @first[-1]
|
||
@first[-1] = false
|
- « Previous
- 1
- 2
- Next »