From aef6b04a7ce7dd56f0fa4facafaca976aff46c16 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 24 Dec 2019 17:32:44 +0100 Subject: [PATCH] Fix docker ps table headers with custom format and "split" or "join" Update the list of overrides for table headers so that columns using split or join will produce the correct table header. Before this patch: docker ps --format='table {{split .Names "/"}}' [NAMES] [unruffled_mclean] [eloquent_meitner] [sleepy_grothendieck] With this patch applied: docker ps --format='table {{split .Names "/"}}' NAMES [unruffled_mclean] [eloquent_meitner] [sleepy_grothendieck] Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/container_test.go | 4 ++++ templates/templates.go | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index b0637da8ff..18740d5499 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -241,6 +241,10 @@ size: 0B Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)}, string(golden.Get(t, "container-context-write-special-headers.golden")), }, + { + Context{Format: NewContainerFormat(`table {{split .Image ":"}}`, false, false)}, + "IMAGE\n[ubuntu]\n[ubuntu]\n", + }, } for _, testcase := range cases { diff --git a/templates/templates.go b/templates/templates.go index 6cc2ec3608..0e8dd15368 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -30,11 +30,23 @@ var basicFunctions = template.FuncMap{ // HeaderFunctions are used to created headers of a table. // This is a replacement of basicFunctions for header generation // because we want the header to remain intact. -// Some functions like `split` are irrelevant so not added. +// Some functions like `pad` are not overridden (to preserve alignment +// with the columns). var HeaderFunctions = template.FuncMap{ "json": func(v string) string { return v }, + "split": func(v string, _ string) string { + // we want the table header to show the name of the column, and not + // split the table header itself. Using a different signature + // here, and return a string instead of []string + return v + }, + "join": func(v string, _ string) string { + // table headers are always a string, so use a different signature + // for the "join" function (string instead of []string) + return v + }, "title": func(v string) string { return v },