text

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

textwrapper

A golang module for wrapping text on word boundaries.

Synopsis

This module provides a parser that consumes a string, treating it as UTF-8. It divides the string into "whitespace" and "words". Words are contiguous sequences of non-whitespace runes. The parser rearranges the string text into rows that are no longer than a specified length wide. It tries to break around whitespace. If a resulting row is a single word that exceeds the maximum line length, it breaks the word at exactly the line length, placing the remainder on the subsequent row (or rows, if the string is long enough).

Install

go get -u github.com/blorticus-go/text

Example

import "github.com/blorticus-go/text"
import "fmt"

func panicIfError(e error) {
    if e != nil {
        panic(e.Error())
    }
}

func main() {
    fh, err := os.Open(os.Args[1])
    panicIfError(err)

    wrapper := textwrapper.NewTextWrapper().
        UsingRowWidth(50).
        UsingIndentStringForRowsAfterTheFirst("   ")

    formattedString, err := wrapper.WrapUTF8TextFromAReader(fh)
    panicIfError(err)

    fmt.Print(formattedString)
}

Documentation

Overview

Package text provides methods for reading UTF-8 text handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Wrapper

type Wrapper struct {
	// contains filtered or unexported fields
}

Wrapper provides UTF-8 text line wrapping. At the start of each inserted line, any whitespace is removed. The Wrapper converts tab characters (code point 9) to a set (but configurable) number of spaces (code point 32). Line break sequences (code point 10 and and 13) are converted into a single space. If more than one line break occurs in a row, they are flattened to a single space. The Wrapper breaks the text stream into runs of unicode spaces and non-spaces. Runs of non-spaces are considered "words". At the start of a new wrapped line, any leading whitespace (after conversions noted above) are discarded. Alternating word and whitespace sequences are emitted until the (configurable) column width (minus the length of the line break sequence) is reached. If the column width would break a word the Wrapper rewinds to the last whitespace sequence, removes it, then inserts the (configurable) line break sequence. If there is no whitespace sequence before the start of the line (i.e., there are more contiguous word characters in the line than the column width minus the line break sequence length), the line break sequence is inserted at the column width minus the line break sequence length and the word continues on the next line. At the start of each indented line, a configurable preamble may be inserted. The characters in the preamble count against the row column count. A configurable preamble may also be be inserted on the initial line, but it is configured separately from the subsequent line indents in case the two should be different (a common case is to have no initial indent, but have a fixed number of spaces on subsequent lines).

func NewWrapper

func NewWrapper() *Wrapper

NewWrapper creates an empty wrapper.

func (*Wrapper) ChangeIndentStringForFirstRowTo

func (wrapper *Wrapper) ChangeIndentStringForFirstRowTo(indent string) *Wrapper

ChangeIndentStringForFirstRowTo sets the indent string for the first row. By default, it is the empty string (meaning "no indent").

func (*Wrapper) ChangeIndentStringForRowsAfterTheFirstTo

func (wrapper *Wrapper) ChangeIndentStringForRowsAfterTheFirstTo(indent string) *Wrapper

ChangeIndentStringForRowsAfterTheFirstTo sets the indent string for rows after the first. By default, it is the empty string (meaning "no indent").

func (*Wrapper) ChangeRowWidthTo

func (wrapper *Wrapper) ChangeRowWidthTo(numberOfColumns uint) *Wrapper

ChangeRowWidthTo changes the column width to the provided value. The default column width is 79.

func (*Wrapper) MustWrapStringText added in v0.2.0

func (wrapper *Wrapper) MustWrapStringText(unwrappedString string) (wrappedText string)

MustWrapStringText is the same as WrapStringText but panics if an error occurs

func (*Wrapper) MustWrapUTF8TextFromAReader added in v0.2.0

func (wrapper *Wrapper) MustWrapUTF8TextFromAReader(reader io.Reader) (wrappedText string)

MustWrapUTF8TextFromAReader is the same as WrapUTF8TextFromAReader but panics if an error occurs

func (*Wrapper) UsingIndentStringForFirstRow

func (wrapper *Wrapper) UsingIndentStringForFirstRow(indent string) *Wrapper

UsingIndentStringForFirstRow is the same as ChangeIndentStringForFirstRowTo(), but provides a more readable name if this chained with the constructor.

func (*Wrapper) UsingIndentStringForRowsAfterTheFirst

func (wrapper *Wrapper) UsingIndentStringForRowsAfterTheFirst(indent string) *Wrapper

UsingIndentStringForRowsAfterTheFirst is the same as ChangeIndentStringForRowsAfterTheFirstTo(), but provides a more readable name if this chained with the constructor.

func (*Wrapper) UsingRowWidth

func (wrapper *Wrapper) UsingRowWidth(numberOfColumns uint) *Wrapper

UsingRowWidth is the same as ChangeRowWidthTo(), but provides a more readable name if this is chained with the constructor, as in:

wrapper := text.NewWrapper().UsingRowWidth(120)

func (*Wrapper) WrapStringText

func (wrapper *Wrapper) WrapStringText(unwrappedString string) (wrappedText string, err error)

WrapStringText takes a string and wraps it using the rules described above. It returns the wrapped text or an error if one occurs.

func (*Wrapper) WrapUTF8TextFromAReader

func (wrapper *Wrapper) WrapUTF8TextFromAReader(reader io.Reader) (wrappedText string, err error)

WrapUTF8TextFromAReader resets the Wrapper parser state. It begins to Read from the supplied reader, treating incoming bytes as UTF-8 encoded text, wrapping using the rules described above. It will Read() until it reaches io.EOF. It returns the wrapped text or an error if one occurs.

Jump to

Keyboard shortcuts

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