You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
948 B

package output
import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
4 years ago
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity"
)
const newline = "\n"
func Stderr() Output {
return New(os.Stderr, FormatString)
}
func Stdout() Output {
return New(os.Stdout, FormatString)
}
func Buffer(buf *bytes.Buffer) Output {
return New(buf, FormatString)
}
4 years ago
func FormatString(_ verbosity.Verbosity, msg string, kv ...label.KeyValue) string {
if len(kv) > 0 {
nline := ""
if msg[len(msg)-1:] == newline {
nline = newline
}
4 years ago
return "msg=\"" + strings.TrimSpace(msg) + "\", " + label.KeyValues(kv).String() + nline
}
return msg
}
4 years ago
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...label.KeyValue) string) Output {
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
return fmt.Fprint(w, format(verb, msg, kv...))
}
}