wordwrap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 5 Imported by: 1

README

wordwrap GoDoc

Package wordwrap provide a utility to wrap text on word boundaries.

import "github.com/mdigger/wordwrap"
// source unwrapped text
source := `Lorem ipsum dolor sit amet, lectus sed ut at lacinia. ` +
    `A adipiscing. Vel placerat, ornare vel consectetur integer. Et ` +
    `molestie ante mauris, sociis aliqua senectus et. Risus wisi ` +
    `fringilla mauris massa vestibulum, ante est, quis euismod ac ` +
    `suspendisse, sem sodales ligula eleifend tincidunt, nemo donec ` +
    `porta viverra. Volutpat hymenaeos eu non neque sint. Torquent ` +
    `mauris ante et, suspendisse aliquam nunc, urna sem a ornare sed ` +
    `ante laoreet.`
w := wordwrap.New(os.Stdout, 50) // init wrap writer
prefix := "> "                   // define a prefix
w.SetPrefix(prefix)              // set prefix for new lines
w.WriteString(prefix)            // add prefix to first line
w.WriteString(source)            // write other text

Output:

> Lorem ipsum dolor sit amet, lectus sed ut at
> lacinia. A adipiscing. Vel placerat, ornare vel
> consectetur integer. Et molestie ante mauris,
> sociis aliqua senectus et. Risus wisi fringilla
> mauris massa vestibulum, ante est, quis euismod
> ac suspendisse, sem sodales ligula eleifend
> tincidunt, nemo donec porta viverra. Volutpat
> hymenaeos eu non neque sint. Torquent mauris
> ante et, suspendisse aliquam nunc, urna sem a
> ornare sed ante laoreet.

Documentation

Overview

Package wordwrap provide a utility to wrap text on word boundaries.

Example
package main

import (
	"os"

	"github.com/mdigger/wordwrap"
)

func main() {
	// source unwrapped text
	source := `Lorem ipsum dolor sit amet, lectus sed ut at lacinia. ` +
		`A adipiscing. Vel placerat, ornare vel consectetur integer. Et ` +
		`molestie ante mauris, sociis aliqua senectus et. Risus wisi ` +
		`fringilla mauris massa vestibulum, ante est, quis euismod ac ` +
		`suspendisse, sem sodales ligula eleifend tincidunt, nemo donec ` +
		`porta viverra. Volutpat hymenaeos eu non neque sint. Torquent ` +
		`mauris ante et, suspendisse aliquam nunc, urna sem a ornare sed ` +
		`ante laoreet.`
	w := wordwrap.New(os.Stdout, 50) // init wrap writer
	prefix := "> "                   // define a prefix
	w.SetPrefix(prefix)              // set prefix for new lines
	w.WriteString(prefix)            // add prefix to first line
	w.WriteString(source)            // write other text
}
Output:

> Lorem ipsum dolor sit amet, lectus sed ut at
> lacinia. A adipiscing. Vel placerat, ornare vel
> consectetur integer. Et molestie ante mauris,
> sociis aliqua senectus et. Risus wisi fringilla
> mauris massa vestibulum, ante est, quis euismod
> ac suspendisse, sem sodales ligula eleifend
> tincidunt, nemo donec porta viverra. Volutpat
> hymenaeos eu non neque sint. Torquent mauris
> ante et, suspendisse aliquam nunc, urna sem a
> ornare sed ante laoreet.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(b []byte, width uint) []byte

Bytes is shorthand for declaring a new default Writer instance, used to immediately word-wrap a byte slice.

func String

func String(s string, width uint) string

String is shorthand for declaring a new default Writer instance, used to immediately word-wrap a string.

Types

type Writer

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

Writer wraps UTF-8 encoded text at word boundaries when lines exceed a limit number of characters. Newlines are preserved, including consecutive and trailing newlines, though trailing whitespace is stripped from each line.

func New

func New(w io.Writer, width uint) *Writer

New returns a new initialized wrapper over io.Writer to write lines with word wrap after a given position in the line.

func (*Writer) GetPrefix

func (w *Writer) GetPrefix() string

GetPrefix return the current Writer prefix.

func (*Writer) Printf

func (w *Writer) Printf(format string, a ...interface{}) (n int, err error)

Printf formats according to a format specifier and writes to Writer. It returns the number of bytes written and any write error encountered.

func (*Writer) SetBreakpoints

func (w *Writer) SetBreakpoints(s string)

SetBreakpoints set additional word breakpoint runes. For exaple: "-:^".

func (*Writer) SetPosition

func (w *Writer) SetPosition(p int)

SetPosition set current line position for correct word wrapping. A negative value will increase the allowable length of the first line.

func (*Writer) SetPrefix

func (w *Writer) SetPrefix(s string)

SetPrefix add prefix for writing on start of newline. The prefix does not affect the first line.

func (*Writer) SetTabWidth

func (w *Writer) SetTabWidth(width int)

SetTabWidth sets the width of tab characters.

Writer attempts to handle tab characters gracefully, converting them to spaces aligned on the boundary. If width is 0, when used tab character as is by default.

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write wraps UTF-8 encoded text at word boundaries when lines exceed a limit number of characters. Newlines are preserved, including consecutive and trailing newlines, though trailing whitespace is stripped from each line.

It returns the number of bytes written and any write error encountered.

func (*Writer) WriteByte

func (w *Writer) WriteByte(c byte) (err error)

WriteByte write byte to Writer.

func (*Writer) WriteRune

func (w *Writer) WriteRune(r rune) (n int, err error)

WriteRune write rune to Writer. It returns the number of bytes written and any write error encountered.

func (*Writer) WriteString

func (w *Writer) WriteString(str string) (n int, err error)

WriteString implement io.WrieString. It returns the number of bytes written and any write error encountered.

Jump to

Keyboard shortcuts

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