wordwrap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2018 License: Apache-2.0 Imports: 6 Imported by: 1

README

wordwrap

GoDoc Reference Build Status

A Go library for word-wrapping text.

Features

  • Text is guaranteed to never exceed line width.
  • Support for multi-byte (utf8) text
  • Handling for tab width and alignment; tabs are replaced by spaces
  • Streaming: text need not be loaded into a buffer.
  • Settings (line prefix, tab width) can be changed on the fly.

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scanner

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

Scanner 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.

Clients should not assume Scanner is thread-safe.

func NewScanner

func NewScanner(r io.Reader, limit int) *Scanner

NewScanner creates and initializes a new Scanner given a reader and fixed line limit. The new Scanner takes ownership of the reader, and the caller should not use it after this call.

func (*Scanner) ReadLine

func (s *Scanner) ReadLine() (string, error)

ReadLine reads a single wrapped line, not including end-of-line characters ("\n"). Trailing newlines are preserved. At EOF, the result will be an empty string and the error will be io.EOF.

ReadLine always attempts to return at least one line, even on empty input.

ReadLine attempts to handle tab characters gracefully, converting them to spaces aligned on the boundary define in SetTabWidth.

func (*Scanner) SetPrefix

func (s *Scanner) SetPrefix(prefix string)

SetPrefix sets a string to prefix each future line. The prefix is not applied to empty lines and the prefix's length is not included in the character limit specified in NewScanner.

It's safe to call SetPrefix between calls to ReadLine.

func (*Scanner) SetTabWidth

func (s *Scanner) SetTabWidth(width int)

SetTabWidth sets the width of tab characters.

It's safe to call SetTabWidth between calls to ReadLine.

func (*Scanner) WriteTo

func (s *Scanner) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo. This may make multiple calls to the Read method of the underlying Reader.

Jump to

Keyboard shortcuts

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