Skip to content

Commit 40e56cf

Browse files
authored
Merge pull request #139 from cpanato/improve-tablewriter
add more wrapper func for tablewriter
2 parents 7ff2414 + dde8abd commit 40e56cf

13 files changed

+117
-37
lines changed

util/common.go renamed to helpers/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package helpers
1818

1919
import (
2020
"bufio"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package helpers
1818

1919
import (
2020
"errors"

util/doc.go renamed to helpers/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util //nolint: revive
17+
package helpers

helpers/tablewriter.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package helpers
18+
19+
import (
20+
"io"
21+
22+
"github.com/olekukonko/tablewriter"
23+
"github.com/olekukonko/tablewriter/renderer"
24+
"github.com/olekukonko/tablewriter/tw"
25+
)
26+
27+
// NewTableWriter creates a new table writer with the given output and options.
28+
func NewTableWriter(output io.Writer, options ...tablewriter.Option) *tablewriter.Table {
29+
table := tablewriter.NewWriter(output)
30+
for _, opt := range options {
31+
table.Options(opt)
32+
}
33+
34+
return table
35+
}
36+
37+
// NewTableWriterWithDefaults creates a new table writer with default markdown configuration.
38+
// It includes left alignment, markdown renderer, and custom borders optimized for terminal output.
39+
func NewTableWriterWithDefaults(output io.Writer, options ...tablewriter.Option) *tablewriter.Table {
40+
defaultOptions := []tablewriter.Option{
41+
tablewriter.WithConfig(tablewriter.Config{
42+
Header: tw.CellConfig{
43+
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
44+
},
45+
}),
46+
tablewriter.WithRenderer(renderer.NewMarkdown()),
47+
tablewriter.WithRendition(tw.Rendition{
48+
Symbols: tw.NewSymbols(tw.StyleMarkdown),
49+
Borders: tw.Border{
50+
Left: tw.On,
51+
Top: tw.Off,
52+
Right: tw.On,
53+
Bottom: tw.Off,
54+
},
55+
Settings: tw.Settings{
56+
Separators: tw.Separators{
57+
BetweenRows: tw.On,
58+
},
59+
},
60+
}),
61+
tablewriter.WithRowAutoWrap(tw.WrapNone),
62+
}
63+
64+
defaultOptions = append(defaultOptions, options...)
65+
66+
return NewTableWriter(output, defaultOptions...)
67+
}
68+
69+
// NewTableWriterWithDefaultsAndHeader creates a new table writer with default configuration and header.
70+
func NewTableWriterWithDefaultsAndHeader(output io.Writer, header []string, options ...tablewriter.Option) *tablewriter.Table {
71+
headerOption := tablewriter.WithHeader(header)
72+
allOptions := append([]tablewriter.Option{headerOption}, options...)
73+
74+
return NewTableWriterWithDefaults(output, allOptions...)
75+
}
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package helpers
1818

1919
import (
2020
"bytes"
@@ -161,3 +161,41 @@ func TestNewTableWriter(t *testing.T) {
161161
compareGolden(t, output.String(), "multiple_rows.golden")
162162
})
163163
}
164+
165+
func TestNewTableWriterWithDefaults(t *testing.T) {
166+
t.Parallel()
167+
168+
t.Run("WithDefaults", func(t *testing.T) {
169+
t.Parallel()
170+
171+
var output bytes.Buffer
172+
173+
table := NewTableWriterWithDefaults(&output)
174+
175+
require.NotNil(t, table)
176+
require.IsType(t, &tablewriter.Table{}, table)
177+
})
178+
179+
t.Run("WithDefaultsAndHeader", func(t *testing.T) {
180+
t.Parallel()
181+
182+
var output bytes.Buffer
183+
184+
header := []string{"TESTGRID BOARD", "TITLE", "STATUS", "STATUS DETAILS"}
185+
table := NewTableWriterWithDefaultsAndHeader(&output, header)
186+
187+
require.NotNil(t, table)
188+
require.IsType(t, &tablewriter.Table{}, table)
189+
})
190+
191+
t.Run("WithDefaultsAndAdditionalOptions", func(t *testing.T) {
192+
t.Parallel()
193+
194+
var output bytes.Buffer
195+
196+
table := NewTableWriterWithDefaults(&output, tablewriter.WithMaxWidth(100))
197+
198+
require.NotNil(t, table)
199+
require.IsType(t, &tablewriter.Table{}, table)
200+
})
201+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)