Class PrettyPrint
In: lib/prettyprint.rb
Parent: Object

This class implements a pretty printing algorithm. It finds line breaks and nice indentations for grouped structure.

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:

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?

References

Christian Lindig, Strictly Pretty, March 2000, www.st.cs.uni-sb.de/~lindig/papers/#pretty

Philip Wadler, A prettier printer, March 1998, homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier

Author

Tanaka Akira <akr@m17n.org>

Methods

Classes and Modules

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

Attributes

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

Public Class methods

This is a convenience method which is same as follows:

  begin
    q = PrettyPrint.new(output, maxwidth, newline, &genspace)
    ...
    q.flush
    output
  end

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.

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.

Public Instance methods

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.

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.

outputs buffered data.

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, text open_obj, open_width is called before grouping. If close_obj is specified, text close_obj, close_width is called after grouping.

Increases left margin after newline with indent for line breaks added in the block.

This adds obj as a text of width columns in width.

If width is not specified, obj.length is used.

[Validate]

ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.