Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSON ¶
type JSON struct { Formatter *formatter.CompFormatter Indent string Style string // contains filtered or unexported fields }
JSON is a generic Writer that formats arbitrary values as JSON.
func NewAutoJSON ¶
NewAutoJSON creates and initializes new JSON Writer with or without formatting. The JSON encoder uses indentation and ANSII escape sequences are used for coloring, if the underlying Writer is stdout on an interactive terminal.
func NewPrettyJSON ¶
NewPrettyJSON creates a new JSON Writer with indentation and coloring.
func (JSON) Write ¶
Write writes the JSON representation of the given value to the underlying Writer.
Example (Struct) ¶
w := NewJSON(os.Stdout) w.Formatter.SetFormatterFunc(reflect.TypeOf(t).Name(), func(i interface{}) (string, error) { tm := i.(Team) return `{"team":"` + tm.Name() + `","members":` + strconv.Itoa(len(tm.Members())) + `}`, nil }) _, _ = w.Write(u) _, _ = w.Write("\n\n") _, _ = w.Write(t)
Output: {"username":"John Doe","email":"john.doe@local"} {"team":"SUPPORT","members":2}
Example (StructSlice) ¶
w := NewJSON(os.Stdout) _, _ = w.Write([]User{u, u})
Output: [{"username":"John Doe","email":"john.doe@local"},{"username":"John Doe","email":"john.doe@local"}]
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 := NewTab(b) f := formatter.AsTab(formatter.Func(func(i interface{}) (string, error) { return fmt.Sprintf("name\t%s\t", i.(Team).Name()), nil })) w.Formatter.SetFormatter(reflect.TypeOf(t).Name(), f) _, _ = w.Write(u) _, _ = w.Write("\n") _, _ = w.Write(t) // 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 := NewTab(b) typ := reflect.TypeOf([]Team{}).String() w.Formatter.SetFormatter(typ, formatter.AsTab(formatter.Func(func(i interface{}) (string, error) { b := strings.Builder{} b.WriteString("name\tmembers\t\n") ts := i.([]Team) for _, t := range ts { b.WriteString(fmt.Sprintf("%s\t%d\t\n", t.Name(), len(t.Members()))) } return b.String(), nil }))) _, _ = w.Write([]User{u, *NewUser("Rudolf", "Lingens")}) _, _ = w.Write("\n") _, _ = w.Write([]Team{t, t}) // 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 := NewText(os.Stdout) w.Formatter.SetFormatterFunc(reflect.TypeOf(t).Name(), func(i interface{}) (string, error) { return i.(Team).Name(), nil }) _, _ = w.Write(t) _, _ = w.Write("\n\n") _, _ = w.Write(u)
Output: SUPPORT John Doe <john.doe@local>
Example (StructSlice) ¶
w := NewText(os.Stdout) w.Formatter.SetFormatterFunc(reflect.TypeOf([]Team{}).String(), func(i interface{}) (string, error) { b := strings.Builder{} for _, t := range i.([]Team) { b.WriteString(t.Name() + "\n") } return b.String(), nil }) _, _ = w.Write([]User{u, u}) _, _ = w.Write("\n\n") _, _ = w.Write([]Team{t, t})
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 interface{}) (int, error) }
Writer is the interface that wraps the generic Write method.
type YAML ¶
type YAML struct { Formatter *formatter.CompFormatter Indent int Style string // contains filtered or unexported fields }
YAML is a generic Writer that formats arbitrary values as YSON.
func NewPrettyYAML ¶
NewPrettyYAML creates a new YAML Writer with indentation and coloring.
func (YAML) Write ¶
Write writes the YAML representation of the given value to the underlying Writer.
Example (Struct) ¶
w := NewYAML(os.Stdout) w.Formatter.SetFormatterFunc(reflect.TypeOf(t).Name(), func(i interface{}) (string, error) { return i.(Team).Name(), nil }) _, _ = w.Write(t) _, _ = w.Write("\n\n") _, _ = w.Write(u)
Output: SUPPORT Username: John Doe E-Mail: john.doe@local
Example (StructSlice) ¶
w := NewYAML(os.Stdout) w.Formatter.SetFormatterFunc(reflect.TypeOf(t).Name(), func(i interface{}) (string, error) { return i.(Team).Name(), nil }) _, _ = w.Write([]User{u, u})
Output: - Username: John Doe E-Mail: john.doe@local - Username: John Doe E-Mail: john.doe@local