Documentation
¶
Overview ¶
Package tblfmt provides streaming table encoders for result sets (ie, from a database).
Index ¶
- func DefaultTableSummary() map[int]func(io.Writer, int) (int, error)
- func Encode(w io.Writer, resultSet ResultSet, options map[string]string) error
- func EncodeAll(w io.Writer, resultSet ResultSet, options map[string]string) error
- func EncodeCSV(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeCSVAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeExpanded(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeExpandedAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeJSON(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeJSONAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeTable(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeTableAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeTemplate(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeTemplateAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func FromMap(opts map[string]string) (Builder, []Option)
- type Align
- type Builder
- type CSVEncoder
- type Encoder
- func NewCSVEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewExpandedEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewJSONEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewTableEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewTemplateEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- type Error
- type EscapeFormatter
- type EscapeFormatterOption
- func WithEscapeJSON(escapeJSON bool) EscapeFormatterOption
- func WithHeaderAlign(a Align) EscapeFormatterOption
- func WithInvalid(invalid string) EscapeFormatterOption
- func WithJSONConfig(prefix, indent string, escapeHTML bool) EscapeFormatterOption
- func WithMarshaler(marshaler func(interface{}) ([]byte, error)) EscapeFormatterOption
- func WithMask(mask string) EscapeFormatterOption
- func WithTimeFormat(timeFormat string) EscapeFormatterOption
- type Executor
- type ExpandedEncoder
- type Formatter
- type JSONEncoder
- type LineStyle
- type Option
- func WithBorder(border int) Option
- func WithCount(count int) Option
- func WithEmpty(empty string) Option
- func WithFieldSeparator(fieldsep rune) Option
- func WithFormatter(formatter Formatter) Option
- func WithHtmlTemplate(t string) Option
- func WithInline(inline bool) Option
- func WithLineStyle(lineStyle LineStyle) Option
- func WithMinExpandWidth(w int) Option
- func WithMinPagerHeight(h int) Option
- func WithMinPagerWidth(w int) Option
- func WithNamedTemplate(name string) Option
- func WithNewline(newline string) Option
- func WithPager(p string) Option
- func WithSummary(summary map[int]func(io.Writer, int) (int, error)) Option
- func WithTableAttributes(a string) Option
- func WithTextTemplate(t string) Option
- func WithTitle(title string) Option
- func WithWidths(widths []int) Option
- type ResultSet
- type TableEncoder
- type TemplateEncoder
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultTableSummary ¶
DefaultTableSummary is the default table summary.
func EncodeCSV ¶
EncodeCSV encodes the result set to the writer as CSV using the supplied encoding options.
func EncodeCSVAll ¶
EncodeCSVAll encodes all result sets to the writer as CSV using the supplied encoding options.
func EncodeExpanded ¶
EncodeExpanded encodes result set to the writer as a table using the supplied encoding options.
func EncodeExpandedAll ¶
EncodeExpandedAll encodes all result sets to the writer as a table using the supplied encoding options.
func EncodeJSON ¶
EncodeJSON encodes the result set to the writer as JSON using the supplied encoding options.
func EncodeJSONAll ¶
EncodeJSONAll encodes all result sets to the writer as JSON using the supplied encoding options.
func EncodeTable ¶
EncodeTable encodes result set to the writer as a table using the supplied encoding options.
func EncodeTableAll ¶
EncodeTableAll encodes all result sets to the writer as a table using the supplied encoding options.
func EncodeTemplate ¶
EncodeTemplate encodes the result set to the writer using a template from the supplied encoding options.
func EncodeTemplateAll ¶
EncodeTemplateAll encodes all result sets to the writer using a template from the supplied encoding options.
Types ¶
type CSVEncoder ¶
type CSVEncoder struct {
// contains filtered or unexported fields
}
CSVEncoder is an unbuffered CSV encoder for result sets.
type Encoder ¶
Encoder is the shared interface for encoders.
func NewCSVEncoder ¶
NewCSVEncoder creates a new CSV encoder using the provided options.
func NewExpandedEncoder ¶
NewExpandedEncoder creates a new table encoder using the provided options.
func NewJSONEncoder ¶
NewJSONEncoder creates a new JSON encoder using the provided options.
func NewTableEncoder ¶
NewTableEncoder creates a new table encoder using the provided options.
type Error ¶
type Error string
Error is an error.
const ( // ErrResultSetIsNil is the result set is nil error. ErrResultSetIsNil Error = "result set is nil" // ErrResultSetHasNoColumns is the result set has no columns error. ErrResultSetHasNoColumns Error = "result set has no columns" // ErrInvalidFormat is the invalid format error. ErrInvalidFormat Error = "invalid format" // ErrInvalidLineStyle is the invalid line style error. ErrInvalidLineStyle Error = "invalid line style" // ErrUnknownTemplate is the unknown template error. ErrUnknownTemplate Error = "unknown template" )
Error values.
type EscapeFormatter ¶
type EscapeFormatter struct {
// contains filtered or unexported fields
}
EscapeFormatter is an escaping formatter, that handles formatting the standard Go types.
If Marshaler is not nil, then it will be passed any map[string]interface{} and []interface{} values encountered. If nil, then the standard encoding/json.Encoder will be used instead.
func NewEscapeFormatter ¶
func NewEscapeFormatter(opts ...EscapeFormatterOption) *EscapeFormatter
NewEscapeFormatter creates a escape formatter to handle basic Go values, such as []byte, string, time.Time. Formatting for map[string]interface{} and []interface{} will be passed to a marshaler provided by WithMarshaler, otherwise the standard encoding/json.Encoder will be used to marshal those values.
func (*EscapeFormatter) Format ¶
func (f *EscapeFormatter) Format(vals []interface{}) ([]*Value, error)
Format satisfies the Formatter interface.
type EscapeFormatterOption ¶
type EscapeFormatterOption func(*EscapeFormatter)
EscapeFormatterOption is an escape formatter option.
func WithEscapeJSON ¶
func WithEscapeJSON(escapeJSON bool) EscapeFormatterOption
WithEscapeJSON is an escape formatter option to escape special JSON characters in non-complex values.
func WithHeaderAlign ¶ added in v0.2.0
func WithHeaderAlign(a Align) EscapeFormatterOption
WithHeaderAlign sets the alignment of header values.
func WithInvalid ¶
func WithInvalid(invalid string) EscapeFormatterOption
WithInvalid is an escape formatter option to set the invalid value used when an invalid rune is encountered during escaping.
func WithJSONConfig ¶
func WithJSONConfig(prefix, indent string, escapeHTML bool) EscapeFormatterOption
WithJSONConfig is an escape formatter option to set the JSON encoding prefix, indent value, and whether or not to escape HTML. Passed to the standard encoding/json.Encoder when a marshaler has not been set on the escape formatter.
func WithMarshaler ¶
func WithMarshaler(marshaler func(interface{}) ([]byte, error)) EscapeFormatterOption
WithMarshaler is an escape formatter option to set a standard Go encoder to use for encoding the value.
func WithMask ¶
func WithMask(mask string) EscapeFormatterOption
WithMask is an escape formatter option to set the mask used for empty headers.
func WithTimeFormat ¶
func WithTimeFormat(timeFormat string) EscapeFormatterOption
WithTimeFormat is an escape formatter option to set the time format used for time values.
type ExpandedEncoder ¶
type ExpandedEncoder struct {
TableEncoder
}
ExpandedEncoder is a buffered, lookahead expanded table encoder for result sets.
type Formatter ¶
type Formatter interface {
// Header returns a slice of formatted values for the provided headers.
Header([]string) ([]*Value, error)
// Format returns a slice of formatted value the provided row values.
Format([]interface{}) ([]*Value, error)
}
Formatter is the common interface for formatting values.
type JSONEncoder ¶
type JSONEncoder struct {
// contains filtered or unexported fields
}
JSONEncoder is an unbuffered JSON encoder for result sets.
type LineStyle ¶
LineStyle is a table line style.
See the ASCII, OldASCII, and Unicode styles below for predefined table styles.
Tables generally look like the following:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | | | 15 | aoeu +| | | | test +| | | | | | +-----------+---------------------------+---+
When border is 0, then no surrounding borders will be shown:
author_id name z
--------- ------------------------- -
14 a b c d
15 aoeu +
test +
When border is 1, then a border between columns will be shown:
author_id | name | z
-----------+---------------------------+---
14 | a b c d |
15 | aoeu +|
| test +|
| |
func ASCIILineStyle ¶
func ASCIILineStyle() LineStyle
ASCIILineStyle is the ASCII line style for tables.
Tables using this style will look like the following:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | | | 15 | aoeu +| | | | test +| | | | | | +-----------+---------------------------+---+
func OldASCIILineStyle ¶
func OldASCIILineStyle() LineStyle
OldASCIILineStyle is the old ASCII line style for tables.
Tables using this style will look like the following:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | | | 15 | aoeu | | | : test | | : | +-----------+---------------------------+---+
func UnicodeDoubleLineStyle ¶
func UnicodeDoubleLineStyle() LineStyle
UnicodeDoubleLineStyle is the Unicode double line style for tables.
Tables using this style will look like the following:
╔═══════════╦═══════════════════════════╦═══╗ ║ author_id ║ name ║ z ║ ╠═══════════╬═══════════════════════════╬═══╣ ║ 14 ║ a b c d ║ ║ ║ 15 ║ aoeu ↵║ ║ ║ ║ test ↵║ ║ ║ ║ ║ ║ ╚═══════════╩═══════════════════════════╩═══╝
func UnicodeLineStyle ¶
func UnicodeLineStyle() LineStyle
UnicodeLineStyle is the Unicode line style for tables.
Tables using this style will look like the following:
┌───────────┬───────────────────────────┬───┐ │ author_id │ name │ z │ ├───────────┼───────────────────────────┼───┤ │ 14 │ a b c d │ │ │ 15 │ aoeu ↵│ │ │ │ test ↵│ │ │ │ │ │ └───────────┴───────────────────────────┴───┘
type Option ¶
type Option func(interface{}) error
Option is a Encoder option.
func WithBorder ¶
WithBorder is a encoder option to set the border size.
func WithFieldSeparator ¶
WithFieldSeparator is a encoder option to set the field separator.
func WithFormatter ¶
WithFormatter is a encoder option to set a formatter for formatting values.
func WithHtmlTemplate ¶ added in v0.2.0
WithHtmlTemplate is a encoder option to set the raw html template used.
func WithInline ¶
WithInline is a encoder option to set the column headers as inline to the top line.
func WithLineStyle ¶
WithLineStyle is a encoder option to set the table line style.
func WithMinExpandWidth ¶ added in v0.3.0
WithMinExpandWidth is a encoder option to set maximum width before switching to expanded format.
func WithMinPagerHeight ¶ added in v0.3.0
WithMinPagerHeight is a encoder option to set maximum height before redirecting output to pager.
func WithMinPagerWidth ¶ added in v0.3.0
WithMinPagerWidth is a encoder option to set maximum width before redirecting output to pager.
func WithNamedTemplate ¶
WithNamedTemplate is a encoder option to set the template used.
func WithNewline ¶
WithNewline is a encoder option to set the newline.
func WithSummary ¶
WithSummary is a encoder option to set a summary callback map.
func WithTableAttributes ¶ added in v0.2.0
WithTableAttributes is a encoder option to set the table attributes.
func WithTextTemplate ¶ added in v0.2.0
WithTextTemplate is a encoder option to set the raw text template used.
func WithWidths ¶
WithWidths is a encoder option to set (minimum) widths for a column.
type ResultSet ¶
type ResultSet interface {
Next() bool
Scan(...interface{}) error
Columns() ([]string, error)
Close() error
Err() error
NextResultSet() bool
}
ResultSet is the shared interface for a result set.
type TableEncoder ¶
type TableEncoder struct {
// contains filtered or unexported fields
}
TableEncoder is a buffered, lookahead table encoder for result sets.
type TemplateEncoder ¶
type TemplateEncoder struct {
// contains filtered or unexported fields
}
TemplateEncoder is an unbuffered template encoder for result sets.
type Value ¶
type Value struct {
// Buf is the formatted value.
Buf []byte
// Newlines are the positions of newline characters in Buf.
Newlines [][2]int
// Tabs are the positions of tab characters in Buf, split per line.
Tabs [][][2]int
// Width is the remaining width.
Width int
// Align indicates value alignment.
Align Align
// Raw tracks whether or not the message should be encoded or not.
Raw bool
}
Value contains information pertaining to a formatted value.
func FormatBytes ¶
FormatBytes parses src, saving escaped (encoded) and unescaped runes to a Value, along with tab and newline positions in the generated buf.