go-ruby-prettyprint

Ruby's PrettyPrint Wadler/Lindig layout engine in pure Go โ€” MRI byte-exact, no cgo.

pure Go ยท zero cgo PrettyPrint-compatible Wadler/Lindig groups & breakables nest ยท indent fill mode single-line format width-fitting MRI byte-exact 100% coverage 6 arches
Documentation GitHub
Documentation (MkDocs Material + mike) License: BSD-3-Clause Go 1.26.4+ Coverage 100%

go-ruby-prettyprint is a pure-Go (no cgo) reimplementation of Ruby's prettyprint โ€” the Wadler/Lindig pretty-printing engine that lays out a stream of text, breakable separators and groups into a width-constrained, nicely indented document. It is a byte-identical port of MRI's prettyprint.rb: each group prints on one line when it fits within maxwidth and is otherwise broken at its breakable points, nesting preserved โ€” without any Ruby runtime. This is the layout engine only โ€” groups, breakables, nesting and the depth-bucketed group queue; the pp object inspector that uses it stays host-side in rbgo. It is the layout backend for go-embedded-ruby, bound by rbgo as a native module just like go-ruby-yaml and go-ruby-marshal โ€” differential-tested against MRI, 100% coverage, CI green across 6 arches and 3 OSes.

Groups & width-fitting ready

Groups print flat when they fit and break at their breakables when they overflow maxwidth โ€” the exact break_outmost_groups / depth-bucketed GroupQueue algorithm MRI uses, so nested groups break outermost-first.

Breakables ready

breakable(sep, width) line-break hints that emit their separator when the line is not broken, with the width argument for multibyte or proportional separators.

Nesting & indentation ready

nest(indent) and the group(indent, โ€ฆ) indent argument, with a pluggable genspace block for the indentation string (default DefaultGenSpace โ€” n ASCII spaces).

Open/close text & fill mode ready

group(indent, open, close) wrapping the block in bracketing text counted toward the fit decision, and fill_breakable, where each break is decided individually.

Single-line formatter ready

singleline_format, where breakables become their separator text and nothing ever breaks โ€” plus custom newline and custom maxwidth, matching MRI’s PrettyPrint.new / PrettyPrint.format signatures.

Differential oracle & coverage ready

A shared corpus run through both this package and a generated PrettyPrint.format / singleline_format script, output asserted identical; deterministic ruby-free golden + white-box tests alone hold 100% coverage, gofmt + go vet clean, green across all six 64-bit Go arches and three OSes.

A faithful port of Ruby's PrettyPrint layout engine in pure Go, cgo disabled, so it cross-compiles and embeds anywhere. It implements the deterministic Wadler/Lindig algorithm โ€” the buffer, the group stack and the depth-bucketed group queue โ€” so groups print flat when they fit and break outermost-first when they overflow, with nesting and fill mode. Validated differentially against the system ruby binary. It is the layout engine only; the pp object walker that emits text/group/breakable calls stays host-side. A standalone, reusable module bound into the sibling org github.com/go-embedded-ruby.