printer

package
v0.0.0-...-949e47c Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package printer defines a set of CLI focused printers to show the results of an operation in different formats.

Index

Constants

View Source
const (
	// MinWidth is the minimal cell width including any padding.
	MinWidth = 8
	// TabWidth is the width of tab characters (equivalent number of spaces)
	TabWidth = 4
	// Padding added to a cell before computing its width
	Padding = 4
	// PaddingChar with the ASCII char used for padding
	PaddingChar = ' '
	// TabWriterFlags with the formatting options.
	TabWriterFlags = 0
)

Variables

This section is empty.

Functions

func PrintError

func PrintError(err error)

func PrintResultOrError

func PrintResultOrError(printer ResultPrinter, result interface{}, err error) error

PrintResultOrError prints the result using a given printer or the error.

Types

type JSONPrinter

type JSONPrinter struct {
}

JSONPrinter structure with the implementation required to print as JSON a given result.

func (*JSONPrinter) Print

func (jp *JSONPrinter) Print(result interface{}) error

Print the result.

func (*JSONPrinter) PrintResultOrError

func (jp *JSONPrinter) PrintResultOrError(result interface{}, err error) error

PrintResultOrError prints the result using a given printer or the error.

type ResultPrinter

type ResultPrinter interface {
	// Print the result.
	Print(result interface{}) error
	// PrintResultOrError prints the result using a given printer or the error.
	PrintResultOrError(result interface{}, err error) error
}

ResultPrinter defines the operations that a printer must define. Multiple printer are offered depending on the desired output format.

func GetPrinter

func GetPrinter(printerType string) (ResultPrinter, error)

GetPrinter creates a ResultPrinter attending to the user preferences.

func NewJSONPrinter

func NewJSONPrinter() (ResultPrinter, error)

NewJSONPrinter build a new ResultPrinter whose output is the JSON representation of the object.

func NewTablePrinter

func NewTablePrinter() (ResultPrinter, error)

NewTablePrinter builds a new ResultPrinter whose output is a human-readable table-like representation of the object.

type TablePrinter

type TablePrinter struct {
	// contains filtered or unexported fields
}

TablePrinter structure with the implementation required to print in a human-readable table format a given result.

func (*TablePrinter) AddTemplate

func (tp *TablePrinter) AddTemplate(templateType reflect.Type, templateContent string)

func (*TablePrinter) AddTemplateFunction

func (tp *TablePrinter) AddTemplateFunction(name string, f interface{})

AddTemplateFunction adds a new function so that it can be called in the golang template.

func (*TablePrinter) CapitalizeWord

func (tp *TablePrinter) CapitalizeWord(word string) string

CapitalizeWord sets as upper case the first letters of a word

func (*TablePrinter) GetTemplate

func (tp *TablePrinter) GetTemplate(result interface{}) (*string, error)

GetTemplate returns a template to print an arbitrary structure in table format.

func (*TablePrinter) Print

func (tp *TablePrinter) Print(result interface{}) error

Print the result.

func (*TablePrinter) PrintResultOrError

func (tp *TablePrinter) PrintResultOrError(result interface{}, err error) error

PrintResultOrError prints the result using a given printer or the error.

Jump to

Keyboard shortcuts

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