* lib/pp.rb, lib/prettyprint.rb: Documentation for PP and PrettyPrint
Based on a patch by Vincent Batts [ruby-core:51253] [Bug #7656] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39180 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cf2da37701
commit
3952de50bf
@ -1,3 +1,8 @@
|
|||||||
|
Sun Feb 10 09:10:00 2013 Zachary Scott <zachary@zacharyscott.net>
|
||||||
|
|
||||||
|
* lib/pp.rb, lib/prettyprint.rb: Documentation for PP and PrettyPrint
|
||||||
|
Based on a patch by Vincent Batts [ruby-core:51253] [Bug #7656]
|
||||||
|
|
||||||
Sat Feb 9 21:11:21 2013 Tanaka Akira <akr@fsij.org>
|
Sat Feb 9 21:11:21 2013 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
* configure.in: move header files check to the beginning of
|
* configure.in: move header files check to the beginning of
|
||||||
|
91
lib/pp.rb
91
lib/pp.rb
@ -1,7 +1,13 @@
|
|||||||
require 'prettyprint'
|
require 'prettyprint'
|
||||||
|
|
||||||
module Kernel
|
module Kernel
|
||||||
# returns a pretty printed object as a string.
|
# Returns a pretty printed object as a string.
|
||||||
|
#
|
||||||
|
# In order to use this method you must first require the PP module:
|
||||||
|
#
|
||||||
|
# require 'pp'
|
||||||
|
#
|
||||||
|
# See the PP module for more information.
|
||||||
def pretty_inspect
|
def pretty_inspect
|
||||||
PP.pp(self, '')
|
PP.pp(self, '')
|
||||||
end
|
end
|
||||||
@ -10,23 +16,23 @@ module Kernel
|
|||||||
# prints arguments in pretty form.
|
# prints arguments in pretty form.
|
||||||
#
|
#
|
||||||
# pp returns argument(s).
|
# pp returns argument(s).
|
||||||
def pp(*objs) # :doc:
|
def pp(*objs) # :nodoc:
|
||||||
objs.each {|obj|
|
objs.each {|obj|
|
||||||
PP.pp(obj)
|
PP.pp(obj)
|
||||||
}
|
}
|
||||||
objs.size <= 1 ? objs.first : objs
|
objs.size <= 1 ? objs.first : objs
|
||||||
end
|
end
|
||||||
module_function :pp
|
module_function :pp # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Pretty-printer for Ruby objects.
|
# Pretty-printer for Ruby objects.
|
||||||
#
|
#
|
||||||
# = Which seems better?
|
# == Which seems better?
|
||||||
#
|
#
|
||||||
# non-pretty-printed output by #p is:
|
# Standard output by #p like this?
|
||||||
# #<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>>
|
# #<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>>
|
||||||
#
|
#
|
||||||
# pretty-printed output by #pp is:
|
# Or the pretty-printed version:
|
||||||
# #<PP:0x81fedf0
|
# #<PP:0x81fedf0
|
||||||
# @buffer=[],
|
# @buffer=[],
|
||||||
# @buffer_width=0,
|
# @buffer_width=0,
|
||||||
@ -46,25 +52,27 @@ end
|
|||||||
#
|
#
|
||||||
# I like the latter. If you do too, this library is for you.
|
# I like the latter. If you do too, this library is for you.
|
||||||
#
|
#
|
||||||
# = Usage
|
# == Usage
|
||||||
#
|
#
|
||||||
# pp(obj) #=> obj
|
# pp(obj) #=> obj
|
||||||
# pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
|
# pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
|
||||||
# pp() #=> nil
|
# pp() #=> nil
|
||||||
#
|
#
|
||||||
# output +obj(s)+ to +$>+ in pretty printed format.
|
# Output <tt>obj(s)</tt> to <tt>$></tt> in pretty printed format.
|
||||||
#
|
#
|
||||||
# It returns +obj(s)+.
|
# It returns <tt>obj(s)</tt>.
|
||||||
#
|
#
|
||||||
# = Output Customization
|
# = Output Customization
|
||||||
|
#
|
||||||
# To define your customized pretty printing function for your classes,
|
# To define your customized pretty printing function for your classes,
|
||||||
# redefine a method #pretty_print(+pp+) in the class.
|
# redefine a method #pretty_print(+pp+) in the class.
|
||||||
|
#
|
||||||
# It takes an argument +pp+ which is an instance of the class PP.
|
# 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
|
# The method should use #text, #breakable, #nest, #group and #pp to print the
|
||||||
# PP#pp to print the object.
|
# object.
|
||||||
#
|
#
|
||||||
# = Author
|
# = Author
|
||||||
# Tanaka Akira <akr@m17n.org>
|
# Tanaka Akira <akr@fsij.org>
|
||||||
class PP < PrettyPrint
|
class PP < PrettyPrint
|
||||||
# Outputs +obj+ to +out+ in pretty printed format of
|
# Outputs +obj+ to +out+ in pretty printed format of
|
||||||
# +width+ columns in width.
|
# +width+ columns in width.
|
||||||
@ -106,6 +114,9 @@ class PP < PrettyPrint
|
|||||||
end
|
end
|
||||||
|
|
||||||
module PPMethods
|
module PPMethods
|
||||||
|
|
||||||
|
# Yields to a block
|
||||||
|
# and preserves the previous set of objects being printed.
|
||||||
def guard_inspect_key
|
def guard_inspect_key
|
||||||
if Thread.current[:__recursive_key__] == nil
|
if Thread.current[:__recursive_key__] == nil
|
||||||
Thread.current[:__recursive_key__] = {}.untrust
|
Thread.current[:__recursive_key__] = {}.untrust
|
||||||
@ -125,14 +136,22 @@ class PP < PrettyPrint
|
|||||||
end
|
end
|
||||||
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)
|
def check_inspect_key(id)
|
||||||
Thread.current[:__recursive_key__] &&
|
Thread.current[:__recursive_key__] &&
|
||||||
Thread.current[:__recursive_key__][:inspect] &&
|
Thread.current[:__recursive_key__][:inspect] &&
|
||||||
Thread.current[:__recursive_key__][:inspect].include?(id)
|
Thread.current[:__recursive_key__][:inspect].include?(id)
|
||||||
end
|
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)
|
def push_inspect_key(id)
|
||||||
Thread.current[:__recursive_key__][:inspect][id] = true
|
Thread.current[:__recursive_key__][:inspect][id] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Removes an object from the set of objects being pretty printed.
|
||||||
def pop_inspect_key(id)
|
def pop_inspect_key(id)
|
||||||
Thread.current[:__recursive_key__][:inspect].delete id
|
Thread.current[:__recursive_key__][:inspect].delete id
|
||||||
end
|
end
|
||||||
@ -165,15 +184,19 @@ class PP < PrettyPrint
|
|||||||
group(1, '#<' + obj.class.name, '>', &block)
|
group(1, '#<' + obj.class.name, '>', &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A mask used in formating object_id's into a hexadecimal id
|
||||||
PointerMask = (1 << ([""].pack("p").size * 8)) - 1
|
PointerMask = (1 << ([""].pack("p").size * 8)) - 1
|
||||||
|
|
||||||
case Object.new.inspect
|
case Object.new.inspect
|
||||||
when /\A\#<Object:0x([0-9a-f]+)>\z/
|
when /\A\#<Object:0x([0-9a-f]+)>\z/
|
||||||
|
# String Formating for hexadecimal id
|
||||||
PointerFormat = "%0#{$1.length}x"
|
PointerFormat = "%0#{$1.length}x"
|
||||||
else
|
else
|
||||||
PointerFormat = "%x"
|
PointerFormat = "%x"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A convenience method, like object_group, but also reformats the Object's
|
||||||
|
# object_id.
|
||||||
def object_address_group(obj, &block)
|
def object_address_group(obj, &block)
|
||||||
id = PointerFormat % (obj.object_id * 2 & PointerMask)
|
id = PointerFormat % (obj.object_id * 2 & PointerMask)
|
||||||
group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
|
group(1, "\#<#{obj.class}:0x#{id}", '>', &block)
|
||||||
@ -225,6 +248,7 @@ class PP < PrettyPrint
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A present standard failsafe for pretty printing any given Object
|
||||||
def pp_object(obj)
|
def pp_object(obj)
|
||||||
object_address_group(obj) {
|
object_address_group(obj) {
|
||||||
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
||||||
@ -240,6 +264,7 @@ class PP < PrettyPrint
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A pretty print for a Hash
|
||||||
def pp_hash(obj)
|
def pp_hash(obj)
|
||||||
group(1, '{', '}') {
|
group(1, '{', '}') {
|
||||||
seplist(obj, nil, :each_pair) {|k, v|
|
seplist(obj, nil, :each_pair) {|k, v|
|
||||||
@ -258,7 +283,7 @@ class PP < PrettyPrint
|
|||||||
|
|
||||||
include PPMethods
|
include PPMethods
|
||||||
|
|
||||||
class SingleLine < PrettyPrint::SingleLine
|
class SingleLine < PrettyPrint::SingleLine # :nodoc:
|
||||||
include PPMethods
|
include PPMethods
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -324,8 +349,8 @@ class PP < PrettyPrint
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Array
|
class Array # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
q.group(1, '[', ']') {
|
q.group(1, '[', ']') {
|
||||||
q.seplist(self) {|v|
|
q.seplist(self) {|v|
|
||||||
q.pp v
|
q.pp v
|
||||||
@ -333,23 +358,23 @@ class Array
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretty_print_cycle(q)
|
def pretty_print_cycle(q) # :nodoc:
|
||||||
q.text(empty? ? '[]' : '[...]')
|
q.text(empty? ? '[]' : '[...]')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Hash
|
class Hash # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
q.pp_hash self
|
q.pp_hash self
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretty_print_cycle(q)
|
def pretty_print_cycle(q) # :nodoc:
|
||||||
q.text(empty? ? '{}' : '{...}')
|
q.text(empty? ? '{}' : '{...}')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class << ENV
|
class << ENV # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
h = {}
|
h = {}
|
||||||
ENV.keys.sort.each {|k|
|
ENV.keys.sort.each {|k|
|
||||||
h[k] = ENV[k]
|
h[k] = ENV[k]
|
||||||
@ -358,8 +383,8 @@ class << ENV
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Struct
|
class Struct # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
||||||
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
||||||
q.breakable
|
q.breakable
|
||||||
@ -373,13 +398,13 @@ class Struct
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def pretty_print_cycle(q)
|
def pretty_print_cycle(q) # :nodoc:
|
||||||
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Range
|
class Range # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
q.pp self.begin
|
q.pp self.begin
|
||||||
q.breakable ''
|
q.breakable ''
|
||||||
q.text(self.exclude_end? ? '...' : '..')
|
q.text(self.exclude_end? ? '...' : '..')
|
||||||
@ -388,9 +413,9 @@ class Range
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class File < IO
|
class File < IO # :nodoc:
|
||||||
class Stat
|
class Stat # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
require 'etc.so'
|
require 'etc.so'
|
||||||
q.object_group(self) {
|
q.object_group(self) {
|
||||||
q.breakable
|
q.breakable
|
||||||
@ -470,8 +495,8 @@ class File < IO
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class MatchData
|
class MatchData # :nodoc:
|
||||||
def pretty_print(q)
|
def pretty_print(q) # :nodoc:
|
||||||
nc = []
|
nc = []
|
||||||
self.regexp.named_captures.each {|name, indexes|
|
self.regexp.named_captures.each {|name, indexes|
|
||||||
indexes.each {|i| nc[i] = name }
|
indexes.each {|i| nc[i] = name }
|
||||||
@ -495,7 +520,7 @@ class MatchData
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Object < BasicObject
|
class Object < BasicObject # :nodoc:
|
||||||
include PP::ObjectMixin
|
include PP::ObjectMixin
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
# * Box based formatting?
|
# * Box based formatting?
|
||||||
# * Other (better) model/algorithm?
|
# * Other (better) model/algorithm?
|
||||||
#
|
#
|
||||||
|
# Report any bugs at http://bugs.ruby-lang.org
|
||||||
|
#
|
||||||
# == References
|
# == References
|
||||||
# Christian Lindig, Strictly Pretty, March 2000,
|
# Christian Lindig, Strictly Pretty, March 2000,
|
||||||
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
|
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
|
||||||
@ -25,7 +27,7 @@
|
|||||||
# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
|
# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
|
||||||
#
|
#
|
||||||
# == Author
|
# == Author
|
||||||
# Tanaka Akira <akr@m17n.org>
|
# Tanaka Akira <akr@fsij.org>
|
||||||
#
|
#
|
||||||
class PrettyPrint
|
class PrettyPrint
|
||||||
|
|
||||||
@ -90,10 +92,64 @@ class PrettyPrint
|
|||||||
@group_queue = GroupQueue.new(root_group)
|
@group_queue = GroupQueue.new(root_group)
|
||||||
@indent = 0
|
@indent = 0
|
||||||
end
|
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 separated 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.
|
# Returns the group most recently added to the stack.
|
||||||
|
#
|
||||||
|
# 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 current_group
|
def current_group
|
||||||
@group_stack.last
|
@group_stack.last
|
||||||
end
|
end
|
||||||
@ -166,7 +222,7 @@ class PrettyPrint
|
|||||||
# may cause 2 results:
|
# may cause 2 results:
|
||||||
# (break,break), (non-break,non-break).
|
# (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.
|
# If +sep+ is not specified, " " is used.
|
||||||
#
|
#
|
||||||
@ -220,6 +276,7 @@ class PrettyPrint
|
|||||||
text close_obj, close_width
|
text close_obj, close_width
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Takes a block and queues a new group that is indented 1 level further.
|
||||||
def group_sub
|
def group_sub
|
||||||
group = Group.new(@group_stack.last.depth + 1)
|
group = Group.new(@group_stack.last.depth + 1)
|
||||||
@group_stack.push group
|
@group_stack.push group
|
||||||
@ -256,25 +313,55 @@ class PrettyPrint
|
|||||||
@buffer_width = 0
|
@buffer_width = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
class Text
|
# 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 # :nodoc:
|
||||||
|
|
||||||
|
# Creates a new text object.
|
||||||
|
#
|
||||||
|
# This contructor takes no arguments.
|
||||||
|
#
|
||||||
|
# The workflow is to append a PrettyPrint::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
|
def initialize
|
||||||
@objs = []
|
@objs = []
|
||||||
@width = 0
|
@width = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The total width of the objects included in this Text object.
|
||||||
attr_reader :width
|
attr_reader :width
|
||||||
|
|
||||||
|
# Render the String text of the objects that have been added to this Text object.
|
||||||
|
#
|
||||||
|
# Output the text to +out+, and increment the width to +output_width+
|
||||||
def output(out, output_width)
|
def output(out, output_width)
|
||||||
@objs.each {|obj| out << obj}
|
@objs.each {|obj| out << obj}
|
||||||
output_width + @width
|
output_width + @width
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Include +obj+ in the objects to be pretty printed, and increment
|
||||||
|
# this Text object's total width by +width+
|
||||||
def add(obj, width)
|
def add(obj, width)
|
||||||
@objs << obj
|
@objs << obj
|
||||||
@width += width
|
@width += width
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Breakable
|
# The Breakable class is used for breaking up object information
|
||||||
|
#
|
||||||
|
# This class is intended for internal use of the PrettyPrint buffers.
|
||||||
|
class Breakable # :nodoc:
|
||||||
|
|
||||||
|
# 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)
|
def initialize(sep, width, q)
|
||||||
@obj = sep
|
@obj = sep
|
||||||
@width = width
|
@width = width
|
||||||
@ -283,8 +370,24 @@ class PrettyPrint
|
|||||||
@group = q.current_group
|
@group = q.current_group
|
||||||
@group.breakables.push self
|
@group.breakables.push self
|
||||||
end
|
end
|
||||||
attr_reader :obj, :width, :indent
|
|
||||||
|
|
||||||
|
# Holds the seperator String
|
||||||
|
#
|
||||||
|
# The +sep+ argument from ::new
|
||||||
|
attr_reader :obj
|
||||||
|
|
||||||
|
# The width of +obj+ / +sep+
|
||||||
|
attr_reader :width
|
||||||
|
|
||||||
|
# The number of spaces to indent.
|
||||||
|
#
|
||||||
|
# This is inferred from +q+ within PrettyPrint, passed in ::new
|
||||||
|
attr_reader :indent
|
||||||
|
|
||||||
|
# Render the String text of the objects that have been added to this
|
||||||
|
# Breakable object.
|
||||||
|
#
|
||||||
|
# Output the text to +out+, and increment the width to +output_width+
|
||||||
def output(out, output_width)
|
def output(out, output_width)
|
||||||
@group.breakables.shift
|
@group.breakables.shift
|
||||||
if @group.break?
|
if @group.break?
|
||||||
@ -299,22 +402,45 @@ class PrettyPrint
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Group
|
# 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 # :nodoc:
|
||||||
|
# Create a Group object
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# * +depth+ - this group's relation to previous groups
|
||||||
def initialize(depth)
|
def initialize(depth)
|
||||||
@depth = depth
|
@depth = depth
|
||||||
@breakables = []
|
@breakables = []
|
||||||
@break = false
|
@break = false
|
||||||
end
|
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
|
def break
|
||||||
@break = true
|
@break = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Boolean of whether this Group has made a break
|
||||||
def break?
|
def break?
|
||||||
@break
|
@break
|
||||||
end
|
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?
|
def first?
|
||||||
if defined? @first
|
if defined? @first
|
||||||
false
|
false
|
||||||
@ -325,18 +451,33 @@ class PrettyPrint
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class GroupQueue
|
# 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 # :nodoc:
|
||||||
|
# Create a GroupQueue object
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# * +groups+ - one or more PrettyPrint::Group objects
|
||||||
def initialize(*groups)
|
def initialize(*groups)
|
||||||
@queue = []
|
@queue = []
|
||||||
groups.each {|g| enq g}
|
groups.each {|g| enq g}
|
||||||
end
|
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)
|
def enq(group)
|
||||||
depth = group.depth
|
depth = group.depth
|
||||||
@queue << [] until depth < @queue.length
|
@queue << [] until depth < @queue.length
|
||||||
@queue[depth] << group
|
@queue[depth] << group
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the outer group of the queue
|
||||||
def deq
|
def deq
|
||||||
@queue.each {|gs|
|
@queue.each {|gs|
|
||||||
(gs.length-1).downto(0) {|i|
|
(gs.length-1).downto(0) {|i|
|
||||||
@ -352,29 +493,67 @@ class PrettyPrint
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Remote +group+ from this queue
|
||||||
def delete(group)
|
def delete(group)
|
||||||
@queue[group.depth].delete(group)
|
@queue[group.depth].delete(group)
|
||||||
end
|
end
|
||||||
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
|
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)
|
def initialize(output, maxwidth=nil, newline=nil)
|
||||||
@output = output
|
@output = output
|
||||||
@first = [true]
|
@first = [true]
|
||||||
end
|
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)
|
def text(obj, width=nil)
|
||||||
@output << obj
|
@output << obj
|
||||||
end
|
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)
|
def breakable(sep=' ', width=nil)
|
||||||
@output << sep
|
@output << sep
|
||||||
end
|
end
|
||||||
|
|
||||||
def nest(indent)
|
# Takes +indent+ arg, but does nothing with it.
|
||||||
|
#
|
||||||
|
# Yields to a block.
|
||||||
|
def nest(indent) # :nodoc:
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Opens a block 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)
|
def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
|
||||||
@first.push true
|
@first.push true
|
||||||
@output << open_obj
|
@output << open_obj
|
||||||
@ -383,9 +562,11 @@ class PrettyPrint
|
|||||||
@first.pop
|
@first.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
def flush
|
# Method present for compatibility, but is a noop
|
||||||
|
def flush # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# This is used as a predicate, and ought to be called first.
|
||||||
def first?
|
def first?
|
||||||
result = @first[-1]
|
result = @first[-1]
|
||||||
@first[-1] = false
|
@first[-1] = false
|
||||||
|
Loading…
x
Reference in New Issue
Block a user