result

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package result formats query values for display and export.

Formatting lives apart from the UI so the grid, CSV export and JSON export all agree on what a value looks like — and so the rules can be tested without a terminal.

Index

Constants

View Source
const CellLimit = 200

CellLimit caps how many runes a grid cell shows. Long values stay in the buffer intact; only the rendering is shortened.

View Source
const NullText = "NULL"

NullText is how a SQL NULL is shown. It has to be visually distinct from an empty string: treating the two alike misleads people into thinking a row failed an IS NULL test for no reason.

View Source
const TimeLayout = "2006-01-02 15:04:05"

TimeLayout is the timestamp rendering, chosen to match MySQL's own.

Variables

This section is empty.

Functions

func Compare added in v0.4.0

func Compare(a, b any, order Order) int

Compare orders two values of one column, returning the usual negative, zero or positive.

NULL sorts first, as it does in an ascending ORDER BY. Anything that cannot be read as the column's kind sorts after everything that can and then ties on its text, so a stray value in a numeric column lands somewhere it can be seen rather than somewhere arbitrary.

func EscapeTags

func EscapeTags(s string) string

EscapeTags neutralises tview's colour-tag syntax.

tview reads "[" as the start of a tag, so a cell containing "[red]" would recolour the grid instead of showing its own text. tview's documented escape is to write "[" as "[[".

func Format

func Format(v any) string

Format renders a scanned value as display text.

func Truncate

func Truncate(s string, limit int) string

Truncate shortens s to limit runes, marking the cut with an ellipsis. A limit of zero or less leaves s unchanged.

Types

type Buffer

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

Buffer holds the rows of one result set.

It is written by the streaming goroutine and read by the UI goroutine, so every accessor takes the lock. Reads outside the current bounds return zero values rather than panicking: tview asks for cells while rendering, and a result that was just cleared would otherwise crash the application.

func NewBuffer

func NewBuffer(max int) *Buffer

NewBuffer returns an empty buffer. A max of zero means unbounded.

func (*Buffer) Append

func (b *Buffer) Append(rows [][]any) int

Append adds rows up to the cap and returns how many were accepted.

func (*Buffer) AtCapacity

func (b *Buffer) AtCapacity() bool

AtCapacity reports whether the buffer has stopped accepting rows.

func (*Buffer) Cell

func (b *Buffer) Cell(row, col int) string

Cell returns display text for a cell, ready to hand to tview.

func (*Buffer) ColumnCount

func (b *Buffer) ColumnCount() int

ColumnCount is the number of columns in the result.

func (*Buffer) ColumnName

func (b *Buffer) ColumnName(col int) string

ColumnName returns the name of a column, or "" if it is out of range.

func (*Buffer) ColumnType added in v0.3.0

func (b *Buffer) ColumnType(col int) string

ColumnType names the column's database type — BIGINT, VARCHAR — or "" when the driver did not say.

A driver is allowed to report nothing, and several statements produce no type information at all, so the caller has to be able to show less rather than showing a blank where a type was promised.

func (*Buffer) Columns

func (b *Buffer) Columns() []string

Columns returns a copy of the column names.

func (*Buffer) Raw

func (b *Buffer) Raw(row, col int) any

Raw returns the unformatted value, which export needs and display does not.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset clears the header and the rows.

func (*Buffer) Row

func (b *Buffer) Row(row int) []any

Row returns a copy of one row's raw values, or nil if out of range.

func (*Buffer) RowCount

func (b *Buffer) RowCount() int

RowCount is the number of rows received so far. While a stream is running it grows, which is what lets the grid scroll through partial results.

func (*Buffer) SetColumns

func (b *Buffer) SetColumns(columns []string, types []*sql.ColumnType)

SetColumns replaces the result header and drops any existing rows.

type Order added in v0.4.0

type Order int

Order is how a column's values are put in sequence.

const (
	// OrderText compares the formatted values, which is what the server does
	// for a character column and the only thing that can be claimed about a
	// column whose type is unknown.
	OrderText Order = iota
	OrderNumber
	OrderTime
)

func OrderFor added in v0.4.0

func OrderFor(databaseType string) Order

OrderFor decides from the column's declared type.

It is the type that decides and never the values, because values reach here as bytes: everything a query returns over the text protocol is a []byte, so "9" and "10" look alike whether the column is a BIGINT or a VARCHAR. Guessing from the bytes would sort a VARCHAR numerically and disagree with the server about its own column.

An unknown type is text. A driver is allowed to report nothing, and an ordering a column cannot justify is worse than a coarse one.

Jump to

Keyboard shortcuts

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