Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Info -> U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Info -> U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Info -> U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Info -> U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
12 changes: 7 additions & 5 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ func getPossibleChips(view treeView) (chips []imageChip) {
return possible
}

func printImageTree(dockerCLI command.Cli, view treeView) {
if streamRedirected(dockerCLI.Out()) {
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
func printImageTree(outs command.Streams, view treeView) {
if streamRedirected(outs.Out()) {
_, _ = fmt.Fprintln(outs.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
}

out := tui.NewOutput(dockerCLI.Out())
out := tui.NewOutput(outs.Out())
_, width := out.GetTtySize()
if width == 0 {
width = 80
Expand All @@ -241,6 +241,8 @@ func printImageTree(dockerCLI command.Cli, view treeView) {
topNameColor := out.Color(aec.NewBuilder(aec.BlueF, aec.Bold).ANSI)
normalColor := out.Color(tui.ColorSecondary)
untaggedColor := out.Color(tui.ColorTertiary)
titleColor := out.Color(tui.ColorTitle)

isTerm := out.IsTerminal()

out.Println(generateLegend(out, width))
Expand Down Expand Up @@ -316,7 +318,7 @@ func printImageTree(dockerCLI command.Cli, view treeView) {
_, _ = fmt.Fprint(out, strings.Repeat(" ", columnSpacing))
}

_, _ = fmt.Fprint(out, h.Print(tui.ColorTitle, strings.ToUpper(h.Title)))
_, _ = fmt.Fprint(out, h.Print(titleColor, strings.ToUpper(h.Title)))
}
_, _ = fmt.Fprintln(out)

Expand Down
132 changes: 132 additions & 0 deletions cli/command/image/tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package image

import (
"strings"
"testing"

"github.com/docker/cli/internal/test"
"gotest.tools/v3/assert"
)

func TestPrintImageTreeAnsiTty(t *testing.T) {
testCases := []struct {
name string
stdinTty bool
stdoutTty bool
stderrTty bool
expectedAnsi bool
}{
{
name: "non-terminal",
stdinTty: false,
stdoutTty: false,
stderrTty: false,

expectedAnsi: false,
},
{
name: "terminal",
stdinTty: true,
stdoutTty: true,
stderrTty: true,

expectedAnsi: true,
},
{
name: "stdout-tty-only",
stdinTty: false,
stdoutTty: true,
stderrTty: false,

expectedAnsi: true,
},
{
name: "stdin-stderr-tty-only",
stdinTty: true,
stdoutTty: false,
stderrTty: true,

expectedAnsi: false,
},
{
name: "stdout-stdin-tty",
stdinTty: true,
stdoutTty: true,
stderrTty: false,

expectedAnsi: true,
},
{
name: "stdout-stderr-tty",
stdinTty: false,
stdoutTty: true,
stderrTty: true,

expectedAnsi: true,
},
{
name: "stdin-tty-only",
stdinTty: true,
stdoutTty: false,
stderrTty: false,

expectedAnsi: false,
},
{
name: "stderr-tty-only",
stdinTty: false,
stdoutTty: false,
stderrTty: true,

expectedAnsi: false,
},
}

mockView := treeView{
images: []topImage{
{
Names: []string{"test-image:latest"},
Details: imageDetails{
ID: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
DiskUsage: "10.5 MB",
InUse: true,
ContentSize: "5.2 MB",
},
Children: []subImage{
{
Platform: "linux/amd64",
Available: true,
Details: imageDetails{
ID: "sha256:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
DiskUsage: "5.1 MB",
InUse: false,
ContentSize: "2.5 MB",
},
},
},
},
},
imageSpacing: false,
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cli := test.NewFakeCli(nil)
cli.In().SetIsTerminal(tc.stdinTty)
cli.Out().SetIsTerminal(tc.stdoutTty)
cli.Err().SetIsTerminal(tc.stderrTty)

printImageTree(cli, mockView)

out := cli.OutBuffer().String()
assert.Check(t, len(out) > 0, "Output should not be empty")

hasAnsi := strings.Contains(out, "\x1b[")
if tc.expectedAnsi {
assert.Check(t, hasAnsi, "Output should contain ANSI escape codes")
} else {
assert.Check(t, !hasAnsi, "Output should not contain ANSI escape codes")
}
})
}
}
Loading