textwrap

package
v0.5.0 Latest Latest
Warning

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

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

README

textwrap

Package textwrap reflows a single paragraph of text so that no line is wider than a given display width, without ever splitting a word or a hyphenated word.

import "github.com/ctx42/cfsync/pkg/textwrap"

s := textwrap.Wrap("The state-of-the-art solution wraps text nicely.", 20)
// The state-of-the-art
// solution wraps text
// nicely.

Width is measured in terminal display columns (runewidth). Pass a width of 0 or less for no limit. Use WrapTokens when a token must stay whole even if it contains spaces (for example a Markdown link).

See the package examples for more.

Documentation

Overview

Package textwrap reflows a single paragraph of text so that no line is wider than a given display width, without ever splitting a word or a hyphenated word.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(s string, width int) string

Wrap reflows s into lines no wider than width display columns and returns the result with lines joined by "\n" and without a trailing newline.

Every run of whitespace in s collapses to a single space, so any line breaks already present are treated as soft and the paragraph is re-wrapped from scratch. Words are whitespace-delimited and atomic: a word, including a hyphenated one such as "state-of-the-art", is never split, not even at a hyphen. A word wider than width on its own occupies a line that exceeds the limit rather than being broken.

Width is measured in terminal display columns with runewidth.StringWidth, so double-width runes count as two columns and zero-width runes as none. A width less than or equal to zero means "no limit": s is returned collapsed to a single line. Input that is empty or all whitespace yields an empty string.

Example
package main

import (
	"fmt"

	"github.com/ctx42/cfsync/pkg/textwrap"
)

func main() {
	s := "The state-of-the-art solution wraps text nicely."
	fmt.Println(textwrap.Wrap(s, 20))
}
Output:
The state-of-the-art
solution wraps text
nicely.

func WrapTokens

func WrapTokens(words []string, width int) string

WrapTokens greedily packs words onto lines no wider than width display columns, joining words with a single space, and returns the result with lines joined by "\n" and without a trailing newline.

Each element of words is atomic and is never split, so a caller can keep a unit that contains spaces (such as a Markdown link) whole by passing it as a single word. Width is measured as in Wrap; a width less than or equal to zero means "no limit". An empty words slice yields an empty string.

Example
package main

import (
	"fmt"

	"github.com/ctx42/cfsync/pkg/textwrap"
)

func main() {
	// A token that contains spaces, such as a Markdown link, stays whole: it is
	// never split at its internal space even when it lands on its own line.
	words := []string{"click", "[Asset Data](url)", "here"}
	fmt.Println(textwrap.WrapTokens(words, 18))
}
Output:
click
[Asset Data](url)
here

Types

This section is empty.

Jump to

Keyboard shortcuts

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