output

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: GPL-3.0, LGPL-3.0 Imports: 12 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 output renders command results in the format the caller asked for.

Every command produces a Result holding the same information three ways: a table for a terminal, an object for a machine, and, where it makes sense, a node set. The format flag then picks one. A command never formats its own output, so -o json means the same thing everywhere.

Index

Constants

View Source
const (
	FormatTable    = "table"
	FormatWide     = "wide"
	FormatJSON     = "json"
	FormatYAML     = "yaml"
	FormatNodeset  = "nodeset"
	FormatName     = "name"
	FormatJSONPath = "jsonpath"
	FormatJQ       = "jq"
)

The output formats -o accepts.

Variables

This section is empty.

Functions

func EscapeCell added in v0.3.0

func EscapeCell(s string) string

EscapeCell is EscapeText for a value that has to stay on one line, such as a table cell or a name in a header: newline and tab are escaped too, as \n and \t, so that a value cannot start a row that seems to belong to another node.

func EscapeText added in v0.3.0

func EscapeText(s string) string

EscapeText makes untrusted text safe to write to a terminal, keeping its lines.

Output from a node, a BMC, Slurm or an agent is not trusted: a carriage return or a cursor movement can overwrite the line printed for another node, and an escape sequence can retitle the terminal or write the clipboard. EscapeText replaces every C0 and C1 control character, DEL, the Unicode controls that reorder or break lines, and bytes that are not UTF-8 with a visible escape such as \x1b or \u009b. Newline and tab are kept, since they cannot move the cursor back over text already printed.

Text that holds none of these is returned unchanged.

func Formats

func Formats() []string

Formats lists the formats -o accepts, for the help text and the shell completion.

Types

type Column

type Column struct {
	// Name is the heading, upper case by convention.
	Name string
	// Wide keeps the column out of the default table and shows it only
	// under -o wide.
	Wide bool
	// Right aligns the values to the right, for counts.
	Right bool
}

Column is one column of a table.

func Cols

func Cols(names ...string) []Column

Cols builds plain columns from their names.

type Format

type Format struct {
	// Kind is one of the format constants.
	Kind string
	// Arg is the expression of a jsonpath or jq format.
	Arg string
}

Format is a parsed -o value.

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat reads a -o value. A jsonpath or jq expression is compiled here, so that a mistake in it is reported before a command has acted rather than when its result is printed.

func (Format) IsMachine

func (f Format) IsMachine() bool

IsMachine reports whether the format is meant to be parsed by a program, which is when progress and decoration are left out.

func (Format) String

func (f Format) String() string

String renders the format the way it was written.

func (Format) Write

func (f Format) Write(w io.Writer, r Result) error

Write renders a result.

func (Format) WriteContext added in v0.3.0

func (f Format) WriteContext(ctx context.Context, w io.Writer, r Result) error

WriteContext renders a result, giving up when ctx ends. Only a jq program can run for long enough to need it.

type Result

type Result struct {
	// Table is rendered by the table and wide formats.
	Table *Table
	// Object is rendered by the json, yaml, jsonpath and jq formats. When it
	// is nil the table is converted to a list of objects.
	Object any
	// Nodes is rendered by the nodeset and name formats. When it is nil
	// those formats fall back to the first column of the table.
	Nodes *nodeset.NodeSet
}

Result is what a command produces, in every shape the formats need.

type Table

type Table struct {
	Columns []Column
	Rows    [][]string
	// Caption is printed under the table, for a total or a note.
	Caption string
}

Table is a command result in rows and columns. Values are never truncated: a long reason or a wide node set is worth a wrapped line, and truncation has hidden the important half of a message often enough.

func NewTable

func NewTable(columns ...Column) *Table

NewTable starts a table with the given headings.

func (*Table) Add

func (t *Table) Add(values ...string)

Add appends a row. Missing cells are left empty and extra cells are kept, so that a column added later does not need every call site changed.

func (*Table) Len

func (t *Table) Len() int

Len reports the number of rows.

func (*Table) Objects

func (t *Table) Objects() []map[string]string

Objects renders the table as a list of objects, which is what the machine formats show when a command has nothing richer to offer.

Jump to

Keyboard shortcuts

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