jsonfmt

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 1 module, 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 jsonfmt renders output as an aligned table or JSON/YAML.

Index

Constants

View Source
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

func Clean(s string) string

Clean strips terminal control and invisible characters from an untrusted string, keeping CLI color.

func EscapeUnsafe added in v0.1.8

func EscapeUnsafe(jsonText string) string

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

func FormatMoney(f float64) string

FormatMoney formats an amount like FormatVND, keeping two decimals when the fraction is non-zero.

func FormatVND

func FormatVND(n int64) string

FormatVND formats n with "." thousands separators, e.g. 5000000 -> "5.000.000".

func OrDash

func OrDash(s string) string

OrDash returns s, or "-" when s is empty.

func RenderJSON

func RenderJSON(w io.Writer, v any) error

RenderJSON writes v as indented JSON.

func RenderYAML

func RenderYAML(w io.Writer, v any) error

RenderYAML writes v as YAML; structs carry yaml tags mirroring json.

func Truncate

func Truncate(s string, width int) string

Truncate shortens s to width visible characters with "…", resetting any cut color. width <= 0 means no limit.

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 NewTable

func NewTable(headers ...string) *Table

NewTable creates a table with the given column headers.

func (*Table) AddRow

func (t *Table) AddRow(cells ...string)

AddRow appends one row of cells.

func (*Table) Droppable added in v0.1.8

func (t *Table) Droppable(headers ...string) *Table

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

func (t *Table) KeepsTail(headers ...string) *Table

KeepsTail names the Truncatable columns whose cells may join a text and a tail with TailSep.

func (*Table) Render

func (t *Table) Render(w io.Writer) error

Render writes the table to w, aligned by visible width.

func (*Table) Truncatable

func (t *Table) Truncatable(headers ...string) *Table

Truncatable names the free-text columns that may be cut to fit; ids, dates and amounts never are.

Jump to

Keyboard shortcuts

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