Documentation
¶
Overview ¶
Package prettyprint is a pure-Go (no cgo) reimplementation of Ruby's `prettyprint` standard library — the Wadler/Lindig pretty-printing engine that MRI's `pp` object inspector is built on.
It is a faithful, byte-identical port of MRI 4.0.5's prettyprint.rb. The engine collects a stream of text, breakable separators and groups, then lays it out so that each group is printed on a single line when it fits within a maximum width and is otherwise broken at its breakable points, with nesting (indentation) preserved. The companion SingleLine variant emits the same stream with no breaks at all (breakables become their separator text).
This package implements only the layout engine. The `pp` object inspector that consumes it lives in the host (go-embedded-ruby / rbgo); this library is the standalone, reusable backend it binds to.
References ¶
- Christian Lindig, Strictly Pretty, March 2000.
- Philip Wadler, A prettier printer, March 1998.
Index ¶
- Constants
- func DefaultGenSpace(n int) string
- func Format(maxwidth int, newline string, genspace GenSpace, fn func(*PrettyPrint)) string
- func FormatDefault(fn func(*PrettyPrint)) string
- func SingleLineFormat(fn func(*SingleLine)) string
- type GenSpace
- type PrettyPrint
- func (q *PrettyPrint) BreakOutmostGroups()
- func (q *PrettyPrint) Breakable(sep string, width int)
- func (q *PrettyPrint) BreakableString()
- func (q *PrettyPrint) CurrentGroup() *group
- func (q *PrettyPrint) FillBreakable(sep string, width int)
- func (q *PrettyPrint) FillBreakableString()
- func (q *PrettyPrint) Flush()
- func (q *PrettyPrint) Group(indent int, openObj string, openWidth int, closeObj string, closeWidth int, ...)
- func (q *PrettyPrint) GroupDefault(fn func())
- func (q *PrettyPrint) GroupSub(_ int, _ string, _ int, _ string, _ int, fn func())
- func (q *PrettyPrint) Indent() int
- func (q *PrettyPrint) Maxwidth() int
- func (q *PrettyPrint) Nest(indent int, fn func())
- func (q *PrettyPrint) NestedGroup(fn func())
- func (q *PrettyPrint) Newline() string
- func (q *PrettyPrint) String() string
- func (q *PrettyPrint) Text(obj string, width int)
- func (q *PrettyPrint) TextString(obj string)
- type SingleLine
- func (s *SingleLine) Breakable(sep string, _ int)
- func (s *SingleLine) BreakableString()
- func (s *SingleLine) First() bool
- func (s *SingleLine) Flush()
- func (s *SingleLine) Group(_ int, openObj string, _ int, closeObj string, _ int, fn func())
- func (s *SingleLine) GroupDefault(fn func())
- func (s *SingleLine) Nest(_ int, fn func())
- func (s *SingleLine) String() string
- func (s *SingleLine) Text(obj string, _ int)
- func (s *SingleLine) TextString(obj string)
Constants ¶
const VERSION = "0.2.0"
VERSION mirrors PrettyPrint::VERSION in MRI 4.0.5.
Variables ¶
This section is empty.
Functions ¶
func DefaultGenSpace ¶
DefaultGenSpace returns a string of n spaces — the default genspace block, `lambda {|n| ' ' * n}`.
func Format ¶
func Format(maxwidth int, newline string, genspace GenSpace, fn func(*PrettyPrint)) string
Format is the convenience entry point of PrettyPrint.format: it builds a buffer, runs fn against it, flushes and returns the rendered string.
func FormatDefault ¶
func FormatDefault(fn func(*PrettyPrint)) string
FormatDefault is Format with MRI's defaults (maxwidth 79, "\n", space genspace).
func SingleLineFormat ¶
func SingleLineFormat(fn func(*SingleLine)) string
SingleLineFormat is the convenience entry point of PrettyPrint.singleline_format: it runs fn against a SingleLine buffer (whose breakables never break) and returns the rendered string.
Types ¶
type GenSpace ¶
GenSpace generates the indentation string for a given number of columns. It corresponds to the `genspace` block of PrettyPrint.new. The default, DefaultGenSpace, returns n ASCII spaces.
type PrettyPrint ¶
type PrettyPrint struct {
// contains filtered or unexported fields
}
PrettyPrint is the layout buffer. It is the port of the PrettyPrint class: callers build a document with PrettyPrint.Text, PrettyPrint.Breakable, PrettyPrint.Group and PrettyPrint.Nest, then read it back with PrettyPrint.String (after PrettyPrint.Flush).
func New ¶
func New(maxwidth int, newline string, genspace GenSpace) *PrettyPrint
New creates a pretty-printing buffer.
maxwidth is the maximum line length (MRI default 79); outputs may still overflow it where a single non-breakable text is wider. newline is the line break string (default "\n"). genspace generates indentation; when nil, DefaultGenSpace is used. The zero values matching MRI's defaults are produced by NewDefault.
func NewDefault ¶
func NewDefault() *PrettyPrint
NewDefault creates a buffer with MRI's defaults: maxwidth 79, "\n" newline and the space-generating block.
func (*PrettyPrint) BreakOutmostGroups ¶
func (q *PrettyPrint) BreakOutmostGroups()
BreakOutmostGroups breaks buffered groups until the pending output fits within maxwidth, draining the breakables (and the trailing texts) of each broken group to the output. It is the port of PrettyPrint#break_outmost_groups.
func (*PrettyPrint) Breakable ¶
func (q *PrettyPrint) Breakable(sep string, width int)
Breakable records a candidate break that prints sep (width columns) when the enclosing group does not break. It is the port of PrettyPrint#breakable.
func (*PrettyPrint) BreakableString ¶
func (q *PrettyPrint) BreakableString()
BreakableString is Breakable with sep " " and width 1, the all-defaults form.
func (*PrettyPrint) CurrentGroup ¶
func (q *PrettyPrint) CurrentGroup() *group
CurrentGroup returns the group most recently pushed on the stack, the port of PrettyPrint#current_group.
func (*PrettyPrint) FillBreakable ¶
func (q *PrettyPrint) FillBreakable(sep string, width int)
FillBreakable groups a single breakable so the break decision is made individually at this point. It is the port of PrettyPrint#fill_breakable.
func (*PrettyPrint) FillBreakableString ¶
func (q *PrettyPrint) FillBreakableString()
FillBreakableString is FillBreakable with sep " " and width 1.
func (*PrettyPrint) Flush ¶
func (q *PrettyPrint) Flush()
Flush writes every buffered element to the output and empties the buffer. It is the port of PrettyPrint#flush.
func (*PrettyPrint) Group ¶
func (q *PrettyPrint) Group(indent int, openObj string, openWidth int, closeObj string, closeWidth int, fn func())
Group runs fn inside a new group nested by indent, optionally emitting openObj (openWidth columns) before and closeObj (closeWidth columns) after. It is the port of PrettyPrint#group.
func (*PrettyPrint) GroupDefault ¶
func (q *PrettyPrint) GroupDefault(fn func())
GroupDefault runs fn in a fresh group with no extra indent and no open/close text — the common `group { ... }` form.
func (*PrettyPrint) GroupSub ¶
GroupSub queues a new group one level deeper than the current one and runs fn inside it, removing the group again afterwards if it registered no breakables. It is the port of PrettyPrint#group_sub. The leading parameters mirror the rbgo binding's signature and are unused; callers normally pass zero/empty.
func (*PrettyPrint) Indent ¶
func (q *PrettyPrint) Indent() int
Indent reports the current indentation in columns.
func (*PrettyPrint) Maxwidth ¶
func (q *PrettyPrint) Maxwidth() int
Maxwidth reports the configured maximum line width.
func (*PrettyPrint) Nest ¶
func (q *PrettyPrint) Nest(indent int, fn func())
Nest increases the left margin by indent for the breaks added inside fn. It is the port of PrettyPrint#nest.
func (*PrettyPrint) NestedGroup ¶
func (q *PrettyPrint) NestedGroup(fn func())
NestedGroup is an alias of PrettyPrint.GroupDefault kept for the rbgo binding's NestedGroup entry point.
func (*PrettyPrint) Newline ¶
func (q *PrettyPrint) Newline() string
Newline reports the configured line-break string.
func (*PrettyPrint) String ¶
func (q *PrettyPrint) String() string
String returns the rendered output accumulated so far. Call PrettyPrint.Flush first to drain any still-buffered elements.
func (*PrettyPrint) Text ¶
func (q *PrettyPrint) Text(obj string, width int)
Text adds obj as a run of width columns. It is the port of PrettyPrint#text. Use PrettyPrint.TextString to default width to len(obj).
func (*PrettyPrint) TextString ¶
func (q *PrettyPrint) TextString(obj string)
TextString adds obj with its byte length as the width, matching MRI's `text(obj, width=obj.length)` default.
type SingleLine ¶
type SingleLine struct {
// contains filtered or unexported fields
}
SingleLine renders the same builder calls as PrettyPrint but never breaks a line: breakables emit their separator as plain text and groups/nests are transparent. It is the port of PrettyPrint::SingleLine.
func (*SingleLine) Breakable ¶
func (s *SingleLine) Breakable(sep string, _ int)
Breakable appends sep (no line break). width is ignored.
func (*SingleLine) BreakableString ¶
func (s *SingleLine) BreakableString()
BreakableString appends a single space.
func (*SingleLine) First ¶
func (s *SingleLine) First() bool
First reports whether this is the first query of the innermost group, then records that it no longer is — the port of SingleLine#first?.
func (*SingleLine) Flush ¶
func (s *SingleLine) Flush()
Flush is a no-op, present for parity with PrettyPrint.Flush; SingleLine writes directly and buffers nothing.
func (*SingleLine) Group ¶
Group emits openObj, runs fn, then closeObj. indent and the width arguments are ignored.
func (*SingleLine) GroupDefault ¶
func (s *SingleLine) GroupDefault(fn func())
GroupDefault runs fn in a group with no open/close text.
func (*SingleLine) Nest ¶
func (s *SingleLine) Nest(_ int, fn func())
Nest runs fn; indent is ignored.
func (*SingleLine) String ¶
func (s *SingleLine) String() string
String returns the rendered output.
func (*SingleLine) Text ¶
func (s *SingleLine) Text(obj string, _ int)
Text appends obj. The width argument is accepted for API parity and ignored.
func (*SingleLine) TextString ¶
func (s *SingleLine) TextString(obj string)
TextString appends obj, the width-defaulting form.
