formatting

package
v0.0.0-...-ddbfcda Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlockLine  = "line"
	BlockTable = "table"
)

Variables

View Source
var CSV = Format{
	FlagValue: "csv",
	WriteCollection: func(w io.Writer, r types.CollectionResponse) error {
		return writeCsv(w, r.DisplayHeaders(), r.Items())
	},
	WriteSingle: func(w io.Writer, r types.SingleResponse) error {
		return writeCsv(w, r.DisplayHeaders(), []map[string]interface{}{r.Fields()})
	},
}
View Source
var Formats = []Format{JSON, CSV, Table, Raw}
View Source
var JSON = Format{
	FlagValue: "json",
	WriteCollection: func(w io.Writer, r types.CollectionResponse) error {
		return writeJson(w, r.Items())
	},
	WriteSingle: func(w io.Writer, r types.SingleResponse) error {
		return writeJson(w, r.Fields())
	},
}
View Source
var Raw = Format{
	FlagValue: "raw",
	WriteCollection: func(w io.Writer, r types.CollectionResponse) error {
		return writeJson(w, r)
	},
	WriteSingle: func(w io.Writer, r types.SingleResponse) error {
		return writeJson(w, r)
	},
}
View Source
var Table = Format{
	FlagValue: "table",
	WriteCollection: func(w io.Writer, cr types.CollectionResponse) error {
		items := cr.Items()
		headers := cr.DisplayHeaders()
		if len(items) == 0 {
			return nil
		}
		table := tablewriter.NewWriter(w)
		ConfigureTableWriter(table)
		table.SetHeader(headers.Names())
		row := make([]string, len(headers))
		for _, item := range items {
			FillRowFromHeaders(headers, item, row)
			table.Append(row)
		}
		table.Render()
		return nil
	},
	WriteSingle: func(w io.Writer, r types.SingleResponse) error {
		fields := r.Fields()
		foundFields := 0
		table := tablewriter.NewWriter(w)
		ConfigureTableWriter(table)
		for _, h := range r.DisplayHeaders() {
			val, ok := fields[h.Key]
			if ok {
				table.Append([]string{h.Name, ToString(val)})
				foundFields++
			}
		}
		if foundFields > 0 {
			table.Render()
		}
		return nil
	},
}

Functions

func ConfigureTableWriter

func ConfigureTableWriter(table *tablewriter.Table)

func FillRowFromHeaders

func FillRowFromHeaders(headers types.DisplayHeaders, item map[string]interface{}, row []string)

func FormatFlagValues

func FormatFlagValues() []string

func ToString

func ToString(i interface{}) string

Types

type Block

type Block struct {
	Type     string          `json:"type"`
	RawValue json.RawMessage `json:"value"`
}

func (Block) LineValue

func (b Block) LineValue() (s string)

func (Block) TableValue

func (b Block) TableValue() (t TabularData)

type Blocks

type Blocks []Block

func (Blocks) WriteTo

func (bs Blocks) WriteTo(w io.Writer) (int64, error)

type Format

type Format struct {
	// The string value of this format to use in a CLI flag, like 'csv' or 'json'.
	FlagValue string
	// Write the API collection response with "display_headers" and "items" to w.
	WriteCollection func(io.Writer, types.CollectionResponse) error
	// Write the API response with "display_headers" to w.
	// Should not have 'items', instead uses display_headers to pluck
	// fields from the response.
	WriteSingle func(io.Writer, types.SingleResponse) error
}

func LookupByFlag

func LookupByFlag(v string) (Format, bool)

type TabularData

type TabularData struct {
	Headers []string   `json:"headers"`
	Rows    [][]string `json:"rows"`
}

func (TabularData) Write

func (t TabularData) Write(w io.Writer) error

Jump to

Keyboard shortcuts

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