cmd/list: Color the output only when displaying on a terminal

https://github.com/containers/toolbox/issues/596
This commit is contained in:
Debarshi Ray 2020-10-30 21:03:49 +01:00
parent e1635c06f3
commit f49df914f4
2 changed files with 33 additions and 13 deletions

View file

@ -25,6 +25,7 @@ import (
"github.com/containers/toolbox/pkg/podman" "github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/utils" "github.com/containers/toolbox/pkg/utils"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -261,16 +262,26 @@ func listOutput(images []toolboxImage, containers []toolboxContainer) {
const defaultColor = "\033[0;00m" // identical to resetColor, but same length as boldGreenColor const defaultColor = "\033[0;00m" // identical to resetColor, but same length as boldGreenColor
const resetColor = "\033[0m" const resetColor = "\033[0m"
stdoutFd := os.Stdout.Fd()
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
if isatty.IsTerminal(stdoutFd) {
fmt.Fprintf(writer, "%s", defaultColor)
}
fmt.Fprintf(writer, fmt.Fprintf(writer,
"%s%s\t%s\t%s\t%s\t%s%s\n", "%s\t%s\t%s\t%s\t%s",
defaultColor,
"CONTAINER ID", "CONTAINER ID",
"CONTAINER NAME", "CONTAINER NAME",
"CREATED", "CREATED",
"STATUS", "STATUS",
"IMAGE NAME", "IMAGE NAME")
resetColor)
if isatty.IsTerminal(stdoutFd) {
fmt.Fprintf(writer, "%s", resetColor)
}
fmt.Fprintf(writer, "\n")
for _, container := range containers { for _, container := range containers {
isRunning := false isRunning := false
@ -278,21 +289,29 @@ func listOutput(images []toolboxImage, containers []toolboxContainer) {
isRunning = container.Status == "running" isRunning = container.Status == "running"
} }
var color string if isatty.IsTerminal(stdoutFd) {
if isRunning { var color string
color = boldGreenColor if isRunning {
} else { color = boldGreenColor
color = defaultColor } else {
color = defaultColor
}
fmt.Fprintf(writer, "%s", color)
} }
fmt.Fprintf(writer, "%s%s\t%s\t%s\t%s\t%s%s\n", fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s",
color,
utils.ShortID(container.ID), utils.ShortID(container.ID),
container.Names[0], container.Names[0],
container.Created, container.Created,
container.Status, container.Status,
container.Image, container.Image)
resetColor)
if isatty.IsTerminal(stdoutFd) {
fmt.Fprintf(writer, "%s", resetColor)
}
fmt.Fprintf(writer, "\n")
} }
writer.Flush() writer.Flush()

View file

@ -9,6 +9,7 @@ require (
github.com/docker/go-units v0.4.0 github.com/docker/go-units v0.4.0
github.com/fsnotify/fsnotify v1.4.7 github.com/fsnotify/fsnotify v1.4.7
github.com/godbus/dbus/v5 v5.0.3 github.com/godbus/dbus/v5 v5.0.3
github.com/mattn/go-isatty v0.0.8
github.com/sirupsen/logrus v1.4.2 github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9