wrap

package
v2.0.0-...-e4ea1e2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: Apache-2.0, MIT Imports: 2 Imported by: 0

README

wrap Go Reference GitHub tag license Go Report Card codecov

An efficient and flexible word-wrapping package for Go (golang)

Usage

var loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc sagittis magna nec purus porttitor mollis. Pellentesque feugiat quam eget laoreet aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia a. Fusce non tortor magna. Cras vel finibus tellus."

// Wrap when lines exceed 80 chars.
fmt.Println(wrap.Wrap(loremIpsum, 80))
// Output:
// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam
// nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper
// fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc sagittis
// magna nec purus porttitor mollis. Pellentesque feugiat quam eget laoreet
// aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia a. Fusce non
// tortor magna. Cras vel finibus tellus.
var loremIpsum = "/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc sagittis magna nec purus porttitor mollis. Pellentesque feugiat quam eget laoreet aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia a. Fusce non tortor magna. Cras vel finibus tellus. */"

w := wrap.NewWrapper()

// Trim the single-line block comment symbols from each input line.
w.TrimInputPrefix = "/* "
w.TrimInputSuffix = " */"

// Prefix each new line with a single-line comment symbol.
w.OutputLinePrefix = "// "

// Wrap when lines exceed 80 chars.
fmt.Println(w.Wrap(loremIpsum, 80))
// Output:
// // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vulputate quam
// // nibh, et faucibus enim gravida vel. Integer bibendum lectus et erat semper
// // fermentum quis a risus. Fusce dignissim tempus metus non pretium. Nunc
// // sagittis magna nec purus porttitor mollis. Pellentesque feugiat quam eget
// // laoreet aliquet. Donec gravida congue massa, et sollicitudin turpis lacinia
// // a. Fusce non tortor magna. Cras vel finibus tellus.
Advanced Usage and more examples (custom breakpoints, prefixes, suffixes, etc.)

See godoc.org/github.com/bbrks/wrap for more examples using the Wrapper type to provide custom breakpoints, prefixes, suffixes, etc.

Contributing

Issues, feature requests or improvements welcome!

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(s string, limit int) string

Wrap is shorthand for declaring a new default Wrapper calling its Wrap method

Types

type Wrapper

type Wrapper struct {
	// Breakpoints defines which characters should be able to break a line.
	// By default, this follows the usual English rules of spaces, and hyphens.
	// Default: " -"
	Breakpoints string

	// Newline defines which characters should be used to split and create new lines.
	// Default: "\n"
	Newline string

	// OutputLinePrefix is prepended to any output lines. This can be useful
	// for wrapping code-comments and prefixing new lines with "// ".
	// Default: ""
	OutputLinePrefix string

	// OutputLineSuffix is appended to any output lines.
	// Default: ""
	OutputLineSuffix string

	// LimitIncludesPrefixSuffix can be set to false if you don't want prefixes
	// and suffixes to be included in the length limits.
	// Default: true
	LimitIncludesPrefixSuffix bool

	// TrimPrefix can be set to remove a prefix on each input line.
	// This can be paired up with OutputPrefix to create a block of C-style
	// comments (/* * */ ) from a long single-line comment.
	// Default: ""
	TrimInputPrefix string

	// TrimSuffix can be set to remove a suffix on each input line.
	// Default: ""
	TrimInputSuffix string

	// StripTrailingNewline can be set to true if you want the trailing
	// newline to be removed from the return value.
	// Default: false
	StripTrailingNewline bool

	// CutLongWords will cause a hard-wrap in the middle of a word if the word's length exceeds the given limit.
	CutLongWords bool
}

Wrapper contains settings for customisable word-wrapping.

func NewWrapper

func NewWrapper() Wrapper

NewWrapper returns a new instance of a Wrapper initialised with defaults.

func (Wrapper) Wrap

func (w Wrapper) Wrap(s string, limit int) string

Wrap will wrap one or more lines of text at the given length. If limit is less than 1, the string remains unwrapped.

Jump to

Keyboard shortcuts

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