Documentation
¶
Overview ¶
Package jsonfmt renders output as an aligned table or JSON/YAML.
Index ¶
- Constants
- func Clean(s string) string
- func EscapeUnsafe(jsonText string) string
- func FormatMoney(f float64) string
- func FormatVND(n int64) string
- func OrDash(s string) string
- func RenderJSON(w io.Writer, v any) error
- func RenderYAML(w io.Writer, v any) error
- func Truncate(s string, width int) string
- type Table
Constants ¶
const TailSep = "\x1f"
TailSep joins the text of a cell in a KeepsTail column to a tail kept whole when the cell is cut, such as an item's code after its name: Render cuts the text before the tail. Clean the parts first, since cleaning the joined cell would strip the separator. In any other column a TailSep is an ordinary control character and is stripped, so API data cannot forge a tail.
Variables ¶
This section is empty.
Functions ¶
func Clean ¶
Clean strips terminal control and invisible characters from an untrusted string, keeping CLI color.
func EscapeUnsafe ¶ added in v0.1.8
EscapeUnsafe rewrites each character Clean would strip from JSON text as a \u escape, so a terminal shows that it is there without acting on it. Outside a string JSON text is ASCII and its only control is the newline Indent writes, which stays.
func FormatMoney ¶
FormatMoney formats an amount like FormatVND, keeping two decimals when the fraction is non-zero.
func RenderYAML ¶
RenderYAML writes v as YAML; structs carry yaml tags mirroring json.
Types ¶
type Table ¶
type Table struct {
// HeaderColor styles header cells.
HeaderColor func(string) string
// MaxWidth is the width to fit; 0 means no limit.
MaxWidth int
// contains filtered or unexported fields
}
Table renders aligned header + rows.
func (*Table) Droppable ¶ added in v0.1.8
Droppable names the columns a table still wider than MaxWidth after cutting leaves out, in the order they go. Only a summary column belongs here: the full record stays in get and --json.
func (*Table) KeepsTail ¶ added in v0.1.8
KeepsTail names the Truncatable columns whose cells may join a text and a tail with TailSep.
func (*Table) Truncatable ¶
Truncatable names the free-text columns that may be cut to fit; ids, dates and amounts never are.