wordwrap

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: BSD-3-Clause Imports: 5 Imported by: 11

README

wordwrap

Wrap the given string at the given width.

API

  • WordWrap(text, maxWidth)
  • WrapLine(line, limit, maxBacktrack)
  • PoeticWrap(text, maxWidth, minWidth)
  • NoLineStart(r)

Version and license

  • Version: 1.2.0
  • License: BSD-3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoLineStart added in v1.1.0

func NoLineStart(r rune) bool

NoLineStart returns true for punctuation that must not begin a wrapped line. These characters in good typography always follow the preceding word.

func PoeticWrap added in v1.2.0

func PoeticWrap(text string, maxWidth, minWidth int) ([]string, error)

PoeticWrap wraps text to maxWidth runes, preferring breaks at natural punctuation (sentence ends, semicolons, colons, commas, em/en dashes) over arbitrary word boundaries. Uses Knuth–Plass-style DP to minimise slack² + brokenness globally. Suited to fortunes, quotes and poems.

minWidth is the soft minimum length for non-final lines; 0 picks a default of maxWidth*2/5, values above maxWidth disable the guard. Newlines are hard paragraph breaks. Honours the NoLineStart rule.

func WordWrap

func WordWrap(text string, maxWidth int) ([]string, error)

WordWrap wraps the input text to the specified maxWidth (in runes). It returns a slice of strings, each of which is a line of the wrapped text, or an error if maxWidth is not valid.

The wrapping respects typographic conventions: no line will begin with punctuation that must follow the preceding word (. , ! ? : ; ) ] } etc.).

Types

type WrapResult added in v1.1.0

type WrapResult struct {
	// Left is the text that stays on the current line (the break space excluded).
	Left string
	// Right is the text that moves to the next line (leading spaces removed).
	Right string
	// BreakAt is the rune index of the space where the line was broken.
	// -1 if no wrapping was performed.
	BreakAt int
	// RightStart is the rune index in the original line where Right begins
	// (past the break space and any consecutive whitespace).
	RightStart int
	// Wrapped is true if the line was successfully wrapped.
	Wrapped bool
}

WrapResult holds the result of a line wrap operation.

func WrapLine added in v1.1.0

func WrapLine(line string, limit, maxBacktrack int) WrapResult

WrapLine finds the best position to break a single line that exceeds limit.

It searches backwards from the limit position for a whitespace character that produces a typographically valid break — one where the resulting new line does not begin with punctuation that must follow the preceding word (. , ! ? : ; ) ] } ' " etc.).

maxBacktrack limits how far back from limit to search for a break point. When maxBacktrack is 0 the search extends all the way to position 1.

If the line does not exceed limit, or no suitable break point is found, the result has Wrapped=false, Left equals the full line, and BreakAt is -1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL