diff --git a/util/common.go b/helpers/common.go similarity index 99% rename from util/common.go rename to helpers/common.go index 9469cd3..ee98c27 100644 --- a/util/common.go +++ b/helpers/common.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package helpers import ( "bufio" diff --git a/util/common_test.go b/helpers/common_test.go similarity index 99% rename from util/common_test.go rename to helpers/common_test.go index 73f9c15..79ffd46 100644 --- a/util/common_test.go +++ b/helpers/common_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package helpers import ( "errors" diff --git a/util/doc.go b/helpers/doc.go similarity index 95% rename from util/doc.go rename to helpers/doc.go index dd5f235..d2bbc24 100644 --- a/util/doc.go +++ b/helpers/doc.go @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util //nolint: revive +package helpers diff --git a/helpers/tablewriter.go b/helpers/tablewriter.go new file mode 100644 index 0000000..d793fa1 --- /dev/null +++ b/helpers/tablewriter.go @@ -0,0 +1,75 @@ +/* +Copyright 2025 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package helpers + +import ( + "io" + + "github.com/olekukonko/tablewriter" + "github.com/olekukonko/tablewriter/renderer" + "github.com/olekukonko/tablewriter/tw" +) + +// NewTableWriter creates a new table writer with the given output and options. +func NewTableWriter(output io.Writer, options ...tablewriter.Option) *tablewriter.Table { + table := tablewriter.NewWriter(output) + for _, opt := range options { + table.Options(opt) + } + + return table +} + +// NewTableWriterWithDefaults creates a new table writer with default markdown configuration. +// It includes left alignment, markdown renderer, and custom borders optimized for terminal output. +func NewTableWriterWithDefaults(output io.Writer, options ...tablewriter.Option) *tablewriter.Table { + defaultOptions := []tablewriter.Option{ + tablewriter.WithConfig(tablewriter.Config{ + Header: tw.CellConfig{ + Alignment: tw.CellAlignment{Global: tw.AlignLeft}, + }, + }), + tablewriter.WithRenderer(renderer.NewMarkdown()), + tablewriter.WithRendition(tw.Rendition{ + Symbols: tw.NewSymbols(tw.StyleMarkdown), + Borders: tw.Border{ + Left: tw.On, + Top: tw.Off, + Right: tw.On, + Bottom: tw.Off, + }, + Settings: tw.Settings{ + Separators: tw.Separators{ + BetweenRows: tw.On, + }, + }, + }), + tablewriter.WithRowAutoWrap(tw.WrapNone), + } + + defaultOptions = append(defaultOptions, options...) + + return NewTableWriter(output, defaultOptions...) +} + +// NewTableWriterWithDefaultsAndHeader creates a new table writer with default configuration and header. +func NewTableWriterWithDefaultsAndHeader(output io.Writer, header []string, options ...tablewriter.Option) *tablewriter.Table { + headerOption := tablewriter.WithHeader(header) + allOptions := append([]tablewriter.Option{headerOption}, options...) + + return NewTableWriterWithDefaults(output, allOptions...) +} diff --git a/util/tablewriter_test.go b/helpers/tablewriter_test.go similarity index 81% rename from util/tablewriter_test.go rename to helpers/tablewriter_test.go index ccdedcc..5424f01 100644 --- a/util/tablewriter_test.go +++ b/helpers/tablewriter_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package helpers import ( "bytes" @@ -161,3 +161,41 @@ func TestNewTableWriter(t *testing.T) { compareGolden(t, output.String(), "multiple_rows.golden") }) } + +func TestNewTableWriterWithDefaults(t *testing.T) { + t.Parallel() + + t.Run("WithDefaults", func(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + table := NewTableWriterWithDefaults(&output) + + require.NotNil(t, table) + require.IsType(t, &tablewriter.Table{}, table) + }) + + t.Run("WithDefaultsAndHeader", func(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + header := []string{"TESTGRID BOARD", "TITLE", "STATUS", "STATUS DETAILS"} + table := NewTableWriterWithDefaultsAndHeader(&output, header) + + require.NotNil(t, table) + require.IsType(t, &tablewriter.Table{}, table) + }) + + t.Run("WithDefaultsAndAdditionalOptions", func(t *testing.T) { + t.Parallel() + + var output bytes.Buffer + + table := NewTableWriterWithDefaults(&output, tablewriter.WithMaxWidth(100)) + + require.NotNil(t, table) + require.IsType(t, &tablewriter.Table{}, table) + }) +} diff --git a/util/testdata/empty_table.golden b/helpers/testdata/empty_table.golden similarity index 100% rename from util/testdata/empty_table.golden rename to helpers/testdata/empty_table.golden diff --git a/util/testdata/multiple_rows.golden b/helpers/testdata/multiple_rows.golden similarity index 100% rename from util/testdata/multiple_rows.golden rename to helpers/testdata/multiple_rows.golden diff --git a/util/testdata/no_options.golden b/helpers/testdata/no_options.golden similarity index 100% rename from util/testdata/no_options.golden rename to helpers/testdata/no_options.golden diff --git a/util/testdata/with_footer_option.golden b/helpers/testdata/with_footer_option.golden similarity index 100% rename from util/testdata/with_footer_option.golden rename to helpers/testdata/with_footer_option.golden diff --git a/util/testdata/with_header_option.golden b/helpers/testdata/with_header_option.golden similarity index 100% rename from util/testdata/with_header_option.golden rename to helpers/testdata/with_header_option.golden diff --git a/util/testdata/with_multiple_options.golden b/helpers/testdata/with_multiple_options.golden similarity index 100% rename from util/testdata/with_multiple_options.golden rename to helpers/testdata/with_multiple_options.golden diff --git a/util/testdata/with_single_option.golden b/helpers/testdata/with_single_option.golden similarity index 100% rename from util/testdata/with_single_option.golden rename to helpers/testdata/with_single_option.golden diff --git a/util/tablewriter.go b/util/tablewriter.go deleted file mode 100644 index ed09c19..0000000 --- a/util/tablewriter.go +++ /dev/null @@ -1,33 +0,0 @@ -/* -Copyright 2025 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "io" - - "github.com/olekukonko/tablewriter" -) - -// NewTableWriter creates a new table writer with the given output and options. -func NewTableWriter(output io.Writer, options ...tablewriter.Option) *tablewriter.Table { - table := tablewriter.NewWriter(output) - for _, opt := range options { - table.Options(opt) - } - - return table -}