formatter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2023 License: MIT Imports: 11 Imported by: 0

README

Formatter

Build Status GoDoc Go Report Card Software License

This stable version.

Description

Tabular data formatting package.

Supports formats
  • table text/template
  • text/template
  • raw
  • pretty
  • json
  • yaml

Installation

$ go get github.com/mantyr/formatter

Format examples

  • table {{.ID}}\t{{.Name}} - Aligns columns and adds a header
  • {{.ID}}\t{{.Name}} - Displays to a string according to the format
  • raw - Displays the object line by line (field: value)
  • json - Displays in json format
  • yaml - Displays in yaml format

Example

package main

import (
	"github.com/mantyr/formatter"
)

func main() {
	err = Print(...)
	if err != nil {
		panic(err)
	}
}

func Print(items []interface{}) error {
	format := formatter.Format("table {{.ID}}\t{{.Name}}\t")
	header := formatter.Header(map[string]string{
		"ID":   "Identifier",
		"Name": "Name",
	})
	f, err := formatter.New(os.Stdout)
	if err != nil {
		return err
	}
	defer f.Flush()

	err = f.SetFormat(format)
	if err != nil {
		return err
	}

	err = f.SetHeader(header)
	if err != nil {
		return err
	}

	for _, item := range items {
		err = f.Write(item)
		if err != nil {
			return err
		}
	}
	return nil
}

Author

Oleg Shevelev

Documentation

Overview

nolint:errcheck

nolint:errcheck

Index

Constants

View Source
const (
	TableFormatKey  = "table"
	RawFormatKey    = "raw"
	PrettyFormatKey = "pretty"
	JSONFormatKey   = "json"
	YAMLFormatKey   = "yaml"

	DefaultQuietFormat = "{{.ID}}"
)

Format keys used to specify certain kinds of output formats

Variables

View Source
var BasicFunctions = template.FuncMap{
	"raw":      MarshalRAW,
	"json":     MarshalJSON,
	"yaml":     MarshalYAML,
	"split":    strings.Split,
	"join":     strings.Join,
	"title":    strings.Title,
	"lower":    strings.ToLower,
	"upper":    strings.ToUpper,
	"pad":      PadWithSpace,
	"truncate": TruncateWithLength,
}

BasicFunctions are the set of initial functions provided to every template.

View Source
var HeaderFunctions = template.FuncMap{
	"raw": func(v string) string {
		return v
	},
	"json": func(v string) string {
		return v
	},
	"yaml": func(v string) string {
		return v
	},
	"split": func(v string, _ string) string {

		return v
	},
	"join": func(v string, _ string) string {

		return v
	},
	"title": func(v string) string {
		return v
	},
	"lower": func(v string) string {
		return v
	},
	"upper": func(v string) string {
		return v
	},
	"truncate": func(v string, _ int) string {
		return v
	},
}

HeaderFunctions are used to created headers of a table. This is a replacement of basicFunctions for header generation because we want the header to remain intact. Some functions like `pad` are not overridden (to preserve alignment with the columns).

Functions

func MarshalJSON

func MarshalJSON(v interface{}) string

func MarshalRAW

func MarshalRAW(v interface{}) string

func MarshalYAML

func MarshalYAML(v interface{}) string

func PadWithSpace

func PadWithSpace(source string, prefix, suffix int) string

PadWithSpace adds whitespace to the input if the input is non-empty

func TruncateWithLength

func TruncateWithLength(source string, length int) string

TruncateWithLength truncates the source string up to the length provided by the input

Types

type Format

type Format string

Format is the format string rendered using the Context

func (Format) Contains

func (f Format) Contains(sub string) bool

Contains returns true if the format contains the substring

func (Format) Header

func (f Format) Header() (*template.Template, error)

func (Format) IsBuffered

func (f Format) IsBuffered() bool

func (Format) IsJSON

func (f Format) IsJSON() bool

IsJSON returns true if the format is the json format

func (Format) IsRaw

func (f Format) IsRaw() bool

func (Format) IsTable

func (f Format) IsTable() bool

IsTable returns true if the format is a table-type format

func (Format) IsYAML

func (f Format) IsYAML() bool

func (Format) String

func (f Format) String() string

func (Format) Template

func (f Format) Template() (*template.Template, error)

type Formatter

type Formatter interface {
	SetFormat(Format) error
	SetHeader(Header) error
	Write(interface{}) error
	Flush() error
}

func New

func New(writer io.Writer) (Formatter, error)
type Header map[string]string

Header is a map destined to formatter header (table format)

func (Header) Label

func (h Header) Label(name string) string

Label returns the header label for the specified string

Directories

Path Synopsis
encoding
raw

Jump to

Keyboard shortcuts

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