❯ go-clikit
v0.10.0 pkg.go.dev ↗

packages › progress & output › cliout

cliout

import "github.com/GSI-HPC/go-clikit/cliout"

Write a result as a table aligned by display width, or as JSON, with every string made safe for a terminal.

since v0.6.0 imports: termtext decisions 30
The place of cliout in go-clikit: the packages it imports, and the packages that import it.termtextprogressdisplaycliprogressprogresstestmcpprogresseventlogfanoutfanouttestclioutdoctorfsutilcobratreecobratreetestlocalwebsessionsselocalwebtestlaunchassetspagehtmx

cliout writes the result of a command for a person and for a program. A cell often holds text from elsewhere, such as the reason a node gave. Table escapes each cell with termtext before it measures it, and WriteJSON escapes the runes that encoding/json leaves as they are.

NODE     STATE    REASON
exe0001  drained  fan failure\nexe0002 idle
exe0003  idle

The newline in the reason cannot start a row that seems to be another.

NameWhat it does
cliout.Table, cliout.Columncolumns aligned by display width; wide columns only for -o wide
cliout.WriteJSONJSON with every string safe for a terminal; a slice one element at a time
table := &cliout.Table{
	Columns: []cliout.Column{{Name: "NODE"}, {Name: "CPUS", Right: true}, {Name: "STATE"}, {Name: "REASON", Wide: true}},
	Caption: "3 nodes, 1 drained",
}
table.Add("exe0001", "128", "drained", "fan failure\nexe0002 idle")
table.Add("exe0002", "64", "idle")
table.Add("exe10", "8", "idle")

for _, wide := range []bool{false, true} {
	if err := table.Write(os.Stdout, wide); err != nil {
		fmt.Println(err)
	}
	fmt.Println("--")
}
NODE     CPUS  STATE
exe0001   128  drained
exe0002    64  idle
exe10       8  idle

3 nodes, 1 drained
--
NODE     CPUS  STATE    REASON
exe0001   128  drained  fan failure\nexe0002 idle
exe0002    64  idle
exe10       8  idle

3 nodes, 1 drained
--
ExampleTable in cliout/example_test.go. CI runs it and checks its output.

Fixed interfaces#

What cliout writes (decision 30). The package parses no flag: the words of -o are the program’s.