format

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CatchPrinterError

func CatchPrinterError(err *error)

CatchPrinterError can be used to capture panics caused by a Printer because of an error encountered while attempting to send output. See the Printer interface documentation for details.

Types

type Colorizer

type Colorizer struct {
	KeyColorCode     []byte
	ScalarColorCodes [4][]byte
	ResetCode        []byte
}

func (*Colorizer) PrintScalar

func (c *Colorizer) PrintScalar(p Printer, scalar *token.Scalar)

func (*Colorizer) PrintSuccintScalar

func (c *Colorizer) PrintSuccintScalar(p Printer, scalar *token.Scalar)

func (*Colorizer) ScalarColorCode

func (c *Colorizer) ScalarColorCode(scalar *token.Scalar) []byte

type DefaultPrinter

type DefaultPrinter struct {
	io.Writer
	Flusher
	IndentSize int
	// contains filtered or unexported fields
}

DefaultPrinter implements a Printer which uses an io.Writer to send output, using IndentSize spaces for each indent level. If IndentSize is negative, then NewLine() does nothing so all the output is on one single line. If IndentSize is 0, then there is no indentation but there are still new lines. If Flusher is not nil, then the printer will call Flush() at the end of each line (good when writing to terminal).

func (*DefaultPrinter) Dedent

func (p *DefaultPrinter) Dedent()

Dedent has the effect of decrementing the indentation level and calls NewLine()

func (*DefaultPrinter) Indent

func (p *DefaultPrinter) Indent()

Indent has the effect of incrementing the indentation level and calls NewLine()

func (*DefaultPrinter) NewLine

func (p *DefaultPrinter) NewLine()

NewLines outputs '\n' followed by a number of spaces corresponding to the current indentation level.

func (*DefaultPrinter) PrintBytes

func (p *DefaultPrinter) PrintBytes(b []byte)

PrintBytes sends the gives bytes verbatim to the printer's writer.

func (*DefaultPrinter) Reset

func (p *DefaultPrinter) Reset()

Reset outputs '\n' unconditionally and resets the indent level.

type Flusher

type Flusher interface {
	Flush() error
}

type Printer

type Printer interface {
	Indent()
	Dedent()
	NewLine()
	Reset()
	PrintBytes([]byte)
}

The Printer interface can be used to output some structured data.

Indent() starts a new line at an increased indentation level Dedent() starts a new line at a decreased indentation level NewLine() start a new line at the current indentation level Reset() resets the indentation level to 0 and moves to a new line (this cannot be overriden) PrintBytes() outputs bytes at the current position

The methods do not return an error because for this program it's assumed to be an exceptional case that outputting results in an error and the only sensible outcome is to stop the program. Instead, implementations are expected to panic with a *PrinterError when they encounter and error. A user of the Printer interface can use

func printingFunction(p Printer) (err error) {
    defer CatchPrinterError(&err)
    return doSomePrinting(printer)
}

to capture such errors.

type PrinterError

type PrinterError struct {
	Err error
}

A PrinterError contains an error that occurred while a Printer implementation was sending some output.

func (*PrinterError) Error

func (e *PrinterError) Error() string

func (*PrinterError) Unwrap

func (e *PrinterError) Unwrap() error

Jump to

Keyboard shortcuts

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