Skip to content

Commit 9eb8ae7

Browse files
committed
add more wrapper fiunc for tablewriter
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
1 parent 7ff2414 commit 9eb8ae7

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

util/tablewriter.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"io"
2121

2222
"github.com/olekukonko/tablewriter"
23+
"github.com/olekukonko/tablewriter/renderer"
24+
"github.com/olekukonko/tablewriter/tw"
2325
)
2426

2527
// NewTableWriter creates a new table writer with the given output and options.
@@ -31,3 +33,43 @@ func NewTableWriter(output io.Writer, options ...tablewriter.Option) *tablewrite
3133

3234
return table
3335
}
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+
}

util/tablewriter_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)