gridtable

package
v0.0.0-...-88f6e9b Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package gridtable implements a library for printing grid tables.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrColumnIndexOutOfRange indicates that an invalid column index was referenced.
	ErrColumnIndexOutOfRange = errors.New("column index out of range")
	// ErrShadowedCell indicates that a "shadowed" cell (one hidden by an already-spanned cell) was referenced.
	ErrShadowedCell = errors.New("wrote to shadowed cell")
	// ErrNegativeSpan indicates that a cell with a negative span was written.
	ErrNegativeSpan = errors.New("negative span")
	// ErrOverlappingSpans indicates that two different spans overlapped.
	ErrOverlappingSpans = errors.New("overlapping spans")
	// ErrSpanBeyondHeader indicates that a cell in the header spanned past the end of the header.
	ErrSpanBeyondHeader = errors.New("span extended beyond header")
	// ErrInvalidColumnSpec indicates that a column spec was invalid.
	ErrInvalidColumnSpec = errors.New("invalid column spec")
	// ErrBadWrap indicates that text could not be wrapped to fit into its column.
	ErrBadWrap = errors.New("text could not be wrapped")
	// ErrMalformedTable indicates that the grid table was malformed.
	ErrMalformedTable = errors.New("malformed grid table")
	// ErrReaderNotDone indicates that the requested operation requires the reader to have completely consumed the table already,
	// and it hasn't.
	ErrReaderNotDone = errors.New("reader not done reading table")
)

Functions

This section is empty.

Types

type Cell

type Cell struct {
	// The text to put into the cell.
	Text string
	// The number of additional rows this cell spans. 0 = no span.
	RowSpan int
	// The number of additional columns this cell spans. 0 = no span.
	ColSpan int
}

Cell is the contents to write into the cell of a table.

func (*Cell) String

func (c *Cell) String() string

String implements Stringer.

type ColumnSpec

type ColumnSpec struct {
	// Width of the column in number of characters (not counting the separators).
	Width int
}

A ColumnSpec describes the parameters of a column.

type Config

type Config struct {
	// The top `NumHeaderRows` are considered to be the header of the table.
	// 0 = no header.
	NumHeaderRows int
	// The specification of the columns in the table.
	Columns []ColumnSpec
}

A Config configures the initialization of a Writer.

type Reader

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

Reader is an object that can be used to read in a grid table.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader instantiates a new Reader that reads table rows from an underlying io.Reader.

func (*Reader) GetConfig

func (r *Reader) GetConfig() (*Config, error)

GetConfig can be used to read the config detected on the table.

func (*Reader) Read

func (r *Reader) Read() iter.Seq2[[]*Cell, error]

Read() returns an iterator over rows that can be ranged over using the range function. A nil entry in the result indicates a shadowed cell. It returns EndOfTable, nil when there are no more rows to read.

type Writer

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

Writer is an object that can be used to write out a grid table.

func NewWriter

func NewWriter(config Config) (*Writer, error)

NewWriter initializes a new Writer based on the specified configuration. Rows are written to `out` as they become ready.

func (*Writer) NextRow

func (w *Writer) NextRow()

NextRow finishes the current row and moves onto the next one.

func (*Writer) String

func (w *Writer) String() (string, error)

String writes out the table to a string.

func (*Writer) WriteColumn

func (w *Writer) WriteColumn(index int, cell Cell) error

WriteColumn writes the cell into the specified column of the current row.

Jump to

Keyboard shortcuts

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