diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index 626c63142d91..a7792f2bc89f 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -2,6 +2,7 @@ package container import ( "strconv" + "strings" "sync" "github.com/docker/cli/cli/command/formatter" @@ -167,9 +168,9 @@ func (c *statsContext) Container() string { } func (c *statsContext) Name() string { - // TODO(thaJeztah): make this explicitly trim the "/" prefix, not just any char. - if len(c.s.Name) > 1 { - return c.s.Name[1:] + // Trim the "/" prefix (if present). + if name := strings.TrimPrefix(c.s.Name, "/"); name != "" { + return name } return noValue } diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index fafa6a56e0d9..be1be92b4e7f 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -155,11 +155,12 @@ func (c *ContainerContext) Names() string { return strings.Join(names, ",") } -// StripNamePrefix removes prefix from string, typically container names as returned by `ContainersList` API +// StripNamePrefix removes any "/" prefix from container names returned +// by the "ContainersList" API. func StripNamePrefix(ss []string) []string { sss := make([]string, len(ss)) for i, s := range ss { - sss[i] = s[1:] + sss[i] = strings.TrimPrefix(s, "/") } return sss }