diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 07df93d9aeed..97cf6c55e67a 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -387,6 +387,10 @@ size: 0B context: Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)}, expected: "IMAGE\n[ubuntu]\n[ubuntu]\n", }, + { + context: Context{Format: NewContainerFormat(`table {{slice .Names 0 6}}`, false, false)}, + expected: "NAMES\nfoobar\nfoobar\n", + }, } containers := []container.Summary{ diff --git a/templates/templates.go b/templates/templates.go index 5443d70d7c77..cc1b54c7ffbe 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -58,6 +58,12 @@ var HeaderFunctions = template.FuncMap{ "truncate": func(v string, _ int) string { return v }, + "slice": func(v string, _ ...int) string { + // slicing the column name would fail if the indexes are out of + // range for the header (e.g. "{{slice .ID 0 15}}"), so return + // the column name as-is. + return v + }, } // Parse creates a new anonymous template with the basic functions diff --git a/templates/templates_test.go b/templates/templates_test.go index ed1ee5b95d13..cd3da618e6b5 100644 --- a/templates/templates_test.go +++ b/templates/templates_test.go @@ -124,6 +124,14 @@ func TestHeaderFunctions(t *testing.T) { doc: "truncate", template: `{{ truncate . 2}}`, }, + { + doc: "slice", + template: `{{ slice . 2}}`, + }, + { + doc: "slice out of range", + template: `{{ slice . 0 15}}`, + }, } for _, tc := range tests {