Documentation
¶
Overview ¶
Package tblfmt provides streaming table encoders for result sets (that is, result sets from a database).
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/xo/tblfmt"
)
func main() {
res := getDatabaseResults()
if err := tblfmt.EncodeAll(os.Stdout, res, map[string]string{
"format": "aligned",
"border": "2",
}); err != nil {
log.Fatal(err)
}
}
// getDatabaseResults returns a tblfmt.ResultSet. A *sql.Rows from the
// database/sql package also implements that interface.
func getDatabaseResults() tblfmt.ResultSet {
return &result{
cols: []string{"author_id", "name", "z"},
vals: [][]any{
{14, "a\tb\tc\td", nil},
{15, "aoeu\ntest\n", nil},
{2, "袈\t袈\t\t袈", nil},
},
}
}
// result is a type that implements the tblfmt.ResultSet interface.
type result struct {
pos int
cols []string
vals [][]any
}
// Columns satisfies the tblfmt.ResultSet interface.
func (res *result) Columns() ([]string, error) {
return res.cols, nil
}
// Next satisfies the tblfmt.ResultSet interface.
func (res *result) Next() bool {
return res.pos < len(res.vals)
}
// Scan satisfies the tblfmt.ResultSet interface.
func (res *result) Scan(vals ...any) error {
for i := range vals {
x, ok := vals[i].(*any)
if !ok {
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i])
}
*x = res.vals[res.pos][i]
}
res.pos++
return nil
}
// Err satisfies the tblfmt.ResultSet interface.
func (res *result) Err() error {
return nil
}
// Close satisfies the tblfmt.ResultSet interface.
func (res *result) Close() error {
return nil
}
// NextResultSet satisfies the tblfmt.ResultSet interface.
func (res *result) NextResultSet() bool {
return false
}
Output: +-----------+----------------------------+---+ | author_id | name | z | +-----------+----------------------------+---+ | 14 | a b c d | | | 15 | aoeu +| | | | test +| | | | | | | 2 | 袈 袈 袈 | | +-----------+----------------------------+---+ (3 rows)
Index ¶
- func Encode(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error
- func EncodeAll(w io.Writer, resultSet ResultSet, params map[string]string, options ...Option) error
- func EncodeAsciiDoc(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeAsciiDocAll(w io.Writer, resultSet ResultSet, opts ...Option) 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 EncodeHTML(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeHTMLAll(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 EncodeUnaligned(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeUnalignedAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeVertical(w io.Writer, resultSet ResultSet, opts ...Option) error
- func EncodeVerticalAll(w io.Writer, resultSet ResultSet, opts ...Option) error
- func FromMap(opts map[string]string) (Builder, []Option)
- func WriteAsciidocTo(w io.Writer, tpl *Template) error
- func WriteHTMLTo(w io.Writer, tpl *Template) error
- func WriteLaTeXLongtableTo(w io.Writer, tpl *Template) error
- func WriteLaTeXTo(w io.Writer, tpl *Template) error
- func WriteTroffMSTo(w io.Writer, tpl *Template) error
- func WriteVerticalTo(w io.Writer, tpl *Template) error
- type Align
- type Builder
- type CrosstabView
- type Encoder
- func NewAsciiDocEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewCSVEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewExpandedEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewHTMLEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewJSONEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewLaTeXEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewLaTeXLongtableEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewTableEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewTemplateEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewTroffMSEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewUnalignedEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- func NewVerticalEncoder(resultSet ResultSet, opts ...Option) (Encoder, error)
- type Error
- type EscapeFormatter
- type EscapeFormatterOption
- func WithAlign(a Align) EscapeFormatterOption
- func WithEncoder(encoder func(any) ([]byte, error)) EscapeFormatterOption
- func WithHeaderAlign(a Align) EscapeFormatterOption
- func WithInvalid(invalid string) EscapeFormatterOption
- func WithIsJSON(isJSON bool) EscapeFormatterOption
- func WithIsRaw(isRaw bool, sep, quote rune) EscapeFormatterOption
- func WithJSONConfig(prefix, indent string, escapeHTML bool) EscapeFormatterOption
- func WithMask(mask string) EscapeFormatterOption
- func WithNumericLocale(enable bool, locale string) EscapeFormatterOption
- func WithTimeFormat(timeFormat string) EscapeFormatterOption
- func WithTimeLocation(timeLocation *time.Location) EscapeFormatterOption
- type ExpandedEncoder
- type Formatter
- type JSONEncoder
- type LineStyle
- type Option
- func FormatterOptionFromMap(opts map[string]string) Option
- func WithBorder(border int) Option
- func WithColumnAlign(f func(*sql.ColumnType) (Align, bool)) Option
- func WithColumnTypes(columnTypes func(ResultSet, []any, int) error) Option
- func WithColumnTypesFunc(f func(*sql.ColumnType) (any, error)) Option
- func WithCount(count int) Option
- func WithEmpty(empty string) Option
- func WithExecutor(executor func(io.Writer, *Template) error) Option
- func WithExpanded(expanded bool) Option
- func WithForceUpperColumnNames(forceUpper bool) Option
- func WithFormatter(formatter Formatter) Option
- func WithFormatterOptions(opts ...EscapeFormatterOption) Option
- func WithHeaderTransformer(headerTransformer Transformer) Option
- func WithIndent(indent string) Option
- func WithLineStyle(lineStyle LineStyle) Option
- func WithLowerColumnNames(lowerColumnNames bool) Option
- func WithMinExpandWidth(w int) Option
- func WithNewline(newline string) Option
- func WithParams(params ...string) Option
- func WithQuote(quote rune) Option
- func WithResultSetSeparator(sep string) Option
- func WithSeparator(sep rune) Option
- func WithSkipHeader(s bool) Option
- func WithSummary(summary Summary) Option
- func WithTableAttributes(a string) Option
- func WithTemplate(name string) Option
- func WithTitle(title string) Option
- func WithUseColumnTypes(useColumnTypes bool) Option
- func WithWidths(widths ...int) Option
- func WithWrapWidth(width int) Option
- type ResultSet
- type Rule
- type Summary
- type TableEncoder
- type Template
- type TemplateEncoder
- type TransformStyle
- type Transformer
- type UnalignedEncoder
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
Encode encodes the result set to the writer. It uses the parameters in the map and any other options that you supply.
func EncodeAll ¶
EncodeAll encodes all result sets to the writer. It uses the parameters in the map and any other options that you supply.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/xo/tblfmt"
)
func main() {
res := getDatabaseResults()
if err := tblfmt.EncodeAll(os.Stdout, res, map[string]string{
"format": "csv",
"fieldsep": "|",
"null": "<nil>",
}); err != nil {
log.Fatal(err)
}
}
// getDatabaseResults returns a tblfmt.ResultSet. A *sql.Rows from the
// database/sql package also implements that interface.
func getDatabaseResults() tblfmt.ResultSet {
return &result{
cols: []string{"author_id", "name", "z"},
vals: [][]any{
{14, "a\tb\tc\td", nil},
{15, "aoeu\ntest\n", nil},
{2, "袈\t袈\t\t袈", nil},
},
}
}
// result is a type that implements the tblfmt.ResultSet interface.
type result struct {
pos int
cols []string
vals [][]any
}
// Columns satisfies the tblfmt.ResultSet interface.
func (res *result) Columns() ([]string, error) {
return res.cols, nil
}
// Next satisfies the tblfmt.ResultSet interface.
func (res *result) Next() bool {
return res.pos < len(res.vals)
}
// Scan satisfies the tblfmt.ResultSet interface.
func (res *result) Scan(vals ...any) error {
for i := range vals {
x, ok := vals[i].(*any)
if !ok {
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i])
}
*x = res.vals[res.pos][i]
}
res.pos++
return nil
}
// Err satisfies the tblfmt.ResultSet interface.
func (res *result) Err() error {
return nil
}
// Close satisfies the tblfmt.ResultSet interface.
func (res *result) Close() error {
return nil
}
// NextResultSet satisfies the tblfmt.ResultSet interface.
func (res *result) NextResultSet() bool {
return false
}
Output: author_id,name,z 14,"a b c d",<nil> 15,"aoeu test ",<nil> 2,"袈 袈 袈",<nil>
func EncodeAsciiDoc ¶ added in v0.6.2
EncodeAsciiDoc encodes the result set to the writer with the asciidoc template and the options that you supply.
func EncodeAsciiDocAll ¶ added in v0.6.2
EncodeAsciiDocAll encodes all result sets to the writer with the asciidoc template and the options that you supply.
func EncodeCSV ¶
EncodeCSV encodes the result set to the writer as unaligned CSV, with the options that you supply.
func EncodeCSVAll ¶
EncodeCSVAll encodes all result sets to the writer as unaligned CSV, with the options that you supply.
func EncodeExpanded ¶
EncodeExpanded encodes the result set to the writer as an expanded table, with the options that you supply.
func EncodeExpandedAll ¶
EncodeExpandedAll encodes all result sets to the writer as an expanded table, with the options that you supply.
func EncodeHTML ¶ added in v0.6.2
EncodeHTML encodes the result set to the writer with the html template and the options that you supply.
func EncodeHTMLAll ¶ added in v0.6.2
EncodeHTMLAll encodes all result sets to the writer with the html template and the options that you supply.
func EncodeJSON ¶
EncodeJSON encodes the result set to the writer as JSON, with the options that you supply.
func EncodeJSONAll ¶
EncodeJSONAll encodes all result sets to the writer as JSON, with the options that you supply.
func EncodeTable ¶
EncodeTable encodes the result set to the writer as a table, with the options that you supply.
func EncodeTableAll ¶
EncodeTableAll encodes all result sets to the writer as a table, with the options that you supply.
func EncodeTemplate ¶
EncodeTemplate encodes the result set to the writer with a template. The options that you supply set the template.
func EncodeTemplateAll ¶
EncodeTemplateAll encodes all result sets to the writer with a template. The options that you supply set the template.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/xo/tblfmt"
)
func main() {
res := getDatabaseResults()
if err := tblfmt.EncodeTemplateAll(os.Stdout, res, tblfmt.WithTemplate("html")); err != nil {
log.Fatal(err)
}
}
// getDatabaseResults returns a tblfmt.ResultSet. A *sql.Rows from the
// database/sql package also implements that interface.
func getDatabaseResults() tblfmt.ResultSet {
return &result{
cols: []string{"author_id", "name", "z"},
vals: [][]any{
{14, "a\tb\tc\td", nil},
{15, "aoeu\ntest\n", nil},
{2, "袈\t袈\t\t袈", nil},
},
}
}
// result is a type that implements the tblfmt.ResultSet interface.
type result struct {
pos int
cols []string
vals [][]any
}
// Columns satisfies the tblfmt.ResultSet interface.
func (res *result) Columns() ([]string, error) {
return res.cols, nil
}
// Next satisfies the tblfmt.ResultSet interface.
func (res *result) Next() bool {
return res.pos < len(res.vals)
}
// Scan satisfies the tblfmt.ResultSet interface.
func (res *result) Scan(vals ...any) error {
for i := range vals {
x, ok := vals[i].(*any)
if !ok {
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i])
}
*x = res.vals[res.pos][i]
}
res.pos++
return nil
}
// Err satisfies the tblfmt.ResultSet interface.
func (res *result) Err() error {
return nil
}
// Close satisfies the tblfmt.ResultSet interface.
func (res *result) Close() error {
return nil
}
// NextResultSet satisfies the tblfmt.ResultSet interface.
func (res *result) NextResultSet() bool {
return false
}
Output: <table border="1"> <tr> <th align="center">author_id</th> <th align="center">name</th> <th align="center">z</th> </tr> <tr valign="top"> <td align="right">14</td> <td align="left">a b c d</td> <td align="left"> </td> </tr> <tr valign="top"> <td align="right">15</td> <td align="left">aoeu<br /> test<br /> </td> <td align="left"> </td> </tr> <tr valign="top"> <td align="right">2</td> <td align="left">袈 袈 袈</td> <td align="left"> </td> </tr> </table> <p>(3 rows)<br /> </p>
func EncodeUnaligned ¶ added in v0.5.0
EncodeUnaligned encodes the result set to the writer as unaligned text, with the options that you supply.
func EncodeUnalignedAll ¶ added in v0.5.0
EncodeUnalignedAll encodes all result sets to the writer as unaligned text, with the options that you supply.
func EncodeVertical ¶ added in v0.6.2
EncodeVertical encodes the result set to the writer with the vertical template and the options that you supply.
func EncodeVerticalAll ¶ added in v0.6.2
EncodeVerticalAll encodes all result sets to the writer with the vertical template and the options that you supply.
func FromMap ¶
FromMap returns the Builder and the options for the format and the other parameters in the map.
Note: this func is mainly a helper for parameter names that are like the format option names of psql.
func WriteAsciidocTo ¶ added in v0.17.0
WriteAsciidocTo writes AsciiDoc output to the writer, as psql print_asciidoc_text and print_asciidoc_vertical do.
func WriteHTMLTo ¶ added in v0.17.0
WriteHTMLTo writes HTML output to the writer, as psql print_html_text and print_html_vertical do.
func WriteLaTeXLongtableTo ¶ added in v0.21.0
WriteLaTeXLongtableTo writes LaTeX longtable output to the writer, as psql print_latex_longtable_text does. The attributes hold the widths of the left aligned columns, as fractions of the text width. Expanded output is the same as for WriteLaTeXTo.
func WriteLaTeXTo ¶ added in v0.21.0
WriteLaTeXTo writes LaTeX output to the writer, as psql print_latex_text and print_latex_vertical do.
func WriteTroffMSTo ¶ added in v0.21.0
WriteTroffMSTo writes troff output with the ms macros to the writer, as psql print_troff_ms_text and print_troff_ms_vertical do.
func WriteVerticalTo ¶ added in v0.17.0
WriteVerticalTo writes vertical output to the writer, as the mysql client writes a result for \G. It right aligns each column name to the widest name, by display width. With SkipHeader, it writes no names, as mysql does with --skip-column-names. See D60 in docs/PLAN.md.
Types ¶
type CrosstabView ¶ added in v0.5.0
type CrosstabView struct {
// contains filtered or unexported fields
}
CrosstabView is a crosstab view for result sets.
CAUTION:
By design, a crosstab view does not support multiple result sets. You must create a new crosstab view for each result set. Thus, NextResultSet always returns false. If you use this view inside a loop, or give it to other code that calls NextResultSet, be careful.
func (*CrosstabView) Close ¶ added in v0.5.0
func (view *CrosstabView) Close() error
Close satisfies the ResultSet interface.
func (*CrosstabView) Columns ¶ added in v0.5.0
func (view *CrosstabView) Columns() ([]string, error)
Columns satisfies the ResultSet interface.
func (*CrosstabView) Err ¶ added in v0.5.0
func (view *CrosstabView) Err() error
Err satisfies the ResultSet interface.
func (*CrosstabView) Next ¶ added in v0.5.0
func (view *CrosstabView) Next() bool
Next satisfies the ResultSet interface.
func (*CrosstabView) NextResultSet ¶ added in v0.5.0
func (view *CrosstabView) NextResultSet() bool
NextResultSet satisfies the ResultSet interface.
func (*CrosstabView) Scan ¶ added in v0.5.0
func (view *CrosstabView) Scan(v ...any) error
Scan satisfies the ResultSet interface.
type Encoder ¶
Encoder is the shared interface for encoders.
func NewAsciiDocEncoder ¶ added in v0.5.0
NewAsciiDocEncoder creates a template encoder for AsciiDoc with the options.
func NewCSVEncoder ¶
NewCSVEncoder creates a CSV encoder with the options.
It creates an unaligned encoder. By default, the field separator is ',' and the field quote is '"'.
func NewExpandedEncoder ¶
NewExpandedEncoder creates an expanded table encoder with the options.
func NewHTMLEncoder ¶ added in v0.5.0
NewHTMLEncoder creates a template encoder for HTML with the options.
func NewJSONEncoder ¶
NewJSONEncoder creates a JSON encoder with the options.
func NewLaTeXEncoder ¶ added in v0.21.0
NewLaTeXEncoder creates a template encoder for LaTeX with the options.
func NewLaTeXLongtableEncoder ¶ added in v0.21.0
NewLaTeXLongtableEncoder creates a template encoder for the LaTeX longtable package with the options.
func NewTableEncoder ¶
NewTableEncoder creates a table encoder with the options.
By default, the table encoder has a border of 1.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/xo/tblfmt"
)
func main() {
res := getDatabaseResults()
enc, err := tblfmt.NewTableEncoder(
res,
tblfmt.WithBorder(2),
tblfmt.WithLineStyle(tblfmt.UnicodeLineStyle(true, false, false)),
tblfmt.WithWidths(20, 20),
tblfmt.WithSummary(tblfmt.DefaultTableSummary()),
)
if err != nil {
log.Fatal(err)
}
if err := enc.EncodeAll(os.Stdout); err != nil {
log.Fatal(err)
}
}
// getDatabaseResults returns a tblfmt.ResultSet. A *sql.Rows from the
// database/sql package also implements that interface.
func getDatabaseResults() tblfmt.ResultSet {
return &result{
cols: []string{"author_id", "name", "z"},
vals: [][]any{
{14, "a\tb\tc\td", nil},
{15, "aoeu\ntest\n", nil},
{2, "袈\t袈\t\t袈", nil},
},
}
}
// result is a type that implements the tblfmt.ResultSet interface.
type result struct {
pos int
cols []string
vals [][]any
}
// Columns satisfies the tblfmt.ResultSet interface.
func (res *result) Columns() ([]string, error) {
return res.cols, nil
}
// Next satisfies the tblfmt.ResultSet interface.
func (res *result) Next() bool {
return res.pos < len(res.vals)
}
// Scan satisfies the tblfmt.ResultSet interface.
func (res *result) Scan(vals ...any) error {
for i := range vals {
x, ok := vals[i].(*any)
if !ok {
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i])
}
*x = res.vals[res.pos][i]
}
res.pos++
return nil
}
// Err satisfies the tblfmt.ResultSet interface.
func (res *result) Err() error {
return nil
}
// Close satisfies the tblfmt.ResultSet interface.
func (res *result) Close() error {
return nil
}
// NextResultSet satisfies the tblfmt.ResultSet interface.
func (res *result) NextResultSet() bool {
return false
}
Output: ╔══════════════════════╤════════════════════════════╤═══╗ ║ author_id │ name │ z ║ ╟──────────────────────┼────────────────────────────┼───╢ ║ 14 │ a b c d │ ║ ║ 15 │ aoeu ↵│ ║ ║ │ test ↵│ ║ ║ │ │ ║ ║ 2 │ 袈 袈 袈 │ ║ ╚══════════════════════╧════════════════════════════╧═══╝ (3 rows)
func NewTemplateEncoder ¶
NewTemplateEncoder creates a template encoder with the options.
func NewTroffMSEncoder ¶ added in v0.21.0
NewTroffMSEncoder creates a template encoder for troff with the ms macros, with the options.
func NewUnalignedEncoder ¶ added in v0.5.0
NewUnalignedEncoder creates an unaligned encoder with the 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" // ErrResultSetHasNoColumnTypes is the result set has no column types error. ErrResultSetHasNoColumnTypes Error = "result set has no column types" // ErrResultSetReturnedInvalidColumnTypes is the result set returned invalid column types error. ErrResultSetReturnedInvalidColumnTypes Error = "result set returned invalid column types" // ErrInvalidFormat is the invalid format error. ErrInvalidFormat Error = "invalid format" // ErrInvalidLineStyle is the invalid line style error. ErrInvalidLineStyle Error = "invalid line style" // ErrFormatterIsNotEscapeFormatter is the formatter is not an escape // formatter error. ErrFormatterIsNotEscapeFormatter Error = "formatter options need an escape formatter" // ErrInvalidIndent is the invalid indent error. ErrInvalidIndent Error = "invalid indent" // ErrInvalidTemplate is the invalid template error. ErrInvalidTemplate Error = "invalid template" // ErrInvalidFieldSeparator is the invalid field separator error. ErrInvalidFieldSeparator Error = "invalid field separator" // ErrInvalidCSVFieldSeparator is the invalid csv field separator error. ErrInvalidCSVFieldSeparator Error = "invalid csv field separator" // ErrInvalidColumnParams is the invalid column params error. ErrInvalidColumnParams Error = "invalid column params" // ErrCrosstabResultMustHaveAtLeast3Columns is the crosstab result must // have at least 3 columns error. ErrCrosstabResultMustHaveAtLeast3Columns Error = "crosstab result must have at least 3 columns" // ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumns // is the data column must be specified when query returns more than three // columns error. ErrCrosstabDataColumnMustBeSpecifiedWhenQueryReturnsMoreThanThreeColumns Error = "data column must be specified when query returns more than three columns" // ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame is the crosstab // vertical and horizontal columns must not be same error. ErrCrosstabVerticalAndHorizontalColumnsMustNotBeSame Error = "crosstab vertical and horizontal columns must not be same" // ErrCrosstabVerticalColumnNotInResult is the crosstab vertical column not // in result error. ErrCrosstabVerticalColumnNotInResult Error = "crosstab vertical column not in result" // ErrCrosstabHorizontalColumnNotInResult is the crosstab horizontal column // not in result error. ErrCrosstabHorizontalColumnNotInResult Error = "crosstab horizontal column not in result" // ErrCrosstabDataColumnNotInResult is the crosstab data column not in // result error. ErrCrosstabDataColumnNotInResult Error = "crosstab data column not in result" // ErrCrosstabHorizontalSortColumnNotInResult is the crosstab horizontal // sort column not in result error. ErrCrosstabHorizontalSortColumnNotInResult Error = "crosstab horizontal sort column not in result" // ErrCrosstabDuplicateVerticalAndHorizontalValue is the crosstab duplicate // vertical and horizontal value error. ErrCrosstabDuplicateVerticalAndHorizontalValue Error = "crosstab duplicate vertical and horizontal value" // ErrCrosstabHorizontalSortColumnIsNotANumber is the crosstab horizontal // sort column is not a number error. ErrCrosstabHorizontalSortColumnIsNotANumber Error = "crosstab horizontal sort column is not a number" )
Error values.
type EscapeFormatter ¶
type EscapeFormatter struct {
// contains filtered or unexported fields
}
EscapeFormatter is a formatter that escapes values. It formats the standard Go types.
If a marshal func is set with WithEncoder, the formatter passes it each map[string]interface{} and []interface{} value. Otherwise, the formatter uses encoding/json/v2 from the standard library.
func NewEscapeFormatter ¶
func NewEscapeFormatter(opts ...EscapeFormatterOption) *EscapeFormatter
NewEscapeFormatter creates an escape formatter for basic Go values, such as []byte, string, time.Time, sql.Null*, and any database/sql/driver.Valuer. The formatter passes map[string]interface{} and []interface{} values to the marshal func set with WithEncoder. Otherwise, it uses the standard encoding/json/v2 to marshal those values.
type EscapeFormatterOption ¶
type EscapeFormatterOption func(*EscapeFormatter)
EscapeFormatterOption is an escape formatter option.
func WithAlign ¶ added in v0.18.0
func WithAlign(a Align) EscapeFormatterOption
WithAlign sets the forced alignment for values.
func WithEncoder ¶ added in v0.10.0
func WithEncoder(encoder func(any) ([]byte, error)) EscapeFormatterOption
WithEncoder is an escape formatter option that sets a standard Go marshal func to encode the value.
func WithHeaderAlign ¶ added in v0.2.0
func WithHeaderAlign(a Align) EscapeFormatterOption
WithHeaderAlign sets the alignment of the column names in the header.
func WithInvalid ¶
func WithInvalid(invalid string) EscapeFormatterOption
WithInvalid is an escape formatter option that sets the text that replaces an invalid rune in the escape.
func WithIsJSON ¶ added in v0.5.0
func WithIsJSON(isJSON bool) EscapeFormatterOption
WithIsJSON is an escape formatter option that turns on a special escape for JSON characters in values that are not complex.
func WithIsRaw ¶ added in v0.5.0
func WithIsRaw(isRaw bool, sep, quote rune) EscapeFormatterOption
WithIsRaw is an escape formatter option that turns on a special escape for raw characters in values.
func WithJSONConfig ¶
func WithJSONConfig(prefix, indent string, escapeHTML bool) EscapeFormatterOption
WithJSONConfig is an escape formatter option that sets the JSON prefix, the JSON indent, and whether to escape HTML. The formatter passes them to encoding/json/v2 if no marshal func is set on the escape formatter.
The prefix and indent must contain only spaces and tabs, and are ignored otherwise. Output is compact when both are empty.
func WithMask ¶
func WithMask(mask string) EscapeFormatterOption
WithMask is an escape formatter option that sets the mask for an empty column name in the header.
func WithNumericLocale ¶ added in v0.10.0
func WithNumericLocale(enable bool, locale string) EscapeFormatterOption
WithNumericLocale sets the numeric locale printer. The printer groups the digits of a number as the locale does, the same as \pset numericlocale in psql.
It has no effect on JSON output. A grouped number is not a JSON number. A JSON string for it makes the JSON type of a column depend on a display option. Every other format applies it. The csv output quotes each field that needs it, exactly as psql does.
func WithTimeFormat ¶
func WithTimeFormat(timeFormat string) EscapeFormatterOption
WithTimeFormat is an escape formatter option that sets the time format for time values.
func WithTimeLocation ¶ added in v0.13.1
func WithTimeLocation(timeLocation *time.Location) EscapeFormatterOption
WithTimeLocation is an escape formatter option that sets the time location for time values.
type ExpandedEncoder ¶
type ExpandedEncoder struct {
TableEncoder
// contains filtered or unexported fields
}
ExpandedEncoder is an encoder that writes a result set as an expanded table. It writes each row as a record, with one line for each column. It buffers a batch of rows ahead, as TableEncoder does.
type Formatter ¶
type Formatter interface {
// Header returns a slice of formatted values for the column names of a
// header.
Header([]string) ([]*Value, error)
// Format returns a slice of formatted values for the values of a row.
Format([]any) ([]*Value, error)
}
Formatter is the common interface that formats values.
type JSONEncoder ¶
type JSONEncoder struct {
// contains filtered or unexported fields
}
JSONEncoder is an encoder that writes a result set as JSON. It does not buffer rows.
type LineStyle ¶
type LineStyle struct {
// Top is the line above the header. Mid is the line under the header,
// and End is the line under the last row. A rule with an empty Line
// writes no line.
Top Rule
Mid Rule
End Rule
// Row holds the vertical lines of a row. Its Line is not used.
Row Rule
// MidNewline is the vertical line before a value that continues after a
// newline. MidBlank is the vertical line before a value that has no more
// lines. psql draws MidWrap before a wrapped value. In expanded output, it
// also draws MidWrap beside each further line of a column name after a
// value that is not empty ends.
MidNewline string
MidBlank string
MidWrap string
// HeaderNewlineLeft and HeaderNewlineRight mark a line of a column name
// that continues after a newline.
HeaderNewlineLeft string
HeaderNewlineRight string
// NewlineLeft and NewlineRight mark a line of a value that continues
// after a newline. WrapLeft and WrapRight mark a line of a value that
// continues after a wrap.
NewlineLeft string
NewlineRight string
WrapLeft string
WrapRight string
// LeftMarks is true when the style marks a continued line on the left of
// a value, as the psql old-ascii style does. With a border of 0, such a
// style writes no mark at the right of a column name.
LeftMarks bool
}
LineStyle is a line style for tables. It holds the parts of a psql line style.
The TableLineStyle, ASCIILineStyle, OldASCIILineStyle and UnicodeLineStyle funcs below return the predefined line styles.
A table with a border of 2 looks like this:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | | | 15 | aoeu +| | | | test +| | | | | | +-----------+---------------------------+---+
If the border is 1, the encoder writes the vertical lines between the columns:
author_id | name | z
-----------+---------------------------+---
14 | a b c d | x
15 | aoeu +|
| test +|
| |
If the border is 0, the encoder writes no vertical line:
author_id name z
--------- ------------------------- -
14 a b c d x
15 aoeu +
test +
func ASCIILineStyle ¶
func ASCIILineStyle() LineStyle
ASCIILineStyle is the ASCII line style for tables, as psql draws it.
A table with this line style looks like this:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | x | | 15 | aoeu +| | | | test +| | | | | | +-----------+---------------------------+---+
func OldASCIILineStyle ¶
func OldASCIILineStyle() LineStyle
OldASCIILineStyle is the old ASCII line style for tables, as psql draws it. It marks a value that continues after a newline with a colon on its left.
A table with this line style looks like this:
+-----------+---------------------------+---+ | author_id | name | z | +-----------+---------------------------+---+ | 14 | a b c d | x | | 15 | aoeu | | | : test | | : | +-----------+---------------------------+---+
func TableLineStyle ¶ added in v0.18.0
func TableLineStyle() LineStyle
TableLineStyle is the table line style. It draws no line.
A table with this line style looks like this:
AUTHOR_ID NAME Z
14 a b c d x
15 aoeu
test
func UnicodeLineStyle ¶
UnicodeLineStyle is the Unicode line style for tables, as psql draws it. Each argument draws one part of the table with double lines, as the psql variables unicode_border_linestyle, unicode_header_linestyle and unicode_column_linestyle do.
A table with single lines looks like this:
┌───────────┬───────────────────────────┬───┐ │ author_id │ name │ z │ ├───────────┼───────────────────────────┼───┤ │ 14 │ a b c d │ x │ │ 15 │ aoeu ↵│ │ │ │ test ↵│ │ │ │ │ │ └───────────┴───────────────────────────┴───┘
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is an encoder option.
func FormatterOptionFromMap ¶ added in v0.11.1
FormatterOptionFromMap builds an option that sets the formatter options from the parameters in the map.
func WithBorder ¶
WithBorder is an encoder option that sets the border size.
func WithColumnAlign ¶ added in v0.21.0
func WithColumnAlign(f func(*sql.ColumnType) (Align, bool)) Option
WithColumnAlign is an encoder option that sets the alignment of each column from its database type, before the encoder reads a row. For each column, f gets the *sql.ColumnType, and returns the alignment and true, or false to align the column by its values. The alignment holds for each value of the column, a null value too, and for a result set that has no rows.
The result set must have a ColumnTypes method, as *sql.Rows has. The table and template encoders read it.
For example, psql right aligns the numbers, and the money type:
tblfmt.WithColumnAlign(func(col *sql.ColumnType) (tblfmt.Align, bool) {
switch col.DatabaseTypeName() {
case "INT2", "INT4", "INT8", "FLOAT4", "FLOAT8", "NUMERIC", "OID", "MONEY":
return tblfmt.AlignRight, true
}
return tblfmt.AlignLeft, true
})
func WithColumnTypes ¶ added in v0.12.0
WithColumnTypes is an encoder option that sets the func that builds the column types.
func WithColumnTypesFunc ¶ added in v0.12.0
func WithColumnTypesFunc(f func(*sql.ColumnType) (any, error)) Option
WithColumnTypesFunc is an encoder option that sets a func that builds the type of each column.
func WithExecutor ¶ added in v0.5.0
WithExecutor is an encoder option that sets the executor.
func WithExpanded ¶ added in v0.21.0
WithExpanded is an encoder option that writes each row as a record, as psql does with the variable expanded on. The unaligned and csv encoders and the html, asciidoc, latex, latex-longtable and troff-ms templates read it. The table encoder has NewExpandedEncoder instead.
func WithForceUpperColumnNames ¶ added in v0.18.0
WithForceUpperColumnNames is an encoder option that changes all column names to upper case. See TransformForceUpper.
func WithFormatter ¶
WithFormatter is an encoder option that sets the formatter for values.
func WithFormatterOptions ¶ added in v0.7.1
func WithFormatterOptions(opts ...EscapeFormatterOption) Option
WithFormatterOptions is an encoder option that adds more formatter options. The formatter of the encoder must be an EscapeFormatter. For any other formatter, the encoder returns ErrFormatterIsNotEscapeFormatter.
func WithHeaderTransformer ¶ added in v0.18.0
func WithHeaderTransformer(headerTransformer Transformer) Option
WithHeaderTransformer is an encoder option that sets the transform style for the header.
func WithIndent ¶ added in v0.21.0
WithIndent is an encoder option that sets the indent of the JSON encoder. By default, the indent is empty, and the encoder writes each result set as compact JSON on one line.
The indent must contain only spaces and tabs.
func WithLineStyle ¶
WithLineStyle is an encoder option that sets the line style of the table.
func WithLowerColumnNames ¶ added in v0.7.5
WithLowerColumnNames is an encoder option that changes the column names to lower case when they are all upper case. See TransformUpperToLower.
func WithMinExpandWidth ¶ added in v0.3.0
WithMinExpandWidth is an encoder option that sets the maximum width before the encoder switches to the expanded format.
func WithNewline ¶
WithNewline is an encoder option that sets the newline.
func WithParams ¶ added in v0.5.0
WithParams is a view option that sets the column parameters.
func WithQuote ¶ added in v0.5.0
WithQuote is an encoder option that sets the quote character for fields.
func WithResultSetSeparator ¶ added in v0.21.0
WithResultSetSeparator is an encoder option that sets the text that EncodeAll writes between two result sets. By default, EncodeAll writes the newline.
func WithSeparator ¶ added in v0.5.0
WithSeparator is an encoder option that sets the field separator.
func WithSkipHeader ¶ added in v0.4.0
WithSkipHeader is an encoder option that stops the encoder from writing the header.
func WithSummary ¶
WithSummary is an encoder option that sets the summary of the table. The JSON encoder writes no summary.
func WithTableAttributes ¶ added in v0.2.0
WithTableAttributes is an encoder option that sets the table attributes.
func WithTemplate ¶
WithTemplate is an encoder option that sets the template by its name.
func WithUseColumnTypes ¶ added in v0.7.0
WithUseColumnTypes is an encoder option that makes the encoder use the column types of the result set.
func WithWidths ¶
WithWidths is an encoder option that sets the minimum width of each column. Only the table encoders read it, because only they pad a column (D61).
func WithWrapWidth ¶ added in v0.21.0
WithWrapWidth is an encoder option that wraps the values of a table, so that the table fits in the width, as the psql format wrapped does. The encoder does not make a column narrower than its name. Zero turns off wrapping.
type ResultSet ¶
type ResultSet interface {
Next() bool
Scan(...any) error
Columns() ([]string, error)
Close() error
Err() error
NextResultSet() bool
}
ResultSet is the shared interface for a result set.
type Rule ¶ added in v0.21.0
Rule holds the strings of one kind of line in a table: the horizontal line, and the vertical lines at the left, between two columns, and at the right.
type Summary ¶ added in v0.13.0
Summary maps a row count to the func that writes the summary for that count. The key -1 holds the func for any other count.
func DefaultTableSummary ¶
func DefaultTableSummary() Summary
DefaultTableSummary is the default summary for tables.
The default summary looks like this:
(3 rows)
type TableEncoder ¶
type TableEncoder struct {
// contains filtered or unexported fields
}
TableEncoder is an encoder that writes a result set as a table. It buffers a batch of rows ahead to find the widths of the columns.
type Template ¶ added in v0.17.0
type Template struct {
Attributes string
Headers []*Value
Rows [][]*Value
SkipHeader bool
Title *Value
// Aligns are the alignments of the columns.
Aligns []Align
// Border is the border, as the psql variable border sets it.
Border int
// Expanded is true when the template writes each row as a record.
Expanded bool
// rule of psql: a table gets the row count or the custom summary, and
// expanded records get only a custom summary.
Footer string
}
Template holds the data for a template.
type TemplateEncoder ¶
type TemplateEncoder struct {
// contains filtered or unexported fields
}
TemplateEncoder is a template encoder for result sets.
Note: the encoder reads every row of a result set into memory before it runs the template, because the template receives all the rows at once.
type TransformStyle ¶ added in v0.18.0
type TransformStyle int
TransformStyle is a transform style for column names in the header.
const ( TransformNone TransformStyle = iota TransformForceLower TransformForceUpper TransformUpperToLower TransformLowerToUpper )
Transform styles.
func (TransformStyle) Transform ¶ added in v0.18.0
func (style TransformStyle) Transform(s string) string
Transform transforms s with the style. It satisfies the Transformer interface.
type Transformer ¶ added in v0.18.0
Transformer is the interface for column transformers.
type UnalignedEncoder ¶ added in v0.5.0
type UnalignedEncoder struct {
// contains filtered or unexported fields
}
UnalignedEncoder is an encoder that writes a result set with no alignment. It does not buffer rows.
You can use it to encode a result set in formats such as comma-separated values (CSV) or tab-separated values (TSV).
By default, the field separator is '|', there is no quote character, and the record separator is the default newline for the platform ("\r\n" on Windows, "\n" otherwise).
func (*UnalignedEncoder) Encode ¶ added in v0.5.0
func (enc *UnalignedEncoder) Encode(w io.Writer) error
Encode encodes one result set to the writer with the options of the encoder.
The encoder writes the result set as psql does in print_unaligned_text, print_unaligned_vertical, print_csv_text and print_csv_vertical, in src/fe_utils/print.c.
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 is the alignment of the value.
Align Align
// Raw is true when the JSON encoder writes Buf exactly, with no quotes.
Raw bool
// Quoted tracks whether a raw value must be quoted, that is, whether it
// contains a space or a non printable character.
Quoted bool
}
Value holds a formatted value and data about it.
func FormatBytes ¶
func FormatBytes(src []byte, invalid []byte, invalidWidth int, isJSON, isRaw bool, sep, quote rune) *Value
FormatBytes escapes src to a Value. The Value holds the escaped (encoded) and unescaped runes, and the positions of the tabs and newlines in its Buf.
func (*Value) LineWidth ¶
LineWidth returns the display width of line l. A tab moves to the next multiple of 8 columns from the start of the line, as psql writes it.