Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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.
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
type JQ ¶ added in v0.2.0
func NewJQWithArgs ¶ added in v0.4.0
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 (JSON) Write ¶
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
WithPretty enables pretty-printing 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 (Tab) Write ¶
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 (Text) Write ¶
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 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 NewTemplatePattern ¶ added in v0.2.0
func WrapIOWriter ¶ added in v0.2.0
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 (YAML) Write ¶
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