formatdata

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: Apache-2.0 Imports: 18 Imported by: 2

README

github.com/shibukawa/formatdata-go

Go Reference

Simple pretty print library

Sample

type SampleStruct struct {
	A string `json:"a"`
	B int    `json:"b"`
}

// If the data can be represented as table, it shows in table format
tableData := []SampleStruct{
    {
        A: "a",
        B: 1,
    },
    {
        A: "b",
        B: 2,
    },
}
formatdata.FormatData(tableData)
// Output:
// ┌───┬───┐
// │ a │ b │
// ╞═══╪═══╡
// │ a │ 1 │
// ├───┼───┤
// │ b │ 2 │
// └───┴───┘

API

func FormatDataTo(data any, out io.Writer, o ...Opt) error

There are three variations. o is option (described below)

  • func FormatData(data any, o ...Opt) error

    This is an alias of FormatDataTo(data, os.Stdout, o...)

  • func FormatDataWithColor(data any, out io.Writer, o ...Opt) error

    This always uses escape sequence to dump colorized output.

  • func FormatDataWithoutColor(data any, out io.Writer, o ...Opt) error

    This always doesn't use escape sequence to dump colorized output.

Option
type Opt struct {
    // Terminal(default), Markdown, JSON, YAML
	OutputFormat             OutputFormat
    // Treat EastAsianAmbiguous characters as wide or not
	EastAsianAmbiguousAsWide bool
    // "terminal", "terminal8", "terminal16", "terminal256", "terminal16m". Default: "terminal"
	Formatter                string
    // https://github.com/alecthomas/chroma/tree/master/styles. Default: "monokai"
	Style                    string
    // Indent for JSON/YAML
	Indent                   int
}

If you want to specify YAML output, use this struct like this:

formatdata.FormatData(d, formatdata.Opt{
    OutputFormat: formatdata.YAML
    Indent:       4,
})

License

Apache 2

It uses code fragments of github.com/alecthomas/chroma. This is developed by Alec Thomas under MIT license.

Documentation

Overview

package formatdata provides pretty-print function FormatData and its variations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatData

func FormatData(data any, o ...Opt) error

FormatData is the simplest API.

data is any data to show. Nested array or array of struct will be formatted in table.

o is an optional. It controls format, style and so on.

func FormatDataTo

func FormatDataTo(data any, out io.Writer, o ...Opt) error

FormatDataTo is variation of FormatData. You can specify output destination.

If out is terminal, it uses escape sequence to dump colorized output.

func FormatDataWithColor

func FormatDataWithColor(data any, out io.Writer, o ...Opt) error

FormatDataWithColor is FormatDataTo's variation that always uses escape sequence to dump colorized output.

func FormatDataWithoutColor

func FormatDataWithoutColor(data any, out io.Writer, o ...Opt) error

FormatDataWithColor is FormatDataTo's variation that always doesn't use escape sequence.

Types

type Opt

type Opt struct {
	OutputFormat             OutputFormat
	EastAsianAmbiguousAsWide bool
	Formatter                string // "terminal", "terminal8", "terminal16", "terminal256", "terminal16m". Default: "terminal"
	Style                    string // https://github.com/alecthomas/chroma/tree/master/styles. Default: "monokai"
	Indent                   int    // Indent for JSON/YAML
}

type OutputFormat

type OutputFormat = int
const (
	Terminal OutputFormat = iota // Default. If data is not grid compatible, fallback to YAML.
	Markdown                     // Markdown table. If data is not grid compatible, fallback to YAML.
	JSON
	YAML
)

Jump to

Keyboard shortcuts

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