gfmt

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: Apache-2.0 Imports: 22 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg added in v0.3.0

type Arg struct {
	Key    string
	Val    string
	String bool
}

func NewArg added in v0.3.0

func NewArg(kv string, strLiteral bool) (*Arg, error)

type IOWriter added in v0.2.0

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

IOWriter wraps an io.Writer and implements the gfmt.Writer interface.

func (IOWriter) Write added in v0.2.0

func (w IOWriter) Write(i any) (int, error)

Write uses the default formats for its operand and writes to the internal io.Writer.

type JMESPath added in v0.2.0

type JMESPath struct {
	Expr   *jmespath.JMESPath
	Indent string
	Color  bool
	// contains filtered or unexported fields
}

func NewJMESPath added in v0.2.0

func NewJMESPath(w Writer, expr string) *JMESPath

func (JMESPath) Write added in v0.2.0

func (w JMESPath) Write(i any) (int, error)

type JQ added in v0.2.0

type JQ struct {
	Expr string
	Args []Arg
	Raw  bool
	// contains filtered or unexported fields
}

func NewJQ added in v0.2.0

func NewJQ(delegate Writer, expr string, opts ...Opt[JQ]) *JQ

func NewJQWithArgs added in v0.4.0

func NewJQWithArgs(delegate Writer, expr string, args []Arg, opts ...Opt[JQ]) *JQ

func (JQ) Write added in v0.2.0

func (w JQ) Write(i any) (int, error)

type JSON

type JSON struct {
	Formatter *formatter.CompFormatter
	Indent    string
	Style     *chroma.Style
	// contains filtered or unexported fields
}

JSON is a generic Writer that formats arbitrary values as JSON.

func NewJSON

func NewJSON(w io.Writer, opts ...Opt[JSON]) *JSON

NewJSON creates a new JSON Writer.

func (JSON) Write

func (w JSON) Write(i any) (int, error)

Write writes the JSON representation of the given value to the underlying Writer.

Example (Struct)
w := gfmt.NewJSON(os.Stdout)
w.Formatter.SetFormatterFunc(reflect.TypeOf(team).Name(), func(i any) (string, error) {
	tm := i.(Team)
	return `{"team":"` + tm.Name() + `","members":` + strconv.Itoa(len(tm.Members())) + `}`, nil
})

_, _ = w.Write(user)
_, _ = w.Write("\n\n")
_, _ = w.Write(team)
Output:

{"username":"John Doe","email":"john.doe@local"}

{"team":"SUPPORT","members":2}
Example (StructSlice)
w := gfmt.NewJSON(os.Stdout)

_, _ = w.Write([]User{user, user})
Output:

[{"username":"John Doe","email":"john.doe@local"},{"username":"John Doe","email":"john.doe@local"}]

type Opt added in v0.2.0

type Opt[W Writer] func(writer *W)

Opt allows to customize the behavior of a Writer.

func WithPretty added in v0.2.0

func WithPretty[W Writer]() Opt[W]

WithPretty enables pretty-printing for the given Writer.

func WithRaw added in v0.4.0

func WithRaw() Opt[JQ]

func WithStyle added in v0.2.0

func WithStyle[W Writer](s *chroma.Style) Opt[W]

WithStyle sets the syntax highlighting style for the given Writer.

type Tab

type Tab struct {
	Formatter *formatter.CompFormatter
	// contains filtered or unexported fields
}

Tab is a generic Writer that formats arbitrary values as ASCII table.

func NewTab

func NewTab(w io.Writer) *Tab

NewTab creates a new table Writer.

func (Tab) Write

func (w Tab) Write(i any) (int, error)

Write formats the given value as a table and writes it to the underlying Writer.

Example (Struct)
b := &strings.Builder{}
w := gfmt.NewTab(b)
f := formatter.AsTab(formatter.Func(func(i any) (string, error) {
	return fmt.Sprintf("name\t%s\t", i.(Team).Name()), nil
}))
w.Formatter.SetFormatter(reflect.TypeOf(team).Name(), f)

_, _ = w.Write(user)
_, _ = w.Write("\n")
_, _ = w.Write(team)

// Since the Output cannot contain trailing spaces, it gets stripped from the table in this Example.
s := regexp.MustCompile(`\s+\n`).ReplaceAllString(b.String(), "\n")
fmt.Println(s)
Output:

username John Doe
email    john.doe@local
name SUPPORT
Example (StructSlice)
b := &strings.Builder{}
w := gfmt.NewTab(b)
typ := reflect.TypeOf([]Team{}).String()
w.Formatter.SetFormatter(typ, formatter.AsTab(formatter.Func(func(i any) (string, error) {
	buf := strings.Builder{}
	buf.WriteString("name\tmembers\t\n")

	ts := i.([]Team)
	for _, t := range ts {
		buf.WriteString(fmt.Sprintf("%s\t%d\t\n", t.Name(), len(t.Members())))
	}
	return buf.String(), nil
})))

_, _ = w.Write([]User{user, *NewUser("Rudolf", "Lingens")})
_, _ = w.Write("\n")
_, _ = w.Write([]Team{team, team})

// Since the Example Output cannot contain trailing spaces, they get stripped.
s := regexp.MustCompile(`\s+\n`).ReplaceAllString(b.String(), "\n")
fmt.Println(s)
Output:

username       email
John Doe       john.doe@local
Rudolf Lingens rudolf.lingens@local
name    members
SUPPORT 2
SUPPORT 2

type Text

type Text struct {
	Formatter *formatter.CompFormatter
	Sep       string
	Delim     string
	// contains filtered or unexported fields
}

Text is a generic Writer that formats arbitrary values as plain text.

func NewText

func NewText(w io.Writer) *Text

NewText creates a new text Writer.

func (Text) Write

func (w Text) Write(i any) (int, error)

Write writes the text representation of the given value to the underlying Writer.

Example (Struct)
w := gfmt.NewText(os.Stdout)
w.Formatter.SetFormatterFunc(reflect.TypeOf(team).Name(), func(i any) (string, error) {
	return i.(Team).Name(), nil
})

_, _ = w.Write(team)
_, _ = w.Write("\n\n")
_, _ = w.Write(user)
Output:

SUPPORT

John Doe <john.doe@local>
Example (StructSlice)
w := gfmt.NewText(os.Stdout)
w.Formatter.SetFormatterFunc(reflect.TypeOf([]Team{}).String(), func(i any) (string, error) {
	b := strings.Builder{}
	for _, t := range i.([]Team) {
		b.WriteString(t.Name() + "\n")
	}
	return b.String(), nil
})

_, _ = w.Write([]User{user, user})
_, _ = w.Write("\n\n")
_, _ = w.Write([]Team{team, team})
Output:

username:email
John Doe:john.doe@local
John Doe:john.doe@local

SUPPORT
SUPPORT

type Tmpl added in v0.2.0

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

func (Tmpl) Write added in v0.2.0

func (w Tmpl) Write(a any) (int, error)

type Writer

type Writer interface {
	// Write writes i to the underlying output stream.
	//
	// It returns the number of bytes written and any error encountered that
	// caused the write to stop early.
	// Write must not modify the given parameter, even temporarily.
	Write(i any) (int, error)
}

Writer is the interface that wraps the generic Write method.

func NewTemplate added in v0.2.0

func NewTemplate(w io.Writer, tmpl *template.Template) Writer

func NewTemplatePattern added in v0.2.0

func NewTemplatePattern(output io.Writer, p string) Writer

func WrapIOWriter added in v0.2.0

func WrapIOWriter(wrapper io.Writer) Writer

WrapIOWriter wraps the given io.Writer as a gfmt.Writer.

type YAML

type YAML struct {
	Formatter *formatter.CompFormatter
	Indent    int
	Style     *chroma.Style
	// contains filtered or unexported fields
}

YAML is a generic Writer that formats arbitrary values as YAML.

func NewYAML

func NewYAML(w io.Writer, opts ...Opt[YAML]) *YAML

NewYAML creates a new YAML Writer.

func (YAML) Write

func (w YAML) Write(i any) (int, error)

Write writes the YAML representation of the given value to the underlying Writer.

Example (Struct)
w := gfmt.NewYAML(os.Stdout)
w.Formatter.SetFormatterFunc(reflect.TypeOf(team).Name(), func(i any) (string, error) {
	return i.(Team).Name(), nil
})

_, _ = w.Write(team)
_, _ = w.Write("\n\n")
_, _ = w.Write(user)
Output:

SUPPORT

Username: John Doe
E-Mail: john.doe@local
Example (StructSlice)
w := gfmt.NewYAML(os.Stdout)
w.Formatter.SetFormatterFunc(reflect.TypeOf(team).Name(), func(i any) (string, error) {
	return i.(Team).Name(), nil
})

_, _ = w.Write([]User{user, user})
Output:

- Username: John Doe
  E-Mail: john.doe@local
- Username: John Doe
  E-Mail: john.doe@local

Jump to

Keyboard shortcuts

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