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 ¶
- func Bytes(b []byte, width uint) []byte
- func String(s string, width uint) string
- type Writer
- func (w *Writer) GetPrefix() string
- func (w *Writer) Printf(format string, a ...interface{}) (n int, err error)
- func (w *Writer) SetBreakpoints(s string)
- func (w *Writer) SetPosition(p int)
- func (w *Writer) SetPrefix(s string)
- func (w *Writer) SetTabWidth(width int)
- func (w *Writer) Write(b []byte) (n int, err error)
- func (w *Writer) WriteByte(c byte) (err error)
- func (w *Writer) WriteRune(r rune) (n int, err error)
- func (w *Writer) WriteString(str string) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
New returns a new initialized wrapper over io.Writer to write lines with word wrap after a given position in the line.
func (*Writer) Printf ¶
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 ¶
SetBreakpoints set additional word breakpoint runes. For exaple: "-:^".
func (*Writer) SetPosition ¶
SetPosition set current line position for correct word wrapping. A negative value will increase the allowable length of the first line.
func (*Writer) SetPrefix ¶
SetPrefix add prefix for writing on start of newline. The prefix does not affect the first line.
func (*Writer) SetTabWidth ¶
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 ¶
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.