cmd/list: Color the output only when displaying on a terminal
https://github.com/containers/toolbox/issues/596
This commit is contained in:
parent
e1635c06f3
commit
f49df914f4
2 changed files with 33 additions and 13 deletions
|
@ -25,6 +25,7 @@ import (
|
|||
|
||||
"github.com/containers/toolbox/pkg/podman"
|
||||
"github.com/containers/toolbox/pkg/utils"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/sirupsen/logrus"
|
||||
"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 resetColor = "\033[0m"
|
||||
|
||||
stdoutFd := os.Stdout.Fd()
|
||||
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
|
||||
if isatty.IsTerminal(stdoutFd) {
|
||||
fmt.Fprintf(writer, "%s", defaultColor)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer,
|
||||
"%s%s\t%s\t%s\t%s\t%s%s\n",
|
||||
defaultColor,
|
||||
"%s\t%s\t%s\t%s\t%s",
|
||||
"CONTAINER ID",
|
||||
"CONTAINER NAME",
|
||||
"CREATED",
|
||||
"STATUS",
|
||||
"IMAGE NAME",
|
||||
resetColor)
|
||||
"IMAGE NAME")
|
||||
|
||||
if isatty.IsTerminal(stdoutFd) {
|
||||
fmt.Fprintf(writer, "%s", resetColor)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "\n")
|
||||
|
||||
for _, container := range containers {
|
||||
isRunning := false
|
||||
|
@ -278,6 +289,7 @@ func listOutput(images []toolboxImage, containers []toolboxContainer) {
|
|||
isRunning = container.Status == "running"
|
||||
}
|
||||
|
||||
if isatty.IsTerminal(stdoutFd) {
|
||||
var color string
|
||||
if isRunning {
|
||||
color = boldGreenColor
|
||||
|
@ -285,14 +297,21 @@ func listOutput(images []toolboxImage, containers []toolboxContainer) {
|
|||
color = defaultColor
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "%s%s\t%s\t%s\t%s\t%s%s\n",
|
||||
color,
|
||||
fmt.Fprintf(writer, "%s", color)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%s",
|
||||
utils.ShortID(container.ID),
|
||||
container.Names[0],
|
||||
container.Created,
|
||||
container.Status,
|
||||
container.Image,
|
||||
resetColor)
|
||||
container.Image)
|
||||
|
||||
if isatty.IsTerminal(stdoutFd) {
|
||||
fmt.Fprintf(writer, "%s", resetColor)
|
||||
}
|
||||
|
||||
fmt.Fprintf(writer, "\n")
|
||||
}
|
||||
|
||||
writer.Flush()
|
||||
|
|
|
@ -9,6 +9,7 @@ require (
|
|||
github.com/docker/go-units v0.4.0
|
||||
github.com/fsnotify/fsnotify v1.4.7
|
||||
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/spf13/cobra v0.0.5
|
||||
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9
|
||||
|
|
Loading…
Reference in a new issue