wrapwriter

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

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

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 8 Imported by: 0

README

The wrapwriter Go package

wrapwriter is a Go package that implements a Writer that wraps long lines by automatically inserting newlines characters into the output stream. It also treats mutiple adjacent non-empty lines passed to it as one single line. A Writer implements the standard io.Writer interface.

Installing the wrapwriter package

Install the package with

go get bitbucket.org/mirdk/wrapwriter

Using the wrapwriter package

A wrapwriter.Writer implements the standard io.Writer interface. Thus, text can be written to a wrapwriter.Writer using the Write() method.

A Writer is constructed by the function wrapwriter.NewWriter(). The arguments are the underlying io.Writer that will receive the data passed to this Writer, the preferred output width, the size of the indentation, and the perceived initial column in the output.

Examples

See subdirectory cmd/ for examples.

License

The wrapwriter package is available under The MIT License.

Documentation

Overview

A wrapwriter.Writer is a Writer that wraps long lines by inserting newlines in the output and that treats adjacent non-empty lines passed to it as one single line. A Writer implements the Write interface.

A Writer contains an underlying io.Writer to which actual data is written. Newlines and spaces written to the Writer are interpreted specially: First, adjacent lines that are separated by only one newline are treated as one single line. Second, spaces written are collapsed and may be replaced by newline characters. And third, an input line that begin with a space is written verbatim, without applying any transformation.

A Writer is parameterized by a preferred output width; the writer will attempt to restrict the length of output lines to fit within this width. This is not a hard constraint: Lines are only broken when a space (or a newline) is written, so very long words may extend beyond the preferred output width.

A Writer is also parameterized by the size (number of spaces) of an indentation that is added in front of all lines written; and by the perceived initial column in the output. These two parameters enable output to be formatted as description list.

A Writer makes just a single pass through the data it receives, and it buffers only as much data as necessary to detect word boundaries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

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

A Writer that wraps long lines by inserting newlines in the output and that treats adjacent non-empty lines in the input as one line.

func NewWriter

func NewWriter(output io.Writer, width, indent, column int) *Writer

Create a new wrapwriter.Writer. The arguments are the underlying io.Writer that will receive the data passed to this Writer, the preferred output width, the size of the indentation, and the perceived initial output column for this Writer. If width is less than or equal to zero, then the Writer will use a preferred output width of size C + width, where C is a guess of the width of the shell or terminal in which the program runs. Of one such guess cannot be found, C has a suitable default value. The indented use of the argument column is in situation where text is known already to appear up to this column on the (first) line.

When this Writer has received its last input, the Flush function must be called to send any data still in its internal buffer to the underlying io.Writer.

This parameters of this Writer can be set by the functions SetWidth(), SetIndent(), SetColumn(), and SetIndentVerbatim().

func (*Writer) Dump

func (w *Writer) Dump() error

Flush the internal buffer to the underlying io.Writer. If the output would have exceeded the available width, then insert a newline first. If the Writer is in verbatim mode, then just dump the internal buffer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush the internal buffer to the underlying io.Writer, reset it to its initial state, and write a newline.

func (*Writer) ReadFrom

func (w *Writer) ReadFrom(reader *bufio.Reader) error

Write all the content delivered by a bufio.Reader.

func (*Writer) SetColumn

func (w *Writer) SetColumn(c int)

Set the perceived initial output column for this Writer. (May give incorrect results is applied in anything but an initial state.)

func (*Writer) SetIndent

func (w *Writer) SetIndent(i int)

Set the indentation that this Writer prefixes each line by. This indentation does not apply to verbatim lines after SetIndentVerbatim(false). (May give incorrect results is applied in anything but an initial state.)

func (*Writer) SetIndentVerbatim

func (w *Writer) SetIndentVerbatim(b bool)

Control whether this writer should indent verbatim lines (the default) or not. (May give incorrect results is applied in anything but an initial state.)

func (*Writer) SetWidth

func (w *Writer) SetWidth(width int)

Set the display width for this Writer. (May give incorrect results is applied in anything but an initial state.)

func (*Writer) Write

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

Write a slice of bytes.

func (*Writer) WriteRune

func (w *Writer) WriteRune(ch rune) error

Write a single rune.

func (*Writer) WriteString

func (w *Writer) WriteString(s string) error

Write a string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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