tabwriter

Imports

Imports #

"fmt"
"io"
"unicode/utf8"

Constants & Variables

AlignRight const #

Force right-alignment of cell content. Default is left-alignment.

const AlignRight

Debug const #

Print a vertical bar ('|') between columns (after formatting). Discarded columns appear as zero-width columns ("||").

const Debug

DiscardEmptyColumns const #

Handle empty columns as if they were not present in the input in the first place.

const DiscardEmptyColumns

Escape const #

To escape a text segment, bracket it with Escape characters. For instance, the tab in this string "Ignore this tab: \xff\t\xff" does not terminate a cell and constitutes a single character of width one for formatting purposes. The value 0xff was chosen because it cannot appear in a valid UTF-8 sequence.

const Escape = '\xff'

FilterHTML const #

Ignore html tags and treat entities (starting with '&' and ending in ';') as single characters (width = 1).

const FilterHTML uint = *ast.BinaryExpr

StripEscape const #

Strip Escape characters bracketing escaped text segments instead of passing them through unchanged with the text.

const StripEscape

TabIndent const #

Always use tabs for indentation columns (i.e., padding of leading empty cells on the left) independent of padchar.

const TabIndent

hbar var #

var hbar = *ast.CallExpr

newline var #

var newline = []byte{...}

tabs var #

var tabs = *ast.CallExpr

vbar var #

var vbar = []byte{...}

Structs

Writer struct #

A Writer is a filter that inserts padding around tab-delimited columns in its input to align them in the output. The Writer treats incoming bytes as UTF-8-encoded text consisting of cells terminated by horizontal ('\t') or vertical ('\v') tabs, and newline ('\n') or formfeed ('\f') characters; both newline and formfeed act as line breaks. Tab-terminated cells in contiguous lines constitute a column. The Writer inserts padding as needed to make all cells in a column have the same width, effectively aligning the columns. It assumes that all characters have the same width, except for tabs for which a tabwidth must be specified. Column cells must be tab-terminated, not tab-separated: non-tab terminated trailing text at the end of a line forms a cell but that cell is not part of an aligned column. For instance, in this example (where | stands for a horizontal tab): aaaa|bbb|d aa |b |dd a | aa |cccc|eee the b and c are in distinct columns (the b column is not contiguous all the way). The d and e are not in a column at all (there's no terminating tab, nor would the column be contiguous). The Writer assumes that all Unicode code points have the same width; this may not be true in some fonts or if the string contains combining characters. If [DiscardEmptyColumns] is set, empty columns that are terminated entirely by vertical (or "soft") tabs are discarded. Columns terminated by horizontal (or "hard") tabs are not affected by this flag. If a Writer is configured to filter HTML, HTML tags and entities are passed through. The widths of tags and entities are assumed to be zero (tags) and one (entities) for formatting purposes. A segment of text may be escaped by bracketing it with [Escape] characters. The tabwriter passes escaped text segments through unchanged. In particular, it does not interpret any tabs or line breaks within the segment. If the [StripEscape] flag is set, the Escape characters are stripped from the output; otherwise they are passed through as well. For the purpose of formatting, the width of the escaped text is always computed excluding the Escape characters. The formfeed character acts like a newline but it also terminates all columns in the current line (effectively calling [Writer.Flush]). Tab- terminated cells in the next line start new columns. Unless found inside an HTML tag or inside an escaped text segment, formfeed characters appear as newlines in the output. The Writer must buffer input internally, because proper spacing of one line may depend on the cells in future lines. Clients must call Flush when done calling [Writer.Write].

type Writer struct {
output io.Writer
minwidth int
tabwidth int
padding int
padbytes [8]byte
flags uint
buf []byte
pos int
cell cell
endChar byte
lines [][]cell
widths []int
}

cell struct #

A cell represents a segment of text terminated by tabs or line breaks. The text itself is stored in a separate buffer; cell only describes the segment's size in bytes, its width in runes, and whether it's an htab ('\t') terminated cell.

type cell struct {
size int
width int
htab bool
}

osError struct #

local error wrapper so we can distinguish errors we want to return as errors from genuine panics (which we don't want to return as errors)

type osError struct {
err error
}

Functions

Flush method #

Flush should be called after the last call to [Writer.Write] to ensure that any data buffered in the [Writer] is written to output. Any incomplete escape sequence at the end is considered complete for formatting purposes.

func (b *Writer) Flush() error

Init method #

A [Writer] must be initialized with a call to Init. The first parameter (output) specifies the filter output. The remaining parameters control the formatting: minwidth minimal cell width including any padding tabwidth width of tab characters (equivalent number of spaces) padding padding added to a cell before computing its width padchar ASCII char used for padding if padchar == '\t', the Writer will assume that the width of a '\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result) flags formatting control

func (b *Writer) Init(output io.Writer, minwidth int, tabwidth int, padding int, padchar byte, flags uint) *Writer

NewWriter function #

NewWriter allocates and initializes a new [Writer]. The parameters are the same as for the Init function.

func NewWriter(output io.Writer, minwidth int, tabwidth int, padding int, padchar byte, flags uint) *Writer

Write method #

Write writes buf to the writer b. The only errors returned are ones encountered while writing to the underlying output stream.

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

addLine method #

addLine adds a new line. flushed is a hint indicating whether the underlying writer was just flushed. If so, the previous line is not likely to be a good indicator of the new line's cells.

func (b *Writer) addLine(flushed bool)

append method #

Append text to current cell.

func (b *Writer) append(text []byte)

dump method #

debugging support (keep code around)

func (b *Writer) dump()

endEscape method #

Terminate escaped mode. If the escaped text was an HTML tag, its width is assumed to be zero for formatting purposes; if it was an HTML entity, its width is assumed to be one. In all other cases, the width is the unicode width of the text.

func (b *Writer) endEscape()

flush method #

flush is the internal version of Flush, with a named return value which we don't want to expose.

func (b *Writer) flush() (err error)

flushNoDefers method #

flushNoDefers is like flush, but without a deferred handlePanic call. This can be called from other methods which already have their own deferred handlePanic calls, such as Write, and avoid the extra defer work.

func (b *Writer) flushNoDefers()

format method #

Format the text between line0 and line1 (excluding line1); pos is the buffer position corresponding to the beginning of line0. Returns the buffer position corresponding to the beginning of line1 and an error, if any.

func (b *Writer) format(pos0 int, line0 int, line1 int) (pos int)

handlePanic method #

func (b *Writer) handlePanic(err *error, op string)

reset method #

Reset the current state.

func (b *Writer) reset()

startEscape method #

Start escaped mode.

func (b *Writer) startEscape(ch byte)

terminateCell method #

Terminate the current cell by adding it to the list of cells of the current line. Returns the number of cells in that line.

func (b *Writer) terminateCell(htab bool) int

updateWidth method #

Update the cell width.

func (b *Writer) updateWidth()

write0 method #

func (b *Writer) write0(buf []byte)

writeLines method #

func (b *Writer) writeLines(pos0 int, line0 int, line1 int) (pos int)

writeN method #

func (b *Writer) writeN(src []byte, n int)

writePadding method #

func (b *Writer) writePadding(textw int, cellw int, useTabs bool)

Generated with Arrow