The tabwriter was configured to have a min-width for columns of 20 positions.
This seemed quite wide, and caused smaller columns to be printed with a large
gap between.
Before:
docker container stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
29184b3ae391 amazing_shirley 0.00% 800KiB / 1.944GiB 0.04% 1.44kB / 0B 0B / 0B 1
403c101bad56 agitated_swartz 0.15% 34.31MiB / 1.944GiB 1.72% 10.2MB / 206kB 0B / 0B 51
0dc4b7f6c6be container2 0.00% 1.012MiB / 1.944GiB 0.05% 12.9kB / 0B 0B / 0B 5
2d99abcc6f62 container99 0.00% 972KiB / 1.944GiB 0.05% 13kB / 0B 0B / 0B 5
9f9aa90173ac foo 0.00% 820KiB / 1.944GiB 0.04% 13kB / 0B 0B / 0B 5
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29184b3ae391 docker-cli-dev "ash" 4 hours ago Up 4 hours amazing_shirley
403c101bad56 docker-dev:master "hack/dind bash" 3 days ago Up 3 days agitated_swartz
0dc4b7f6c6be nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp container2
2d99abcc6f62 nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp container99
9f9aa90173ac nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp foo
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-cli-dev latest 5f603caa04aa 4 hours ago 610MB
docker-cli-native latest 9dd29f8d387b 4 hours ago 519MB
docker-dev master 8132bf7a199e 3 days ago 2.02GB
docker-dev improve-build-errors 69e208994b3f 11 days ago 2.01GB
docker-dev refactor-idtools 69e208994b3f 11 days ago 2.01GB
After:
docker container stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
29184b3ae391 amazing_shirley 0.14% 5.703MiB / 1.944GiB 0.29% 1.44kB / 0B 0B / 0B 10
403c101bad56 agitated_swartz 0.15% 56.97MiB / 1.944GiB 2.86% 10.2MB / 206kB 0B / 0B 51
0dc4b7f6c6be container2 0.00% 1016KiB / 1.944GiB 0.05% 12.9kB / 0B 0B / 0B 5
2d99abcc6f62 container99 0.00% 956KiB / 1.944GiB 0.05% 13kB / 0B 0B / 0B 5
9f9aa90173ac foo 0.00% 980KiB / 1.944GiB 0.05% 13kB / 0B 0B / 0B 5
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29184b3ae391 docker-cli-dev "ash" 12 minutes ago Up 12 minutes amazing_shirley
403c101bad56 docker-dev:master "hack/dind bash" 3 days ago Up 3 days agitated_swartz
0dc4b7f6c6be nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp container2
2d99abcc6f62 nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp container99
9f9aa90173ac nginx:alpine "/docker-entrypoint.…" 4 days ago Up 4 days 80/tcp foo
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-cli-dev latest 5f603caa04aa 4 hours ago 610MB
docker-cli-native latest 9dd29f8d387b 4 hours ago 519MB
docker-dev master 8132bf7a199e 3 days ago 2.02GB
docker-dev improve-build-errors 69e208994b3f 11 days ago 2.01GB
docker-dev refactor-idtools 69e208994b3f 11 days ago 2.01GB
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package formatter
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
"text/tabwriter"
|
|
"text/template"
|
|
|
|
"github.com/docker/cli/templates"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// Format keys used to specify certain kinds of output formats
|
|
const (
|
|
TableFormatKey = "table"
|
|
RawFormatKey = "raw"
|
|
PrettyFormatKey = "pretty"
|
|
|
|
DefaultQuietFormat = "{{.ID}}"
|
|
)
|
|
|
|
// Format is the format string rendered using the Context
|
|
type Format string
|
|
|
|
// IsTable returns true if the format is a table-type format
|
|
func (f Format) IsTable() bool {
|
|
return strings.HasPrefix(string(f), TableFormatKey)
|
|
}
|
|
|
|
// Contains returns true if the format contains the substring
|
|
func (f Format) Contains(sub string) bool {
|
|
return strings.Contains(string(f), sub)
|
|
}
|
|
|
|
// Context contains information required by the formatter to print the output as desired.
|
|
type Context struct {
|
|
// Output is the output stream to which the formatted string is written.
|
|
Output io.Writer
|
|
// Format is used to choose raw, table or custom format for the output.
|
|
Format Format
|
|
// Trunc when set to true will truncate the output of certain fields such as Container ID.
|
|
Trunc bool
|
|
|
|
// internal element
|
|
finalFormat string
|
|
header interface{}
|
|
buffer *bytes.Buffer
|
|
}
|
|
|
|
func (c *Context) preFormat() {
|
|
c.finalFormat = string(c.Format)
|
|
|
|
// TODO: handle this in the Format type
|
|
if c.Format.IsTable() {
|
|
c.finalFormat = c.finalFormat[len(TableFormatKey):]
|
|
}
|
|
|
|
c.finalFormat = strings.Trim(c.finalFormat, " ")
|
|
r := strings.NewReplacer(`\t`, "\t", `\n`, "\n")
|
|
c.finalFormat = r.Replace(c.finalFormat)
|
|
}
|
|
|
|
func (c *Context) parseFormat() (*template.Template, error) {
|
|
tmpl, err := templates.Parse(c.finalFormat)
|
|
if err != nil {
|
|
return tmpl, errors.Errorf("Template parsing error: %v\n", err)
|
|
}
|
|
return tmpl, err
|
|
}
|
|
|
|
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {
|
|
if c.Format.IsTable() {
|
|
t := tabwriter.NewWriter(c.Output, 10, 1, 3, ' ', 0)
|
|
buffer := bytes.NewBufferString("")
|
|
tmpl.Funcs(templates.HeaderFunctions).Execute(buffer, subContext.FullHeader())
|
|
buffer.WriteTo(t)
|
|
t.Write([]byte("\n"))
|
|
c.buffer.WriteTo(t)
|
|
t.Flush()
|
|
} else {
|
|
c.buffer.WriteTo(c.Output)
|
|
}
|
|
}
|
|
|
|
func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error {
|
|
if err := tmpl.Execute(c.buffer, subContext); err != nil {
|
|
return errors.Errorf("Template parsing error: %v\n", err)
|
|
}
|
|
if c.Format.IsTable() && c.header != nil {
|
|
c.header = subContext.FullHeader()
|
|
}
|
|
c.buffer.WriteString("\n")
|
|
return nil
|
|
}
|
|
|
|
// SubFormat is a function type accepted by Write()
|
|
type SubFormat func(func(SubContext) error) error
|
|
|
|
// Write the template to the buffer using this Context
|
|
func (c *Context) Write(sub SubContext, f SubFormat) error {
|
|
c.buffer = bytes.NewBufferString("")
|
|
c.preFormat()
|
|
|
|
tmpl, err := c.parseFormat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
subFormat := func(subContext SubContext) error {
|
|
return c.contextFormat(tmpl, subContext)
|
|
}
|
|
if err := f(subFormat); err != nil {
|
|
return err
|
|
}
|
|
|
|
c.postFormat(tmpl, sub)
|
|
return nil
|
|
}
|