Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading