wordwrap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2018 License: MIT Imports: 2 Imported by: 598

README

go-wordwrap

go-wordwrap (Golang package: wordwrap) is a package for Go that automatically wraps words into multiple lines. The primary use case for this is in formatting CLI output, but of course word wrapping is a generally useful thing to do.

Installation and Usage

Install using go get github.com/mitchellh/go-wordwrap.

Full documentation is available at http://godoc.org/github.com/mitchellh/go-wordwrap

Below is an example of its usage ignoring errors:

wrapped := wordwrap.WrapString("foo bar baz", 3)
fmt.Println(wrapped)

Would output:

foo
bar
baz

Word Wrap Algorithm

This library doesn't use any clever algorithm for word wrapping. The wrapping is actually very naive: whenever there is whitespace or an explicit linebreak. The goal of this library is for word wrapping CLI output, so the input is typically pretty well controlled human language. Because of this, the naive approach typically works just fine.

In the future, we'd like to make the algorithm more advanced. We would do so without breaking the API.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WrapString

func WrapString(s string, lim uint) string

WrapString wraps the given string within lim width in characters.

Wrapping is currently naive and only happens at white-space. A future version of the library will implement smarter wrapping. This means that pathological cases can dramatically reach past the limit, such as a very long word.

Types

This section is empty.

Jump to

Keyboard shortcuts

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