Skip to content

Commit

Permalink
list/tree: Align number right, text left
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Aug 16, 2024
1 parent 4d4ec16 commit 9e14c08
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
},
}

if sub.Details.Used {
// Mark top-level parent image as used if any of its subimages are used.
details.Used = true
}

totalContent += im.Size.Content
children = append(children, sub)
}
Expand Down Expand Up @@ -117,30 +122,38 @@ func printImageTree(dockerCLI command.Cli, images []topImage) error {
}

columns := []imgColumn{
{Title: "Image", Width: 0, Left: true},
{
Title: "Image",
Align: alignLeft,
Width: 0,
},
{
Title: "ID",
Align: alignLeft,
Width: 12,
DetailsValue: func(d *imageDetails) string {
return stringid.TruncateID(d.ID)
},
},
{
Title: "Disk usage",
Align: alignRight,
Width: 10,
DetailsValue: func(d *imageDetails) string {
return d.DiskUsage
},
},
{
Title: "Content size",
Align: alignRight,
Width: 12,
DetailsValue: func(d *imageDetails) string {
return d.ContentSize
},
},
{
Title: "Used",
Align: alignCenter,
Width: 4,
Color: &greenColor,
DetailsValue: func(d *imageDetails) string {
Expand Down Expand Up @@ -244,10 +257,18 @@ func printNames(out *streams.Out, headers []imgColumn, img topImage, color aec.A
}
}

type alignment int

const (
alignLeft alignment = iota
alignCenter
alignRight
)

type imgColumn struct {
Title string
Width int
Left bool
Align alignment

DetailsValue func(*imageDetails) string
Color *aec.ANSI
Expand All @@ -261,14 +282,18 @@ func truncateRunes(s string, length int) string {
return s
}

func (h imgColumn) Print(clr aec.ANSI, s string) (out string) {
if h.Left {
return h.PrintL(clr, s)
func (h imgColumn) Print(clr aec.ANSI, s string) string {
switch h.Align {
case alignCenter:
return h.PrintC(clr, s)
case alignRight:
return h.PrintR(clr, s)
case alignLeft:
}
return h.PrintC(clr, s)
return h.PrintL(clr, s)
}

func (h imgColumn) PrintC(clr aec.ANSI, s string) (out string) {
func (h imgColumn) PrintC(clr aec.ANSI, s string) string {
ln := utf8.RuneCountInString(s)

if ln > h.Width {
Expand All @@ -292,6 +317,15 @@ func (h imgColumn) PrintL(clr aec.ANSI, s string) string {
return clr.Apply(s) + strings.Repeat(" ", h.Width-ln)
}

func (h imgColumn) PrintR(clr aec.ANSI, s string) string {
ln := utf8.RuneCountInString(s)
if ln > h.Width {
return clr.Apply(truncateRunes(s, h.Width))
}

return strings.Repeat(" ", h.Width-ln) + clr.Apply(s)
}

type noColor struct{}

func (a noColor) With(ansi ...aec.ANSI) aec.ANSI {
Expand Down

0 comments on commit 9e14c08

Please sign in to comment.