From 180d3a69b9a5c7a165f53dd4dd4af96881d4efa9 Mon Sep 17 00:00:00 2001 From: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:18:07 +0530 Subject: [PATCH] templates: don't fail table headers that use the slice function Table headers are rendered by executing the format template against the column names, using HeaderFunctions to stub out functions that should not modify the header. The builtin "slice" function was not stubbed, so a format such as "table {{slice .Names 0 15}}" failed for column names shorter than the given indexes, and the header was silently omitted. Add a "slice" stub to HeaderFunctions that returns the column name as-is. Signed-off-by: Nimesh Kulkarni <167794819+GitNimay@users.noreply.github.com> --- cli/command/formatter/container_test.go | 4 ++++ templates/templates.go | 6 ++++++ templates/templates_test.go | 8 ++++++++ 3 files changed, 18 insertions(+) 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 {