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 ¶
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 ¶
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.