traefik/vendor/github.com/docker/libcompose/project/info.go

49 lines
955 B
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package project
import (
"bytes"
"io"
"text/tabwriter"
)
// InfoSet holds a list of Info.
type InfoSet []Info
// Info holds a list of InfoPart.
type Info map[string]string
2017-02-07 21:33:23 +00:00
func (infos InfoSet) String(columns []string, titleFlag bool) string {
2017-02-07 21:33:23 +00:00
//no error checking, none of this should fail
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
tabwriter := tabwriter.NewWriter(buffer, 4, 4, 2, ' ', 0)
first := true
for _, info := range infos {
if first && titleFlag {
writeLine(tabwriter, columns, true, info)
2017-02-07 21:33:23 +00:00
}
first = false
writeLine(tabwriter, columns, false, info)
2017-02-07 21:33:23 +00:00
}
tabwriter.Flush()
return buffer.String()
}
func writeLine(writer io.Writer, columns []string, key bool, info Info) {
2017-02-07 21:33:23 +00:00
first := true
for _, column := range columns {
2017-02-07 21:33:23 +00:00
if !first {
writer.Write([]byte{'\t'})
}
first = false
if key {
writer.Write([]byte(column))
2017-02-07 21:33:23 +00:00
} else {
writer.Write([]byte(info[column]))
2017-02-07 21:33:23 +00:00
}
}
writer.Write([]byte{'\n'})
}