class PrettyPrint
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: * 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?
Report any bugs at bugs.ruby-lang.org
References
Christian Lindig, Strictly Pretty, March 2000, lindig.github.io/papers/strictly-pretty-2000.pdf
Philip Wadler, A prettier printer, March 1998, homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier
Author
Tanaka Akira akr@fsij.org
Attributes
The PrettyPrint::GroupQueue of groups in stack to be pretty printed
The number of spaces to be indented
The maximum width of a line, before it is separated in to a newline
This defaults to 79, and should be an Integer
The value that is appended to output to add a new line.
This defaults to βnβ, and should be String
_Output
The output object.
This defaults to β, and should accept the << method
Public Class Methods
(?untyped output, ?Integer maxwidth, ?String newline, ?^(Integer) → Integer genspace) { (PrettyPrint) → untyped } → _Output
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 51
def self.format: (?untyped output, ?Integer maxwidth, ?String newline, ?^(Integer) -> Integer genspace) { (PrettyPrint) -> untyped } -> _Output
This is a convenience method which is same as follows:
begin q = PrettyPrint.new(output, maxwidth, newline, &genspace) ... q.flush output end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 87
def initialize: (?untyped output, ?Integer maxwidth, ?String newline, ?^(Integer) -> Integer genspace) -> void
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.
(?untyped output, ?Integer? maxwidth, ?String? newline, ?^(Integer) → Integer? genspace) { (PrettyPrint::SingleLine) → untyped } → _Output
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 64
def self.singleline_format: (?untyped output, ?Integer? maxwidth, ?String? newline, ?^(Integer) -> Integer? genspace) { (PrettyPrint::SingleLine) -> untyped } -> _Output
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
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 171
def break_outmost_groups: () -> untyped
Breaks the buffer into lines that are shorter than maxwidth
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 216
def breakable: (?String sep, ?Integer width) -> void
This says β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.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 163
def current_group: () -> PrettyPrint::Group
Returns the group most recently added to the stack.
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>
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 202
def fill_breakable: (?String sep, ?Integer width) -> void
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).
The text sep is inserted if a line is not broken at this 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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 257
def flush: () -> Integer
outputs buffered data.
(?::Integer indent, ?::String open_obj, ?::String close_obj, ?Integer open_width, ?Integer close_width) { () → untyped } → Integer
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 232
def group: (?::Integer indent, ?::String open_obj, ?::String close_obj, ?Integer open_width, ?Integer close_width) { () -> untyped } -> Integer
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.
() { () → untyped } → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 240
def group_sub: () { () -> untyped } -> untyped
Takes a block and queues a new group that is indented 1 level further.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/prettyprint/0/prettyprint.rbs, line 249
def nest: (Integer indent) { () -> untyped } -> void
Increases left margin after newline with indent for line breaks added in the block.