Skip to content
Merged
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
2 changes: 1 addition & 1 deletion util/common.go → helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util
package helpers

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion util/common_test.go → helpers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util
package helpers

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion util/doc.go → helpers/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util //nolint: revive
package helpers
75 changes: 75 additions & 0 deletions helpers/tablewriter.go
Original file line number Diff line number Diff line change
@@ -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...)
}
40 changes: 39 additions & 1 deletion util/tablewriter_test.go → helpers/tablewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package util
package helpers

import (
"bytes"
Expand Down Expand Up @@ -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)
})
}
File renamed without changes.
File renamed without changes.
33 changes: 0 additions & 33 deletions util/tablewriter.go

This file was deleted.