format

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package format renders tool results as compact, bounded text: CSV tables with a footer line, unit helpers and error text. Every tool renderer uses only this package to write its output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Approx

func Approx(n int64) string

Approx formats a count: exact below 1 000, then 1.2k, 16k, 210M, 1.9B.

func CSVLine

func CSVLine(fields ...string) string

CSVLine renders fields as one CSV line with the escaping and quoting of Render, ending with a newline. Callers that build a small table of their own use it instead of a second CSV writer.

func Duration

func Duration(d time.Duration) string

Duration formats a duration: 0.4 ms below 1 ms, 38 ms up to 1 s, then 6.1 s.

func Error

func Error(err error) string

Error renders err for a tool result: "ERROR: ..." lines, at most 2000 bytes. A connection error is rendered as such even when it wraps a Postgres error.

func Percent

func Percent(frac float64) string

Percent formats a ratio given as a 0-1 fraction: 0.992 gives 99.2%.

func PercentChange

func PercentChange(frac float64) string

PercentChange formats a change given as a fraction, always signed: -0.998 gives -99.8%, 0.065 gives +6.5%.

func Render

func Render(t Table, maxBytes, maxCell int, d time.Duration) (string, error)

Render writes the table as CSV followed by its footer. The whole text, prefix and footer included, stays within maxBytes: rendering stops before the first row that would exceed the budget.

func RenderTag

func RenderTag(tag string, d time.Duration) string

RenderTag renders the footer of a statement that returns no rows: "-- UPDATE 3, 3 ms".

func Shorten

func Shorten(s string, max int) string

Shorten cuts s to its first max code points followed by "…[+N chars]", N being the number of code points removed. A value that would lose fewer than 20 code points is returned unchanged.

func Size

func Size(bytes int64) string

Size formats a byte count, 1024-based, in the style of pg_size_pretty: 512 bytes, 2.3 kB, 120 MB, 1.4 GB.

func SummarizeTSVector

func SummarizeTSVector(s string) (summary string, ok bool)

SummarizeTSVector returns "[tsvector: <n> lexemes]" for the text form of a tsvector ("[tsvector: 1 lexeme]" for one). Lexemes are single-quoted, a quote inside is doubled, a backslash escapes the next character, and each is optionally followed by ":positions"; entries are separated by spaces. ok is false when the text is not a well-formed tsvector, so that the caller can keep the original text.

func SummarizeVector added in v0.4.0

func SummarizeVector(s string) (summary string, ok bool)

SummarizeVector returns "[vector: <n> dims]" for the text form of a pgvector value, "[0.1,-0.2,3e-05]". ok is false when the text is not a well-formed one, so that the caller can keep the original text. The caller must already know the column is of pgvector's type: json and a user's range type render the same way.

func WithSQL

func WithSQL(err error, sql string) error

WithSQL attaches the statement text to err; Error then shows the line holding a Postgres error position.

Types

type Cell

type Cell []byte

Cell is the Postgres text value of one field; nil means NULL.

func Cut added in v0.3.3

func Cut(c []byte, max int) (Cell, bool)

Cut is Shorten on a cell read from the server: it copies only the code points it keeps, so a reader can hold a bounded result without ever holding the whole value. The text it returns is what Shorten would have returned, and Render leaves it alone when it renders with the same max.

type Table

type Table struct {
	Columns []string
	Rows    [][]Cell // at most Limit rows
	More    bool     // the caller fetched Limit+1 rows and got the extra one
	Limit   int
	// Shortened marks, per row and column, the cells the caller already cut with Cut; the footer counts them
	// like the ones Render cuts itself. A missing row or column means not cut.
	Shortened [][]bool
	Prefix    string // lines written before the table, counted in the byte budget
	// Caps holds, per column, a cell limit that replaces the maxCell given to Render; 0 or a missing entry keeps maxCell.
	Caps []int
	// CellLimit is the highest max_cell a caller may ask for; when above the maxCell given to Render, a shortened
	// cell adds a footer line naming the columns and saying how to read more. 0 for tools without max_cell.
	CellLimit int
}

Table is a result set ready to render.

Jump to

Keyboard shortcuts

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