@@ -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.
1111type 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.
2020func 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.
3232func (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.
3737func (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.
4242func (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.
4848func (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.
106106func (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.
115115func 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