render

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package render formats query results for terminal output. Phase 0 provides a minimal aligned text table; json/csv and pipe-aware output land in Phase 1 (see docs/TASKS.md C1/C2).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cell added in v0.12.0

func Cell(v any) string

Cell formats a single value for human-facing display: NULL for nil, and a hex literal (0x…) for binary values. Drivers normalize []byte to string, so binary columns (e.g. MySQL binary(16) UUIDs) arrive as invalid-UTF-8 strings that would otherwise print as mojibake. Machine formats (csv/tsv/json) do not use this — they keep the raw data.

func Table

func Table(w io.Writer, cols []string, rows [][]any) error

Table writes the result set as a simple left-aligned text table.

func Write

func Write(w io.Writer, f Format, cols []string, rows [][]any) error

Write renders an in-memory result set. It is a convenience wrapper over WriteSource for callers that already hold all rows (e.g. tests). Streaming callers should use WriteSource directly.

func WriteSource

func WriteSource(w io.Writer, opts Options, src Source) (int, error)

WriteSource renders src in the chosen format, returning the row count. csv/tsv/json stream row-by-row; table buffers (it needs column widths) and is the interactive format where result sets are small.

Types

type Format

type Format string

Format is an output format for a result set.

const (
	// FormatTable is a human-readable aligned text table (TTY default).
	FormatTable Format = "table"
	// FormatJSON is a JSON array of objects, one per row, preserving column order.
	FormatJSON Format = "json"
	// FormatCSV is comma-separated values with a header row.
	FormatCSV Format = "csv"
	// FormatTSV is tab-separated values with a header row (pipe default).
	FormatTSV Format = "tsv"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat validates and returns a Format.

type Options

type Options struct {
	Format Format
	// MaxCellWidth truncates table cells longer than this many runes (0 =
	// unlimited). Ignored by machine formats (csv/tsv/json).
	MaxCellWidth int
}

Options configures rendering.

type Source

type Source interface {
	Columns() []string
	// Next returns the next row; ok is false at end-of-rows (then err carries
	// any iteration error).
	Next() (row []any, ok bool, err error)
}

Source is a pull-based result set for streaming rendering, so large results (csv/tsv/json) are written row-by-row without buffering the whole set in memory. driver.Rows is adapted to this in the cmd layer.

Jump to

Keyboard shortcuts

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