wordwrap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: MIT Imports: 2 Imported by: 0

README

wordwrap

CI codecov Go Reference

Wraps words at a given limit. Wraps at whitespace, hyphens (-), and will wrap words that exceed the given limit. See package documentation for more details.

Example

lines := wordwrap.WordWrap("this test-string has been successfully wrapped successfully", 10)
for _, line := range lines {
	fmt.Println(line)
}
Output
this test-
string has
been
successful
ly wrapped
successful
ly

Documentation

Overview

Package wordwrap provides a word-wrapping function that returns the wrapped lines as a slice of strings.

Example

Words are wrapped at whitespace and at hyphens. Wrapping whitespace is trimmed, hyphens are kept. If a word is too long to fit in the limit, it will be broken at the limit.

lines := wordwrap.WordWrap("this test-string has been successfully wrapped successfully", 10)
for _, line := range lines {
	fmt.Println(line)
}
Output:

this test-
string has
been
successful
ly wrapped
successful
ly

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WordWrap

func WordWrap(s string, limit int) (lines []string)

WordWrap wraps a string, where each "line" of the wrapped string is an item of a slice of strings. A limit of zero or less is treated as an infinite limit.

If a word is longer than the limit would be allowed even after breaking on word separators, then the word itself will be wrapped.

Whitespace such as spaces and tabs are considered word separators that can excluded from the output. Hyphens are also considered word separators, but will be included at the end of a line.

Example (Hyphen)

Words are wrapped at hyphens, and the hyphens are kept at the ends of lines.

lines := wordwrap.WordWrap("hyphenated-words", 15)
for _, line := range lines {
	fmt.Println(line)
}
Output:

hyphenated-
words
Example (Long)

Words that are too long for the limit will be broken.

lines := wordwrap.WordWrap("longwordsarewrapped", 10)
for _, line := range lines {
	fmt.Println(line)
}
Output:

longwordsa
rewrapped
Example (Space)

Words are wrapped at spaces.

lines := wordwrap.WordWrap("we wrap at spaces", 9)
for _, line := range lines {
	fmt.Println(line)
}
Output:

we wrap
at spaces

Types

This section is empty.

Jump to

Keyboard shortcuts

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