Skip to content

Commit a5ef799

Browse files
committed
style: improve code formatting and documentation in table package
1 parent 0e5775e commit a5ef799

6 files changed

Lines changed: 30 additions & 29 deletions

File tree

cmd/account/account_company_list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"strconv"
66
"strings"
77

8-
"github.com/shopware/shopware-cli/internal/table"
98
"github.com/spf13/cobra"
9+
10+
"github.com/shopware/shopware-cli/internal/table"
1011
)
1112

1213
var accountCompanyListCmd = &cobra.Command{

cmd/account/account_merchant_shop_list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"os"
55
"strconv"
66

7-
"github.com/shopware/shopware-cli/internal/table"
87
"github.com/spf13/cobra"
8+
9+
"github.com/shopware/shopware-cli/internal/table"
910
)
1011

1112
var accountCompanyMerchantShopListCmd = &cobra.Command{

cmd/account/account_producer_extension_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"os"
66
"strconv"
77

8-
"github.com/shopware/shopware-cli/internal/table"
98
"github.com/spf13/cobra"
109

1110
account_api "github.com/shopware/shopware-cli/internal/account-api"
11+
"github.com/shopware/shopware-cli/internal/table"
1212
)
1313

1414
var accountCompanyProducerExtensionListCmd = &cobra.Command{

cmd/extension/extension_validate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77

88
"github.com/spf13/cobra"
99

10-
"github.com/shopware/shopware-cli/internal/table"
11-
1210
"github.com/shopware/shopware-cli/extension"
11+
"github.com/shopware/shopware-cli/internal/table"
1312
"github.com/shopware/shopware-cli/logging"
1413
)
1514

cmd/project/project_debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"os"
66
"path/filepath"
77

8-
"github.com/shopware/shopware-cli/internal/table"
98
"github.com/spf13/cobra"
109

1110
"github.com/shopware/shopware-cli/extension"
11+
"github.com/shopware/shopware-cli/internal/table"
1212
"github.com/shopware/shopware-cli/logging"
1313
"github.com/shopware/shopware-cli/shop"
1414
)

internal/table/table.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import (
77
"github.com/charmbracelet/lipgloss"
88
)
99

10-
// Writer is a wrapper around Charm's table that provides a similar interface to tablewriter
10+
// Writer is a wrapper around Charm's table that provides a similar interface to tablewriter.
1111
type Writer struct {
12-
headers []string
13-
rows [][]string
14-
out io.Writer
15-
baseStyle lipgloss.Style
12+
headers []string
13+
rows [][]string
14+
out io.Writer
15+
baseStyle lipgloss.Style
1616
headerStyle lipgloss.Style
1717
}
1818

19-
// NewWriter creates a new table writer
19+
// NewWriter creates a new table writer.
2020
func NewWriter(out io.Writer) *Writer {
2121
return &Writer{
22-
out: out,
22+
out: out,
2323
baseStyle: lipgloss.NewStyle().
2424
BorderStyle(lipgloss.RoundedBorder()).
2525
BorderForeground(lipgloss.NoColor{}),
@@ -28,37 +28,37 @@ func NewWriter(out io.Writer) *Writer {
2828
}
2929
}
3030

31-
// Header sets the table headers
31+
// Header sets the table headers.
3232
func (w *Writer) SetHeader(headers []string) {
3333
w.headers = headers
3434
}
3535

36-
// Header is an alias to SetHeader for compatibility with tablewriter
36+
// Header is an alias to SetHeader for compatibility with tablewriter.
3737
func (w *Writer) Header(headers []string) {
3838
w.SetHeader(headers)
3939
}
4040

41-
// Append adds a row to the table
41+
// Append adds a row to the table.
4242
func (w *Writer) Append(row []string) error {
4343
w.rows = append(w.rows, row)
4444
return nil
4545
}
4646

47-
// Render prints the table to the writer
47+
// Render prints the table to the writer.
4848
func (w *Writer) Render() error {
4949
// Let's build a simple ASCII table manually instead of using the Bubble Tea component
5050
// to ensure consistent styling across all rows
51-
51+
5252
// Calculate column widths based on content
5353
widths := make([]int, len(w.headers))
54-
54+
5555
// First check header lengths
5656
for i, h := range w.headers {
5757
if len(h) > widths[i] {
5858
widths[i] = len(h)
5959
}
6060
}
61-
61+
6262
// Then check data lengths
6363
for _, row := range w.rows {
6464
for i, cell := range row {
@@ -67,24 +67,24 @@ func (w *Writer) Render() error {
6767
}
6868
}
6969
}
70-
70+
7171
// Add padding
7272
for i := range widths {
7373
widths[i] += 4 // Add extra padding for better readability
7474
}
75-
75+
7676
// Format the header row
7777
headerRow := ""
7878
for i, h := range w.headers {
7979
headerRow += w.formatCell(h, widths[i])
8080
}
81-
81+
8282
// Write header
8383
_, err := fmt.Fprintln(w.out, w.headerStyle.Render(headerRow))
8484
if err != nil {
8585
return err
8686
}
87-
87+
8888
// Write data rows
8989
for _, row := range w.rows {
9090
dataRow := ""
@@ -98,11 +98,11 @@ func (w *Writer) Render() error {
9898
return err
9999
}
100100
}
101-
101+
102102
return nil
103103
}
104104

105-
// formatCell pads a cell to the given width
105+
// formatCell pads a cell to the given width.
106106
func (w *Writer) formatCell(content string, width int) string {
107107
padded := content
108108
for len(padded) < width {
@@ -111,16 +111,16 @@ func (w *Writer) formatCell(content string, width int) string {
111111
return padded
112112
}
113113

114-
// RenderTable is a convenience function to render a simple table
114+
// RenderTable is a convenience function to render a simple table.
115115
func RenderTable(out io.Writer, headers []string, rows [][]string) error {
116116
w := NewWriter(out)
117117
w.SetHeader(headers)
118-
118+
119119
for _, row := range rows {
120120
if err := w.Append(row); err != nil {
121121
return err
122122
}
123123
}
124-
124+
125125
return w.Render()
126126
}

0 commit comments

Comments
 (0)