2015-11-10 14:48:14 +03:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 16:27:48 +03:00
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# This class implements a pretty printing algorithm. It finds line breaks and
|
|
|
|
# nice indentations for grouped structure.
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# By default, the class assumes that primitive elements are strings and each
|
|
|
|
# byte in the strings have single column in width. But it can be used for
|
|
|
|
# other situations by giving suitable arguments for some methods:
|
|
|
|
# * newline object and space generation block for PrettyPrint.new
|
|
|
|
# * optional width argument for PrettyPrint#text
|
|
|
|
# * PrettyPrint#breakable
|
|
|
|
#
|
|
|
|
# There are several candidate uses:
|
|
|
|
# * text formatting using proportional fonts
|
|
|
|
# * multibyte characters which has columns different to number of bytes
|
|
|
|
# * non-string formatting
|
|
|
|
#
|
|
|
|
# == Bugs
|
|
|
|
# * Box based formatting?
|
|
|
|
# * Other (better) model/algorithm?
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2013-02-10 04:10:11 +04:00
|
|
|
# Report any bugs at http://bugs.ruby-lang.org
|
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# == References
|
|
|
|
# Christian Lindig, Strictly Pretty, March 2000,
|
|
|
|
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# Philip Wadler, A prettier printer, March 1998,
|
|
|
|
# http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# == Author
|
2013-02-10 04:10:11 +04:00
|
|
|
# Tanaka Akira <akr@fsij.org>
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2001-12-24 20:38:33 +03:00
|
|
|
class PrettyPrint
|
2006-07-17 22:22:16 +04:00
|
|
|
|
|
|
|
# This is a convenience method which is same as follows:
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2006-07-17 22:22:16 +04:00
|
|
|
# begin
|
|
|
|
# q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
|
|
|
# ...
|
|
|
|
# q.flush
|
|
|
|
# output
|
|
|
|
# end
|
2008-09-14 19:18:53 +04:00
|
|
|
#
|
2015-10-05 16:27:48 +03:00
|
|
|
def PrettyPrint.format(output=''.dup, maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
|
2003-12-16 15:22:15 +03:00
|
|
|
q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
|
|
|
yield q
|
|
|
|
q.flush
|
2002-06-27 16:01:07 +04:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# This is similar to PrettyPrint::format but the result has no breaks.
|
|
|
|
#
|
|
|
|
# +maxwidth+, +newline+ and +genspace+ are ignored.
|
|
|
|
#
|
|
|
|
# The invocation of +breakable+ in the block doesn't break a line and is
|
|
|
|
# treated as just an invocation of +text+.
|
|
|
|
#
|
2015-10-05 16:27:48 +03:00
|
|
|
def PrettyPrint.singleline_format(output=''.dup, maxwidth=nil, newline=nil, genspace=nil)
|
2003-12-16 15:22:15 +03:00
|
|
|
q = SingleLine.new(output)
|
|
|
|
yield q
|
2002-10-08 05:58:34 +04:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# Creates a buffer for pretty printing.
|
|
|
|
#
|
|
|
|
# +output+ is an output target. If it is not specified, '' is assumed. It
|
|
|
|
# should have a << method which accepts the first argument +obj+ of
|
|
|
|
# PrettyPrint#text, the first argument +sep+ of PrettyPrint#breakable, the
|
|
|
|
# first argument +newline+ of PrettyPrint.new, and the result of a given
|
|
|
|
# block for PrettyPrint.new.
|
|
|
|
#
|
|
|
|
# +maxwidth+ specifies maximum line length. If it is not specified, 79 is
|
|
|
|
# assumed. However actual outputs may overflow +maxwidth+ if long
|
|
|
|
# non-breakable texts are provided.
|
|
|
|
#
|
|
|
|
# +newline+ is used for line breaks. "\n" is used if it is not specified.
|
|
|
|
#
|
|
|
|
# The block is used to generate spaces. {|width| ' ' * width} is used if it
|
|
|
|
# is not given.
|
|
|
|
#
|
2015-10-05 16:27:48 +03:00
|
|
|
def initialize(output=''.dup, maxwidth=79, newline="\n", &genspace)
|
2002-06-27 10:27:20 +04:00
|
|
|
@output = output
|
|
|
|
@maxwidth = maxwidth
|
2001-12-24 20:38:33 +03:00
|
|
|
@newline = newline
|
|
|
|
@genspace = genspace || lambda {|n| ' ' * n}
|
2002-06-27 10:27:20 +04:00
|
|
|
|
|
|
|
@output_width = 0
|
|
|
|
@buffer_width = 0
|
|
|
|
@buffer = []
|
|
|
|
|
|
|
|
root_group = Group.new(0)
|
|
|
|
@group_stack = [root_group]
|
|
|
|
@group_queue = GroupQueue.new(root_group)
|
|
|
|
@indent = 0
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
2013-02-10 04:10:11 +04:00
|
|
|
|
|
|
|
# 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
|
2002-06-27 13:30:47 +04:00
|
|
|
|
2011-06-17 02:38:48 +04:00
|
|
|
# Returns the group most recently added to the stack.
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
|
|
|
# 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>
|
2002-06-27 13:30:47 +04:00
|
|
|
def current_group
|
|
|
|
@group_stack.last
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2011-06-17 02:38:48 +04:00
|
|
|
# Breaks the buffer into lines that are shorter than #maxwidth
|
2002-06-27 10:27:20 +04:00
|
|
|
def break_outmost_groups
|
|
|
|
while @maxwidth < @output_width + @buffer_width
|
|
|
|
return unless group = @group_queue.deq
|
|
|
|
until group.breakables.empty?
|
2002-06-27 16:01:07 +04:00
|
|
|
data = @buffer.shift
|
|
|
|
@output_width = data.output(@output, @output_width)
|
|
|
|
@buffer_width -= data.width
|
2002-06-27 10:27:20 +04:00
|
|
|
end
|
|
|
|
while !@buffer.empty? && Text === @buffer.first
|
2002-06-27 16:01:07 +04:00
|
|
|
text = @buffer.shift
|
|
|
|
@output_width = text.output(@output, @output_width)
|
|
|
|
@buffer_width -= text.width
|
2002-06-27 10:27:20 +04:00
|
|
|
end
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# This adds +obj+ as a text of +width+ columns in width.
|
|
|
|
#
|
|
|
|
# If +width+ is not specified, obj.length is used.
|
|
|
|
#
|
2002-06-27 10:27:20 +04:00
|
|
|
def text(obj, width=obj.length)
|
|
|
|
if @buffer.empty?
|
|
|
|
@output << obj
|
|
|
|
@output_width += width
|
|
|
|
else
|
|
|
|
text = @buffer.last
|
|
|
|
unless Text === text
|
|
|
|
text = Text.new
|
2002-06-27 16:01:07 +04:00
|
|
|
@buffer << text
|
2002-06-27 10:27:20 +04:00
|
|
|
end
|
|
|
|
text.add(obj, width)
|
|
|
|
@buffer_width += width
|
|
|
|
break_outmost_groups
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2011-08-02 19:25:59 +04:00
|
|
|
# This is similar to #breakable except
|
|
|
|
# the decision to break or not is determined individually.
|
|
|
|
#
|
|
|
|
# Two #fill_breakable under a group may cause 4 results:
|
|
|
|
# (break,break), (break,non-break), (non-break,break), (non-break,non-break).
|
|
|
|
# This is different to #breakable because two #breakable under a group
|
|
|
|
# may cause 2 results:
|
|
|
|
# (break,break), (non-break,non-break).
|
2011-06-17 02:38:48 +04:00
|
|
|
#
|
2013-02-10 04:10:11 +04:00
|
|
|
# The text +sep+ is inserted if a line is not broken at this point.
|
2011-06-17 02:38:48 +04:00
|
|
|
#
|
|
|
|
# If +sep+ is not specified, " " is used.
|
|
|
|
#
|
|
|
|
# If +width+ is not specified, +sep.length+ is used. You will have to
|
|
|
|
# specify this when +sep+ is a multibyte character, for example.
|
|
|
|
#
|
2002-06-27 16:01:07 +04:00
|
|
|
def fill_breakable(sep=' ', width=sep.length)
|
|
|
|
group { breakable sep, width }
|
|
|
|
end
|
|
|
|
|
2011-06-17 02:38:48 +04:00
|
|
|
# This says "you can break a line here if necessary", and a +width+\-column
|
2006-07-17 22:22:16 +04:00
|
|
|
# text +sep+ is inserted if a line is not broken at the point.
|
|
|
|
#
|
|
|
|
# If +sep+ is not specified, " " is used.
|
|
|
|
#
|
|
|
|
# If +width+ is not specified, +sep.length+ is used. You will have to
|
|
|
|
# specify this when +sep+ is a multibyte character, for example.
|
|
|
|
#
|
2002-06-27 10:27:20 +04:00
|
|
|
def breakable(sep=' ', width=sep.length)
|
|
|
|
group = @group_stack.last
|
|
|
|
if group.break?
|
|
|
|
flush
|
|
|
|
@output << @newline
|
|
|
|
@output << @genspace.call(@indent)
|
|
|
|
@output_width = @indent
|
|
|
|
@buffer_width = 0
|
|
|
|
else
|
2002-06-27 13:30:47 +04:00
|
|
|
@buffer << Breakable.new(sep, width, self)
|
2002-06-27 10:27:20 +04:00
|
|
|
@buffer_width += width
|
|
|
|
break_outmost_groups
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# Groups line break hints added in the block. The line break hints are all
|
|
|
|
# to be used or not.
|
|
|
|
#
|
|
|
|
# If +indent+ is specified, the method call is regarded as nested by
|
|
|
|
# nest(indent) { ... }.
|
|
|
|
#
|
|
|
|
# If +open_obj+ is specified, <tt>text open_obj, open_width</tt> is called
|
|
|
|
# before grouping. If +close_obj+ is specified, <tt>text close_obj,
|
|
|
|
# close_width</tt> is called after grouping.
|
|
|
|
#
|
2002-06-27 16:01:07 +04:00
|
|
|
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 {
|
|
|
|
nest(indent) {
|
|
|
|
yield
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text close_obj, close_width
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Takes a block and queues a new group that is indented 1 level further.
|
2002-06-27 16:01:07 +04:00
|
|
|
def group_sub
|
2002-06-27 10:27:20 +04:00
|
|
|
group = Group.new(@group_stack.last.depth + 1)
|
|
|
|
@group_stack.push group
|
|
|
|
@group_queue.enq group
|
2001-12-24 20:38:33 +03:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2002-06-27 10:27:20 +04:00
|
|
|
@group_stack.pop
|
2002-06-27 13:30:47 +04:00
|
|
|
if group.breakables.empty?
|
|
|
|
@group_queue.delete group
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# Increases left margin after newline with +indent+ for line breaks added in
|
|
|
|
# the block.
|
|
|
|
#
|
2002-06-27 10:27:20 +04:00
|
|
|
def nest(indent)
|
|
|
|
@indent += indent
|
2002-02-22 05:31:46 +03:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2002-06-27 10:27:20 +04:00
|
|
|
@indent -= indent
|
2002-02-22 05:31:46 +03:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2006-07-17 22:22:16 +04:00
|
|
|
# outputs buffered data.
|
|
|
|
#
|
2002-06-27 10:27:20 +04:00
|
|
|
def flush
|
|
|
|
@buffer.each {|data|
|
2002-06-27 13:30:47 +04:00
|
|
|
@output_width = data.output(@output, @output_width)
|
2002-06-27 10:27:20 +04:00
|
|
|
}
|
|
|
|
@buffer.clear
|
|
|
|
@buffer_width = 0
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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.
|
|
|
|
#
|
2013-05-19 07:10:21 +04:00
|
|
|
# This constructor takes no arguments.
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
|
|
|
# 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.
|
2002-06-27 10:27:20 +04:00
|
|
|
def initialize
|
|
|
|
@objs = []
|
|
|
|
@width = 0
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
2013-02-10 04:10:11 +04:00
|
|
|
|
|
|
|
# The total width of the objects included in this Text object.
|
2002-06-27 10:27:20 +04:00
|
|
|
attr_reader :width
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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+
|
2002-06-27 13:30:47 +04:00
|
|
|
def output(out, output_width)
|
2002-06-27 10:27:20 +04:00
|
|
|
@objs.each {|obj| out << obj}
|
|
|
|
output_width + @width
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Include +obj+ in the objects to be pretty printed, and increment
|
|
|
|
# this Text object's total width by +width+
|
2002-06-27 10:27:20 +04:00
|
|
|
def add(obj, width)
|
|
|
|
@objs << obj
|
|
|
|
@width += width
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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:
|
2013-05-19 07:10:21 +04:00
|
|
|
# * +sep+ String of the separator
|
2013-02-10 04:10:11 +04:00
|
|
|
# * +width+ Fixnum width of the +sep+
|
|
|
|
# * +q+ parent PrettyPrint object, to base from
|
2003-12-16 15:22:15 +03:00
|
|
|
def initialize(sep, width, q)
|
2002-06-27 10:27:20 +04:00
|
|
|
@obj = sep
|
2001-12-24 20:38:33 +03:00
|
|
|
@width = width
|
2003-12-16 15:22:15 +03:00
|
|
|
@pp = q
|
|
|
|
@indent = q.indent
|
|
|
|
@group = q.current_group
|
2002-06-27 10:27:20 +04:00
|
|
|
@group.breakables.push self
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-05-19 07:10:21 +04:00
|
|
|
# Holds the separator String
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
|
|
|
# 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+
|
2002-06-27 13:30:47 +04:00
|
|
|
def output(out, output_width)
|
2002-06-27 10:27:20 +04:00
|
|
|
@group.breakables.shift
|
|
|
|
if @group.break?
|
2002-06-27 16:01:07 +04:00
|
|
|
out << @pp.newline
|
|
|
|
out << @pp.genspace.call(@indent)
|
|
|
|
@indent
|
2001-12-24 20:38:33 +03:00
|
|
|
else
|
2002-06-27 16:01:07 +04:00
|
|
|
@pp.group_queue.delete @group if @group.breakables.empty?
|
|
|
|
out << @obj
|
|
|
|
output_width + @width
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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
|
2002-06-27 10:27:20 +04:00
|
|
|
def initialize(depth)
|
|
|
|
@depth = depth
|
|
|
|
@breakables = []
|
|
|
|
@break = false
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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
|
2002-06-27 10:27:20 +04:00
|
|
|
def break
|
|
|
|
@break = true
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Boolean of whether this Group has made a break
|
2002-06-27 10:27:20 +04:00
|
|
|
def break?
|
|
|
|
@break
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
2002-06-27 16:01:07 +04:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Boolean of whether this Group has been queried for being first
|
|
|
|
#
|
|
|
|
# This is used as a predicate, and ought to be called first.
|
2002-06-27 16:01:07 +04:00
|
|
|
def first?
|
|
|
|
if defined? @first
|
|
|
|
false
|
|
|
|
else
|
|
|
|
@first = false
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2002-06-27 10:27:20 +04:00
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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
|
2002-06-27 10:27:20 +04:00
|
|
|
def initialize(*groups)
|
|
|
|
@queue = []
|
|
|
|
groups.each {|g| enq g}
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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+
|
2002-06-27 10:27:20 +04:00
|
|
|
def enq(group)
|
|
|
|
depth = group.depth
|
|
|
|
@queue << [] until depth < @queue.length
|
|
|
|
@queue[depth] << group
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Returns the outer group of the queue
|
2002-06-27 10:27:20 +04:00
|
|
|
def deq
|
|
|
|
@queue.each {|gs|
|
2002-06-27 16:01:07 +04:00
|
|
|
(gs.length-1).downto(0) {|i|
|
|
|
|
unless gs[i].breakables.empty?
|
|
|
|
group = gs.slice!(i, 1).first
|
|
|
|
group.break
|
|
|
|
return group
|
|
|
|
end
|
|
|
|
}
|
|
|
|
gs.each {|group| group.break}
|
|
|
|
gs.clear
|
2002-02-22 05:31:46 +03:00
|
|
|
}
|
2002-06-27 10:27:20 +04:00
|
|
|
return nil
|
2002-02-22 05:31:46 +03:00
|
|
|
end
|
2002-06-27 13:30:47 +04:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Remote +group+ from this queue
|
2002-06-27 13:30:47 +04:00
|
|
|
def delete(group)
|
|
|
|
@queue[group.depth].delete(group)
|
|
|
|
end
|
2002-02-22 05:31:46 +03:00
|
|
|
end
|
2002-10-08 05:58:34 +04:00
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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
|
|
|
|
#
|
2002-10-08 05:58:34 +04:00
|
|
|
class SingleLine
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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.
|
2002-10-08 05:58:34 +04:00
|
|
|
def initialize(output, maxwidth=nil, newline=nil)
|
|
|
|
@output = output
|
|
|
|
@first = [true]
|
|
|
|
end
|
|
|
|
|
2013-05-19 07:10:21 +04:00
|
|
|
# Add +obj+ to the text to be output.
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
|
|
|
# +width+ argument is here for compatibility. It is a noop argument.
|
2002-10-08 05:58:34 +04:00
|
|
|
def text(obj, width=nil)
|
|
|
|
@output << obj
|
|
|
|
end
|
|
|
|
|
2013-05-19 07:10:21 +04:00
|
|
|
# Appends +sep+ to the text to be output. By default +sep+ is ' '
|
2013-02-10 04:10:11 +04:00
|
|
|
#
|
|
|
|
# +width+ argument is here for compatibility. It is a noop argument.
|
2002-10-08 05:58:34 +04:00
|
|
|
def breakable(sep=' ', width=nil)
|
|
|
|
@output << sep
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Takes +indent+ arg, but does nothing with it.
|
|
|
|
#
|
|
|
|
# Yields to a block.
|
|
|
|
def nest(indent) # :nodoc:
|
2002-10-08 05:58:34 +04:00
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# 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.
|
2002-10-08 05:58:34 +04:00
|
|
|
def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil)
|
|
|
|
@first.push true
|
|
|
|
@output << open_obj
|
|
|
|
yield
|
|
|
|
@output << close_obj
|
|
|
|
@first.pop
|
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# Method present for compatibility, but is a noop
|
|
|
|
def flush # :nodoc:
|
2002-10-08 05:58:34 +04:00
|
|
|
end
|
|
|
|
|
2013-02-10 04:10:11 +04:00
|
|
|
# This is used as a predicate, and ought to be called first.
|
2002-10-08 05:58:34 +04:00
|
|
|
def first?
|
|
|
|
result = @first[-1]
|
|
|
|
@first[-1] = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
2001-12-24 20:38:33 +03:00
|
|
|
end
|