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 ¶
- type Writer
- func (w *Writer) Dump() error
- func (w *Writer) Flush() error
- func (w *Writer) ReadFrom(reader *bufio.Reader) error
- func (w *Writer) SetColumn(c int)
- func (w *Writer) SetIndent(i int)
- func (w *Writer) SetIndentVerbatim(b bool)
- func (w *Writer) SetWidth(width int)
- func (w *Writer) Write(b []byte) (int, error)
- func (w *Writer) WriteRune(ch rune) error
- func (w *Writer) WriteString(s string) error
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 ¶
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 ¶
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 ¶
Flush the internal buffer to the underlying io.Writer, reset it to its initial state, and write a newline.
func (*Writer) SetColumn ¶
Set the perceived initial output column for this Writer. (May give incorrect results is applied in anything but an initial state.)
func (*Writer) SetIndent ¶
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 ¶
Control whether this writer should indent verbatim lines (the default) or not. (May give incorrect results is applied in anything but an initial state.)