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 ¶
- func Approx(n int64) string
- func CSVLine(fields ...string) string
- func Duration(d time.Duration) string
- func Error(err error) string
- func Percent(frac float64) string
- func PercentChange(frac float64) string
- func Render(t Table, maxBytes, maxCell int, d time.Duration) (string, error)
- func RenderTag(tag string, d time.Duration) string
- func Shorten(s string, max int) string
- func Size(bytes int64) string
- func SummarizeTSVector(s string) (summary string, ok bool)
- func SummarizeVector(s string) (summary string, ok bool)
- func WithSQL(err error, sql string) error
- type Cell
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CSVLine ¶
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 Error ¶
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 PercentChange ¶
PercentChange formats a change given as a fraction, always signed: -0.998 gives -99.8%, 0.065 gives +6.5%.
func Render ¶
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 ¶
RenderTag renders the footer of a statement that returns no rows: "-- UPDATE 3, 3 ms".
func Shorten ¶
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 ¶
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 ¶
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
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.
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
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.