2001-12-24 12:38:33 -05:00
|
|
|
# $Id$
|
|
|
|
|
2006-07-17 14:22:16 -04:00
|
|
|
# This class implements a pretty printing algorithm. It finds line breaks and
|
|
|
|
# nice indentations for grouped structure.
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2006-07-17 14: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 11:18:53 -04:00
|
|
|
#
|
2006-07-17 14:22:16 -04:00
|
|
|
# == References
|
|
|
|
# Christian Lindig, Strictly Pretty, March 2000,
|
|
|
|
# http://www.st.cs.uni-sb.de/~lindig/papers/#pretty
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2006-07-17 14: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 11:18:53 -04:00
|
|
|
#
|
2006-07-17 14:22:16 -04:00
|
|
|
# == Author
|
|
|
|
# Tanaka Akira <akr@m17n.org>
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2001-12-24 12:38:33 -05:00
|
|
|
class PrettyPrint
|
2006-07-17 14:22:16 -04:00
|
|
|
|
|
|
|
# This is a convenience method which is same as follows:
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2006-07-17 14:22:16 -04:00
|
|
|
# begin
|
|
|
|
# q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
|
|
|
# ...
|
|
|
|
# q.flush
|
|
|
|
# output
|
|
|
|
# end
|
2008-09-14 11:18:53 -04:00
|
|
|
#
|
2002-06-27 08:01:07 -04:00
|
|
|
def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n})
|
2003-12-16 07:22:15 -05:00
|
|
|
q = PrettyPrint.new(output, maxwidth, newline, &genspace)
|
|
|
|
yield q
|
|
|
|
q.flush
|
2002-06-27 08:01:07 -04:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2006-07-17 14: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+.
|
|
|
|
#
|
2002-10-07 21:58:34 -04:00
|
|
|
def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
|
2003-12-16 07:22:15 -05:00
|
|
|
q = SingleLine.new(output)
|
|
|
|
yield q
|
2002-10-07 21:58:34 -04:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2006-07-17 14: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.
|
|
|
|
#
|
2002-06-27 08:01:07 -04:00
|
|
|
def initialize(output='', maxwidth=79, newline="\n", &genspace)
|
2002-06-27 02:27:20 -04:00
|
|
|
@output = output
|
|
|
|
@maxwidth = maxwidth
|
2001-12-24 12:38:33 -05:00
|
|
|
@newline = newline
|
|
|
|
@genspace = genspace || lambda {|n| ' ' * n}
|
2002-06-27 02: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 12:38:33 -05:00
|
|
|
end
|
2002-06-27 05:30:47 -04:00
|
|
|
attr_reader :output, :maxwidth, :newline, :genspace
|
|
|
|
attr_reader :indent, :group_queue
|
|
|
|
|
|
|
|
def current_group
|
|
|
|
@group_stack.last
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2006-07-17 14:22:16 -04:00
|
|
|
# first? is a predicate to test the call is a first call to first? with
|
|
|
|
# current group.
|
|
|
|
#
|
|
|
|
# It is useful to format comma separated values as:
|
|
|
|
#
|
|
|
|
# q.group(1, '[', ']') {
|
|
|
|
# xxx.each {|yyy|
|
|
|
|
# unless q.first?
|
|
|
|
# q.text ','
|
|
|
|
# q.breakable
|
|
|
|
# end
|
|
|
|
# ... pretty printing yyy ...
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# first? is obsoleted in 1.8.2.
|
|
|
|
#
|
2002-06-27 08:01:07 -04:00
|
|
|
def first?
|
2004-02-05 10:50:43 -05:00
|
|
|
warn "PrettyPrint#first? is obsoleted at 1.8.2."
|
2002-06-27 08:01:07 -04:00
|
|
|
current_group.first?
|
|
|
|
end
|
|
|
|
|
2002-06-27 02: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 08:01:07 -04:00
|
|
|
data = @buffer.shift
|
|
|
|
@output_width = data.output(@output, @output_width)
|
|
|
|
@buffer_width -= data.width
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
while !@buffer.empty? && Text === @buffer.first
|
2002-06-27 08:01:07 -04:00
|
|
|
text = @buffer.shift
|
|
|
|
@output_width = text.output(@output, @output_width)
|
|
|
|
@buffer_width -= text.width
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2006-07-17 14: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 02: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 08:01:07 -04:00
|
|
|
@buffer << text
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
text.add(obj, width)
|
|
|
|
@buffer_width += width
|
|
|
|
break_outmost_groups
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 08:01:07 -04:00
|
|
|
def fill_breakable(sep=' ', width=sep.length)
|
|
|
|
group { breakable sep, width }
|
|
|
|
end
|
|
|
|
|
2006-07-17 14:22:16 -04:00
|
|
|
# This tells "you can break a line here if necessary", and a +width+\-column
|
|
|
|
# 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 02: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 05:30:47 -04:00
|
|
|
@buffer << Breakable.new(sep, width, self)
|
2002-06-27 02:27:20 -04:00
|
|
|
@buffer_width += width
|
|
|
|
break_outmost_groups
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-17 14: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 08: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
|
|
|
|
|
|
|
|
def group_sub
|
2002-06-27 02:27:20 -04:00
|
|
|
group = Group.new(@group_stack.last.depth + 1)
|
|
|
|
@group_stack.push group
|
|
|
|
@group_queue.enq group
|
2001-12-24 12:38:33 -05:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2002-06-27 02:27:20 -04:00
|
|
|
@group_stack.pop
|
2002-06-27 05:30:47 -04:00
|
|
|
if group.breakables.empty?
|
|
|
|
@group_queue.delete group
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-07-17 14:22:16 -04:00
|
|
|
# Increases left margin after newline with +indent+ for line breaks added in
|
|
|
|
# the block.
|
|
|
|
#
|
2002-06-27 02:27:20 -04:00
|
|
|
def nest(indent)
|
|
|
|
@indent += indent
|
2002-02-21 21:31:46 -05:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2002-06-27 02:27:20 -04:00
|
|
|
@indent -= indent
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2006-07-17 14:22:16 -04:00
|
|
|
# outputs buffered data.
|
|
|
|
#
|
2002-06-27 02:27:20 -04:00
|
|
|
def flush
|
|
|
|
@buffer.each {|data|
|
2002-06-27 05:30:47 -04:00
|
|
|
@output_width = data.output(@output, @output_width)
|
2002-06-27 02:27:20 -04:00
|
|
|
}
|
|
|
|
@buffer.clear
|
|
|
|
@buffer_width = 0
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Text
|
2002-06-27 02:27:20 -04:00
|
|
|
def initialize
|
|
|
|
@objs = []
|
|
|
|
@width = 0
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2002-06-27 02:27:20 -04:00
|
|
|
attr_reader :width
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-06-27 05:30:47 -04:00
|
|
|
def output(out, output_width)
|
2002-06-27 02:27:20 -04:00
|
|
|
@objs.each {|obj| out << obj}
|
|
|
|
output_width + @width
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def add(obj, width)
|
|
|
|
@objs << obj
|
|
|
|
@width += width
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Breakable
|
2003-12-16 07:22:15 -05:00
|
|
|
def initialize(sep, width, q)
|
2002-06-27 02:27:20 -04:00
|
|
|
@obj = sep
|
2001-12-24 12:38:33 -05:00
|
|
|
@width = width
|
2003-12-16 07:22:15 -05:00
|
|
|
@pp = q
|
|
|
|
@indent = q.indent
|
|
|
|
@group = q.current_group
|
2002-06-27 02:27:20 -04:00
|
|
|
@group.breakables.push self
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2002-06-27 02:27:20 -04:00
|
|
|
attr_reader :obj, :width, :indent
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-06-27 05:30:47 -04:00
|
|
|
def output(out, output_width)
|
2002-06-27 02:27:20 -04:00
|
|
|
@group.breakables.shift
|
|
|
|
if @group.break?
|
2002-06-27 08:01:07 -04:00
|
|
|
out << @pp.newline
|
|
|
|
out << @pp.genspace.call(@indent)
|
|
|
|
@indent
|
2001-12-24 12:38:33 -05:00
|
|
|
else
|
2002-06-27 08:01:07 -04:00
|
|
|
@pp.group_queue.delete @group if @group.breakables.empty?
|
|
|
|
out << @obj
|
|
|
|
output_width + @width
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Group
|
2002-06-27 02:27:20 -04:00
|
|
|
def initialize(depth)
|
|
|
|
@depth = depth
|
|
|
|
@breakables = []
|
|
|
|
@break = false
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2002-06-27 02:27:20 -04:00
|
|
|
attr_reader :depth, :breakables
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def break
|
|
|
|
@break = true
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def break?
|
|
|
|
@break
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
2002-06-27 08:01:07 -04:00
|
|
|
|
|
|
|
def first?
|
|
|
|
if defined? @first
|
|
|
|
false
|
|
|
|
else
|
|
|
|
@first = false
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
class GroupQueue
|
|
|
|
def initialize(*groups)
|
|
|
|
@queue = []
|
|
|
|
groups.each {|g| enq g}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def enq(group)
|
|
|
|
depth = group.depth
|
|
|
|
@queue << [] until depth < @queue.length
|
|
|
|
@queue[depth] << group
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def deq
|
|
|
|
@queue.each {|gs|
|
2002-06-27 08: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-21 21:31:46 -05:00
|
|
|
}
|
2002-06-27 02:27:20 -04:00
|
|
|
return nil
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
2002-06-27 05:30:47 -04:00
|
|
|
|
|
|
|
def delete(group)
|
|
|
|
@queue[group.depth].delete(group)
|
|
|
|
end
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
2002-10-07 21:58:34 -04:00
|
|
|
|
|
|
|
class SingleLine
|
|
|
|
def initialize(output, maxwidth=nil, newline=nil)
|
|
|
|
@output = output
|
|
|
|
@first = [true]
|
|
|
|
end
|
|
|
|
|
|
|
|
def text(obj, width=nil)
|
|
|
|
@output << obj
|
|
|
|
end
|
|
|
|
|
|
|
|
def breakable(sep=' ', width=nil)
|
|
|
|
@output << sep
|
|
|
|
end
|
|
|
|
|
|
|
|
def nest(indent)
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def flush
|
|
|
|
end
|
|
|
|
|
|
|
|
def first?
|
|
|
|
result = @first[-1]
|
|
|
|
@first[-1] = false
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
2003-01-21 00:51:41 -05:00
|
|
|
require 'test/unit'
|
2001-12-24 12:38:33 -05:00
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class WadlerExample < Test::Unit::TestCase # :nodoc:
|
2001-12-24 12:38:33 -05:00
|
|
|
def setup
|
|
|
|
@tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"),
|
2002-06-27 08:01:07 -04:00
|
|
|
Tree.new("dd")),
|
|
|
|
Tree.new("eee"),
|
|
|
|
Tree.new("ffff", Tree.new("gg"),
|
|
|
|
Tree.new("hhh"),
|
|
|
|
Tree.new("ii")))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def hello(width)
|
2002-06-27 08:01:07 -04:00
|
|
|
PrettyPrint.format('', width) {|hello|
|
|
|
|
hello.group {
|
|
|
|
hello.group {
|
|
|
|
hello.group {
|
|
|
|
hello.group {
|
|
|
|
hello.text 'hello'
|
|
|
|
hello.breakable; hello.text 'a'
|
|
|
|
}
|
|
|
|
hello.breakable; hello.text 'b'
|
|
|
|
}
|
|
|
|
hello.breakable; hello.text 'c'
|
|
|
|
}
|
|
|
|
hello.breakable; hello.text 'd'
|
|
|
|
}
|
|
|
|
}
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
|
2001-12-24 12:38:33 -05:00
|
|
|
def test_hello_00_06
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
hello
|
|
|
|
a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
d
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, hello(0))
|
|
|
|
assert_equal(expected, hello(6))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hello_07_08
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
hello a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
d
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, hello(7))
|
|
|
|
assert_equal(expected, hello(8))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hello_09_10
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
hello a b
|
|
|
|
c
|
|
|
|
d
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
out = hello(9); assert_equal(expected, out)
|
|
|
|
out = hello(10); assert_equal(expected, out)
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hello_11_12
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
hello a b c
|
|
|
|
d
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, hello(11))
|
|
|
|
assert_equal(expected, hello(12))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_hello_13
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
hello a b c d
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, hello(13))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def tree(width)
|
2003-12-16 07:22:15 -05:00
|
|
|
PrettyPrint.format('', width) {|q| @tree.show(q)}
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_00_19
|
2001-12-24 12:38:33 -05:00
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[bbbbb[ccc,
|
|
|
|
dd],
|
|
|
|
eee,
|
|
|
|
ffff[gg,
|
|
|
|
hhh,
|
|
|
|
ii]]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree(0))
|
|
|
|
assert_equal(expected, tree(19))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_20_22
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[bbbbb[ccc, dd],
|
|
|
|
eee,
|
|
|
|
ffff[gg,
|
|
|
|
hhh,
|
|
|
|
ii]]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree(20))
|
|
|
|
assert_equal(expected, tree(22))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_23_43
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[bbbbb[ccc, dd],
|
|
|
|
eee,
|
|
|
|
ffff[gg, hhh, ii]]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree(23))
|
|
|
|
assert_equal(expected, tree(43))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_44
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(<<'End'.chomp, tree(44))
|
2001-12-24 12:38:33 -05:00
|
|
|
aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]]
|
|
|
|
End
|
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def tree_alt(width)
|
2003-12-16 07:22:15 -05:00
|
|
|
PrettyPrint.format('', width) {|q| @tree.altshow(q)}
|
2002-06-27 02:27:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_alt_00_18
|
2001-12-24 12:38:33 -05:00
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[
|
|
|
|
bbbbb[
|
|
|
|
ccc,
|
|
|
|
dd
|
|
|
|
],
|
|
|
|
eee,
|
|
|
|
ffff[
|
|
|
|
gg,
|
|
|
|
hhh,
|
|
|
|
ii
|
|
|
|
]
|
|
|
|
]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree_alt(0))
|
|
|
|
assert_equal(expected, tree_alt(18))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_alt_19_20
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[
|
|
|
|
bbbbb[ ccc, dd ],
|
|
|
|
eee,
|
|
|
|
ffff[
|
|
|
|
gg,
|
|
|
|
hhh,
|
|
|
|
ii
|
|
|
|
]
|
|
|
|
]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree_alt(19))
|
|
|
|
assert_equal(expected, tree_alt(20))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_alt_20_49
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[
|
|
|
|
bbbbb[ ccc, dd ],
|
|
|
|
eee,
|
|
|
|
ffff[ gg, hhh, ii ]
|
|
|
|
]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree_alt(21))
|
|
|
|
assert_equal(expected, tree_alt(49))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tree_alt_50
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ]
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, tree_alt(50))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class Tree # :nodoc:
|
2001-12-24 12:38:33 -05:00
|
|
|
def initialize(string, *children)
|
|
|
|
@string = string
|
2002-06-27 08:01:07 -04:00
|
|
|
@children = children
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2003-12-16 07:22:15 -05:00
|
|
|
def show(q)
|
|
|
|
q.group {
|
|
|
|
q.text @string
|
|
|
|
q.nest(@string.length) {
|
2002-06-27 08:01:07 -04:00
|
|
|
unless @children.empty?
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text '['
|
|
|
|
q.nest(1) {
|
2002-06-27 08:01:07 -04:00
|
|
|
first = true
|
|
|
|
@children.each {|t|
|
|
|
|
if first
|
|
|
|
first = false
|
|
|
|
else
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text ','
|
|
|
|
q.breakable
|
2002-06-27 08:01:07 -04:00
|
|
|
end
|
2003-12-16 07:22:15 -05:00
|
|
|
t.show(q)
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text ']'
|
2002-06-27 08:01:07 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
2003-12-16 07:22:15 -05:00
|
|
|
def altshow(q)
|
|
|
|
q.group {
|
|
|
|
q.text @string
|
2002-06-27 08:01:07 -04:00
|
|
|
unless @children.empty?
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text '['
|
|
|
|
q.nest(2) {
|
|
|
|
q.breakable
|
2002-06-27 08:01:07 -04:00
|
|
|
first = true
|
|
|
|
@children.each {|t|
|
|
|
|
if first
|
|
|
|
first = false
|
|
|
|
else
|
2003-12-16 07:22:15 -05:00
|
|
|
q.text ','
|
|
|
|
q.breakable
|
2002-06-27 08:01:07 -04:00
|
|
|
end
|
2003-12-16 07:22:15 -05:00
|
|
|
t.altshow(q)
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.breakable
|
|
|
|
q.text ']'
|
2002-06-27 08:01:07 -04:00
|
|
|
end
|
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class StrictPrettyExample < Test::Unit::TestCase # :nodoc:
|
2002-06-27 02:27:20 -04:00
|
|
|
def prog(width)
|
2003-12-16 07:22:15 -05:00
|
|
|
PrettyPrint.format('', width) {|q|
|
|
|
|
q.group {
|
|
|
|
q.group {q.nest(2) {
|
|
|
|
q.text "if"; q.breakable;
|
|
|
|
q.group {
|
|
|
|
q.nest(2) {
|
|
|
|
q.group {q.text "a"; q.breakable; q.text "=="}
|
|
|
|
q.breakable; q.text "b"}}}}
|
|
|
|
q.breakable
|
|
|
|
q.group {q.nest(2) {
|
|
|
|
q.text "then"; q.breakable;
|
|
|
|
q.group {
|
|
|
|
q.nest(2) {
|
|
|
|
q.group {q.text "a"; q.breakable; q.text "<<"}
|
|
|
|
q.breakable; q.text "2"}}}}
|
|
|
|
q.breakable
|
|
|
|
q.group {q.nest(2) {
|
|
|
|
q.text "else"; q.breakable;
|
|
|
|
q.group {
|
|
|
|
q.nest(2) {
|
|
|
|
q.group {q.text "a"; q.breakable; q.text "+"}
|
|
|
|
q.breakable; q.text "b"}}}}}
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_00_04
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if
|
|
|
|
a
|
|
|
|
==
|
|
|
|
b
|
|
|
|
then
|
|
|
|
a
|
|
|
|
<<
|
|
|
|
2
|
|
|
|
else
|
|
|
|
a
|
|
|
|
+
|
|
|
|
b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(0))
|
|
|
|
assert_equal(expected, prog(4))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_05
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if
|
|
|
|
a
|
|
|
|
==
|
|
|
|
b
|
|
|
|
then
|
|
|
|
a
|
|
|
|
<<
|
|
|
|
2
|
|
|
|
else
|
|
|
|
a +
|
|
|
|
b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(5))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_06
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if
|
|
|
|
a ==
|
|
|
|
b
|
|
|
|
then
|
|
|
|
a <<
|
|
|
|
2
|
|
|
|
else
|
|
|
|
a +
|
|
|
|
b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(6))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_07
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if
|
|
|
|
a ==
|
|
|
|
b
|
|
|
|
then
|
|
|
|
a <<
|
|
|
|
2
|
|
|
|
else
|
|
|
|
a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(7))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_08
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if
|
|
|
|
a == b
|
|
|
|
then
|
|
|
|
a << 2
|
|
|
|
else
|
|
|
|
a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(8))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_09
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if a == b
|
|
|
|
then
|
|
|
|
a << 2
|
|
|
|
else
|
|
|
|
a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(9))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_10
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if a == b
|
|
|
|
then
|
|
|
|
a << 2
|
|
|
|
else a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(10))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_11_31
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if a == b
|
|
|
|
then a << 2
|
|
|
|
else a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(11))
|
|
|
|
assert_equal(expected, prog(15))
|
|
|
|
assert_equal(expected, prog(31))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_32
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
if a == b then a << 2 else a + b
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, prog(32))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class TailGroup < Test::Unit::TestCase # :nodoc:
|
2001-12-24 12:38:33 -05:00
|
|
|
def test_1
|
2003-12-16 07:22:15 -05:00
|
|
|
out = PrettyPrint.format('', 10) {|q|
|
|
|
|
q.group {
|
|
|
|
q.group {
|
|
|
|
q.text "abc"
|
|
|
|
q.breakable
|
|
|
|
q.text "def"
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
2003-12-16 07:22:15 -05:00
|
|
|
q.group {
|
|
|
|
q.text "ghi"
|
|
|
|
q.breakable
|
|
|
|
q.text "jkl"
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
}
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal("abc defghi\njkl", out)
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class NonString < Test::Unit::TestCase # :nodoc:
|
2002-06-27 02:27:20 -04:00
|
|
|
def format(width)
|
2003-12-16 07:22:15 -05:00
|
|
|
PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q|
|
|
|
|
q.text(3, 3)
|
|
|
|
q.breakable(1, 1)
|
|
|
|
q.text(3, 3)
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_6
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal([3, "newline", "0 spaces", 3], format(6))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_7
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal([3, 1, 3], format(7))
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:23:15 -05:00
|
|
|
class Fill < Test::Unit::TestCase # :nodoc:
|
2002-06-27 02:27:20 -04:00
|
|
|
def format(width)
|
2003-12-16 07:22:15 -05:00
|
|
|
PrettyPrint.format('', width) {|q|
|
|
|
|
q.group {
|
|
|
|
q.text 'abc'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'def'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'ghi'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'jkl'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'mno'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'pqr'
|
|
|
|
q.fill_breakable
|
|
|
|
q.text 'stu'
|
2002-06-27 08:01:07 -04:00
|
|
|
}
|
2002-02-21 21:31:46 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def test_00_06
|
2002-02-21 21:31:46 -05:00
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc
|
|
|
|
def
|
|
|
|
ghi
|
|
|
|
jkl
|
|
|
|
mno
|
|
|
|
pqr
|
|
|
|
stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(0))
|
|
|
|
assert_equal(expected, format(6))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
2002-06-27 02:27:20 -04:00
|
|
|
def test_07_10
|
2002-02-21 21:31:46 -05:00
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def
|
|
|
|
ghi jkl
|
|
|
|
mno pqr
|
|
|
|
stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(7))
|
|
|
|
assert_equal(expected, format(10))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_11_14
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def ghi
|
|
|
|
jkl mno pqr
|
|
|
|
stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(11))
|
|
|
|
assert_equal(expected, format(14))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_15_18
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def ghi jkl
|
|
|
|
mno pqr stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(15))
|
|
|
|
assert_equal(expected, format(18))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_19_22
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def ghi jkl mno
|
|
|
|
pqr stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(19))
|
|
|
|
assert_equal(expected, format(22))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_23_26
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def ghi jkl mno pqr
|
|
|
|
stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(23))
|
|
|
|
assert_equal(expected, format(26))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_27
|
|
|
|
expected = <<'End'.chomp
|
|
|
|
abc def ghi jkl mno pqr stu
|
|
|
|
End
|
2002-06-27 02:27:20 -04:00
|
|
|
assert_equal(expected, format(27))
|
2002-02-21 21:31:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2001-12-24 12:38:33 -05:00
|
|
|
end
|