output

package
v0.5.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: PostgreSQL Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ColorEnabled bool

Functions

func Bold

func Bold(s string) string

Bold wraps s in ANSI bold escape codes when color is enabled.

func BoolYesNo

func BoolYesNo(b bool) string

BoolYesNo renders a boolean as "yes" or "no" for table output.

func CheckHeaders

func CheckHeaders() []string

CheckHeaders returns the header row every doctor's check table prints. A function, not a package-level slice, so no caller can reorder the shared headers for everyone else by writing to it.

func ColorStatus

func ColorStatus(s string) string

ColorStatus wraps s in an ANSI color appropriate for its status value. Green for success states, red for failure states, yellow for transitional states. Returns s unchanged for unrecognised statuses or when color is off.

func DerefString

func DerefString(s *string) string

DerefString returns the pointed-to string, or "" when the pointer is nil. Optional API fields are rendered as pointers, and a blank cell is the right rendering for absent.

func FormatBytes

func FormatBytes(n *int) string

FormatBytes renders an optional byte count for table output: a blank cell when the pointer is nil, a plain byte count below 1 KiB, and a one-decimal binary unit above it. Sizes arrive from the API as optional ints, and a raw ten-digit byte count is unreadable in a column.

func FormatDate

func FormatDate(t time.Time) string

FormatDate renders a time.Time as its date part, for the generated fields whose spec declares format: date-time and so arrive parsed rather than as RFC3339 strings. A zero time renders blank, matching how an absent optional field renders.

func FormatTime

func FormatTime(t string) string

FormatTime renders an API timestamp as its date part. API times are RFC3339 strings; tables show the date only.

func IsText

func IsText(format string) bool

IsText reports whether format is the human-facing table view, under either of its names. Every verb that chooses between printing a response verbatim and rendering rows asks this one function, so a guard cannot drop the alias on its own. ValidFormat and Print's dispatch are the only other places both names are spelled out.

func OneLine

func OneLine(s string) string

OneLine collapses every run of whitespace in s to a single space and trims the ends, for values not written for a table cell. API error messages arrive wrapped, or with a stack trace or a Postgres DETAIL line folded in, and Print would show each newline in a cell as a literal \n. Nothing is dropped.

func Sanitize

func Sanitize(s string) string

Sanitize makes a server-controlled string safe to print to a terminal, by escaping every character that would otherwise forge structure or misrepresent what the server sent. Unescaped, a newline splits one record into several rows, breaking the contract that one table row is one record (measured on a managed metrics instance-name cell: two forged rows), and an ANSI escape repaints the screen. `-o json` never needed this, because encoding/json escapes control characters.

The rule is printability, not control-ness. unicode.IsControl misses U+202E RIGHT-TO-LEFT OVERRIDE, so a database named "invoice<U+202E>gnp.txt" displays as "invoicetxt.png" (Trojan Source); U+200B and U+FEFF, which let two different rows render identically and so forge an identity; and U+2028 and U+2029, which some consumers treat as line breaks. unicode.IsPrint is false for all of them and true for every legitimate value but the spaces below, and it is the rule strconv.Quote uses, which is why %q handles all this.

Every non-ASCII space is escaped on purpose. IsPrint admits only U+0020, so U+00A0, U+2007, U+202F and U+3000 are escaped even though a no-break space is legitimate in pasted prose: it imitates a space, the forgery U+200B commits, and U+3000 is double-width while tabwriter counts runes, so it misaligns the column. unicode.IsGraphic differs from IsPrint by exactly category Zs and would otherwise pass the whole suite, so TestSanitizeEscapesWhatForgesStructure pins the boundary.

The Unicode tables are the stdlib's, so a code point unassigned today, and so escaped, becomes printable when Go ships a newer Unicode version: a value's rendering can change on a Go upgrade with no diff here.

The backslash is escaped too, which makes the escaping injective. Otherwise a literal \n and a real newline render identically, and a consumer that un-escapes turns the literal text into a newline. It escapes rather than strips because a forged value is exactly when an operator needs to see what the server sent.

No ANSI sequence is preserved. ColorEnabled is never assigned outside tests, so Bold and ColorStatus return their input unchanged and this package emits no escape sequences in production. Preserving any would let a value open red with no reset and bleed into every later row and the shell prompt. If colour is wired up, this function has to learn about it, and so does the header path, which is deliberately not sanitized: a bold header would survive while a coloured status rendered as its literal escape text.

func ValidFormat

func ValidFormat(name string) bool

ValidFormat reports whether name is an output format the renderer understands. "table" is an internal alias for "text"; both are accepted even though only text/json/yaml are advertised on -o.

Types

type CheckRow

type CheckRow struct {
	Check   string
	Status  string
	Details string
}

CheckRow is one line of a doctor-style check table: a check's name, its status, and a human-readable detail. Its Columns runs Status through ColorStatus, so every doctor colours its verdicts the same way. It lives here rather than in `pgedge doctor`, `pgedge starfleet doctor` and `pgedge controlplane doctor` so the three cannot drift apart, and all three already import this package.

func (CheckRow) Columns

func (r CheckRow) Columns() []string

Columns satisfies Row.

type Renderer

type Renderer struct {
	Format string
	Color  bool
	Out    io.Writer
	Err    io.Writer
}

Renderer writes command results honoring the -o format flag. Structured data goes to Out; Err is for progress/debug lines.

func (*Renderer) Print

func (r *Renderer) Print(data any, headers []string) error

Print writes data in the renderer's format.

"text" (alias "table") — tab-aligned table; data must be []Row. "json" — compact JSON, newline-terminated (json.Encoder). "yaml" — YAML document.

func (*Renderer) Structured

func (r *Renderer) Structured() bool

Structured reports whether -o selected a machine-readable format (json or yaml) rather than the text view.

type Row

type Row interface {
	Columns() []string
}

Row is implemented by any type that can provide its column values as strings.

Jump to

Keyboard shortcuts

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