In: |
prettyprint.rb
|
Parent: | Object |
genspace | [R] | |
group_queue | [R] | |
indent | [R] | |
maxwidth | [R] | |
newline | [R] | |
output | [R] |
# File prettyprint.rb, line 127 def PrettyPrint.format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) pp = PrettyPrint.new(output, maxwidth, newline, &genspace) yield pp pp.flush output end
# File prettyprint.rb, line 134 def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil) pp = SingleLine.new(output) yield pp output end
# File prettyprint.rb, line 140 def initialize(output='', maxwidth=79, newline="\n", &genspace) @output = output @maxwidth = maxwidth @newline = newline @genspace = genspace || lambda {|n| ' ' * n} @output_width = 0 @buffer_width = 0 @buffer = [] root_group = Group.new(0) @group_stack = [root_group] @group_queue = GroupQueue.new(root_group) @indent = 0 end
# File prettyprint.rb, line 166 def break_outmost_groups while @maxwidth < @output_width + @buffer_width return unless group = @group_queue.deq until group.breakables.empty? data = @buffer.shift @output_width = data.output(@output, @output_width) @buffer_width -= data.width end while !@buffer.empty? && Text === @buffer.first text = @buffer.shift @output_width = text.output(@output, @output_width) @buffer_width -= text.width end end end
# File prettyprint.rb, line 182 def text(obj, width=obj.length) if @buffer.empty? @output << obj @output_width += width else text = @buffer.last unless Text === text text = Text.new @buffer << text end text.add(obj, width) @buffer_width += width break_outmost_groups end end
# File prettyprint.rb, line 198 def fill_breakable(sep=' ', width=sep.length) group { breakable sep, width } end
# File prettyprint.rb, line 202 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 @buffer << Breakable.new(sep, width, self) @buffer_width += width break_outmost_groups end end
# File prettyprint.rb, line 217 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
# File prettyprint.rb, line 227 def group_sub group = Group.new(@group_stack.last.depth + 1) @group_stack.push group @group_queue.enq group begin yield ensure @group_stack.pop if group.breakables.empty? @group_queue.delete group end end end