wordwrap

package module
v0.0.0-...-e0f5412 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2016 License: MIT Imports: 2 Imported by: 24

README

Wordwrap Travis Build Status

Ultra-simple word-wrapping library for Golang. Unsophisticated by design.

Installation

Installation via go get:

$ go get github.com/eidolon/wordwrap

Then simply import the github.com/eidolon/wordwrap package.

Usage

Documentation: https://godoc.org/github.com/eidolon/wordwrap

The primary use-case for this library was to wrap text for use in console applications. To that end there are two things this library does; wrapping text, and indenting multi-line strings with a given prefix (e.g. for generating help text).

Wrapping:

Create a wrapper function and choose your wrapping options (line length, and whether or not to break words onto new lines).

wrapper := wordwrap.Wrapper(20, false)
wrapped := wrapper("This string would be split onto several new lines")

Value of wrapped:

This string would
be split onto
several new lines

Indenting:

Given the primary use-case of this library is for console application text generation, you may want to take the output of the wrapper and indent that to produce some help text, like this:

description := wrapped
names := "-f, --foo"

synopsis := wordwrap.Indent(description, names + "  ", false)

Value of synopsis:

-f, --foo  This string would
           be split onto
           several new lines

License

MIT

Documentation

Overview

Package wordwrap provides functions for word wrapping to break long strings onto multiple lines with contraints on line length.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Indent

func Indent(input string, prefix string, prefixAll bool) string

Indent a string with the given prefix at the start of either the first, or all lines.

input     - The input string to indent.
prefix    - The prefix to add.
prefixAll - If true, prefix all lines with the given prefix.

Example usage:

indented := wordwrap.Indent("Hello\nWorld", "-", true)

Types

type WrapperFunc

type WrapperFunc func(string) string

WrapperFunc takes a given input string, and returns some wrapped output. The wrapping may be altered by currying the wrapper function.

func Wrapper

func Wrapper(limit int, breakWords bool) WrapperFunc

Wrapper creates a curried wrapper function (see WrapperFunc) with the given options applied to it. Create a WrapperFunc and store it is a variable, then re-use it elsewhere.

limit      - The maximum number of characters for a line.
breakWords - Whether or not to break long words onto new lines.

Example usage:

wrapper := wordwrap.Wrapper(10, false)
wrapped := wrapper("This string would be split onto several new lines")

Jump to

Keyboard shortcuts

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