wordwrap

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package wordwrap wraps text to a fixed width, modeled on the npm "word-wrap" package.

Text is broken on whitespace so that no line exceeds Width characters (measured excluding the indent). Each output line is prefixed with Indent. Existing newlines in the input are preserved as paragraph breaks. When Cut is true, words longer than Width are broken into Width-sized pieces instead of overflowing.

Index

Constants

View Source
const (
	DefaultWidth   = 50
	DefaultIndent  = "  "
	DefaultNewline = "\n"
)

Default option values, matching the npm word-wrap defaults.

Variables

This section is empty.

Functions

func Wrap

func Wrap(text string, opts Options) string

Wrap wraps text according to opts and returns the wrapped result.

Types

type Options

type Options struct {
	// Width is the maximum line length excluding the indent. Values <= 0 are
	// treated as DefaultWidth (50).
	Width int
	// Indent is prefixed to every output line. The zero value is no indent.
	Indent string
	// Newline is the separator placed between output lines. An empty value is
	// treated as DefaultNewline ("\n").
	Newline string
	// TrimTrailing removes trailing spaces and tabs from every output line when
	// true.
	TrimTrailing bool
	// Cut breaks words longer than Width into Width-sized pieces when true.
	Cut bool
}

Options configures Wrap.

A zero-value Options is usable: Width <= 0 is treated as DefaultWidth and an empty Newline is treated as DefaultNewline. Note that a zero-value Indent is the empty string (no indentation); to obtain the two-space default indent, build options with NewOptions rather than a bare Options literal.

func NewOptions

func NewOptions() Options

NewOptions returns an Options populated with the package defaults: Width 50, Indent two spaces, and Newline "\n".

Jump to

Keyboard shortcuts

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