diff options
author | umohnani8 <umohnani@redhat.com> | 2018-02-05 10:15:36 -0500 |
---|---|---|
committer | umohnani8 <umohnani@redhat.com> | 2018-02-07 10:43:16 -0500 |
commit | 363cfcb0ce1477249a40861ef936f377b95aaf09 (patch) | |
tree | fde73614f43b40592b46a65920ce02d22d5fb4ce /cmd/podman/formats/formats.go | |
parent | 3f0170227d40b154205eb9f787e6f0254d75749e (diff) | |
download | podman-363cfcb0ce1477249a40861ef936f377b95aaf09.tar.gz podman-363cfcb0ce1477249a40861ef936f377b95aaf09.tar.bz2 podman-363cfcb0ce1477249a40861ef936f377b95aaf09.zip |
Fix when the --format flag prints a new line at the end
If the output is to a terminal, return a new line at the end of the
output so that the output is visually appealing. If the output is being
piped, or saved to a file, basically not being outputted to a terminal, do
not print a new line at the end of the output. This ensures any further data
manipulation with the results happens smoothly without requiring to remember
the '/n' at the end of the output.
Signed-off-by: umohnani8 <umohnani@redhat.com>
Diffstat (limited to 'cmd/podman/formats/formats.go')
-rw-r--r-- | cmd/podman/formats/formats.go | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/cmd/podman/formats/formats.go b/cmd/podman/formats/formats.go index 4b6527b30..bfd773c45 100644 --- a/cmd/podman/formats/formats.go +++ b/cmd/podman/formats/formats.go @@ -11,6 +11,7 @@ import ( "github.com/ghodss/yaml" "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" ) const ( @@ -70,7 +71,8 @@ func (j JSONStructArray) Out() error { // If the we did get NULL back, we should spit out {} which is // at least valid JSON for the consumer. - fmt.Printf("%s\n", data) + fmt.Printf("%s", data) + humanNewLine() return nil } @@ -95,13 +97,20 @@ func (t StdoutTemplateArray) Out() error { if err != nil { return errors.Wrapf(err, "Template parsing error") } - for _, img := range t.Output { + for i, img := range t.Output { basicTmpl := tmpl.Funcs(basicFunctions) err = basicTmpl.Execute(w, img) if err != nil { return err } - fmt.Fprintln(w, "") + if i != len(t.Output)-1 { + fmt.Fprintln(w, "") + continue + } + // Only print new line at the end of the output if stdout is the terminal + if terminal.IsTerminal(int(os.Stdout.Fd())) { + fmt.Fprintln(w, "") + } } return w.Flush() } @@ -112,7 +121,8 @@ func (j JSONStruct) Out() error { if err != nil { return err } - fmt.Printf("%s\n", data) + fmt.Printf("%s", data) + humanNewLine() return nil } @@ -126,7 +136,7 @@ func (t StdoutTemplate) Out() error { if err != nil { return err } - fmt.Println() + humanNewLine() return nil } @@ -138,6 +148,14 @@ func (y YAMLStruct) Out() error { if err != nil { return err } - fmt.Println(string(buf)) + fmt.Printf("%s", string(buf)) + humanNewLine() return nil } + +// humanNewLine prints a new line at the end of the output only if stdout is the terminal +func humanNewLine() { + if terminal.IsTerminal(int(os.Stdout.Fd())) { + fmt.Println() + } +} |