# PrettyPrint (Class)

In: | prettyprint.rb
Parent: | Object

## Methods

break_outmost_groups breakable current_group fill_breakable first? flush format group group_sub nest new singleline_format text

## Attributes

genspace | [R] |
group_queue | [R] |
indent | [R] |
maxwidth | [R] |
newline | [R] |
output | [R] |

## Classes and Modules

Class PrettyPrint::Breakable
Class PrettyPrint::Group
Class PrettyPrint::GroupQueue
Class PrettyPrint::SingleLine
Class PrettyPrint::Text

## Public Class methods

format(output='', maxwidth=79, newline="\n", genspace=lambda {|n| ' ' * n}) {|pp| ...}

[Source]

    # 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

singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil) {|pp| ...}

[Source]

    # File prettyprint.rb, line 134
      def PrettyPrint.singleline_format(output='', maxwidth=nil, newline=nil, genspace=nil)
        pp = SingleLine.new(output)
        yield pp
        output
      end

new(output='', maxwidth=79, newline="\n", &genspace)

[Source]

    # 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

## Public Instance methods

current_group()

[Source]

    # File prettyprint.rb, line 158
      def current_group
        @group_stack.last
      end

first?()

[Source]

    # File prettyprint.rb, line 162
      def first?
        current_group.first?
      end

break_outmost_groups()

[Source]

    # 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

text(obj, width=obj.length)

[Source]

    # 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

fill_breakable(sep=' ', width=sep.length)

[Source]

    # File prettyprint.rb, line 198
      def fill_breakable(sep=' ', width=sep.length)
        group { breakable sep, width }
      end

breakable(sep=' ', width=sep.length)

[Source]

    # 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

group(indent=0, open_obj='', close_obj='', open_width=open_obj.length, close_width=close_obj.length) {|| ...}

[Source]

    # 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

group_sub() {|| ...}

[Source]

    # 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

nest(indent) {|| ...}

[Source]

    # File prettyprint.rb, line 241
      def nest(indent)
        @indent += indent
        begin
          yield
        ensure
          @indent -= indent
        end
      end

flush()

[Source]

    # File prettyprint.rb, line 250
      def flush
        @buffer.each {|data|
          @output_width = data.output(@output, @output_width)
        }
        @buffer.clear
        @buffer_width = 0
      end

[Validate]