@@ -5,43 +5,8 @@ import (
55 "strings"
66)
77
8- // TableStyle represents the complete styling configuration for a table.
9- type TableStyle struct {
10- BorderStyle BorderStyle // Border style configuration
11- HeaderStyle HeaderStyle // Header styling configuration
12- }
13-
14- // DefaultTableStyle returns the default table style.
15- func DefaultTableStyle () TableStyle {
16- return TableStyle {
17- BorderStyle : BoxDrawingStyle ,
18- HeaderStyle : HeaderStyle {},
19- }
20- }
21-
22- // WithBorderStyle creates a TableStyle with the specified border style.
23- func WithBorderStyle (borderStyle BorderStyle ) TableStyle {
24- return TableStyle {
25- BorderStyle : borderStyle ,
26- HeaderStyle : HeaderStyle {},
27- }
28- }
29-
30- // WithHeaderStyle creates a TableStyle with the specified header style.
31- func WithHeaderStyle (headerStyle HeaderStyle ) TableStyle {
32- return TableStyle {
33- BorderStyle : BoxDrawingStyle ,
34- HeaderStyle : headerStyle ,
35- }
36- }
37-
38- // WithStyles creates a TableStyle with both border and header styles.
39- func WithStyles (borderStyle BorderStyle , headerStyle HeaderStyle ) TableStyle {
40- return TableStyle {
41- BorderStyle : borderStyle ,
42- HeaderStyle : headerStyle ,
43- }
44- }
8+ // TableOption is a functional option for configuring Table.
9+ type TableOption func (* Table )
4510
4611// Table represents the main table structure.
4712type Table struct {
@@ -84,25 +49,37 @@ func (t *Table) GetAlign() bool {
8449 return t .align
8550}
8651
87- // NewTable creates a new table with default styling.
88- func NewTable (writer io.Writer , columns []Column ) * Table {
89- return NewTableWithStyle (writer , columns , DefaultTableStyle ())
90- }
91-
92- // NewTableWithStyle creates a new table with specified styling.
93- func NewTableWithStyle (writer io.Writer , columns []Column , style TableStyle ) * Table {
94- borderConfig := getBorderConfig (style .BorderStyle )
52+ // NewTable creates a new table with the given columns and optional configuration.
53+ //
54+ // This function uses the Functional Option Pattern:
55+ //
56+ // table := termhyo.NewTable(os.Stdout, columns, termhyo.Border(termhyo.BoxDrawingStyle), termhyo.Header(headerStyle), ...)
57+ //
58+ // You can specify border style, header style, alignment, etc. by passing option functions.
59+ //
60+ // Example:
61+ //
62+ // table := termhyo.NewTable(os.Stdout, columns, termhyo.Border(termhyo.DoubleStyle), termhyo.Align(false))
63+ //
64+ // This is the recommended way to create and configure tables in termhyo.
65+ func NewTable (writer io.Writer , columns []Column , opts ... TableOption ) * Table {
66+ borderConfig := GetBorderConfig (BoxDrawingStyle )
9567
9668 t := & Table {
9769 columns : columns ,
9870 writer : writer ,
9971 rows : make ([]Row , 0 ),
10072 padding : 1 ,
10173 align : true , // Default to aligned columns
102- borderStyle : style . BorderStyle ,
74+ borderStyle : BoxDrawingStyle ,
10375 borderConfig : borderConfig ,
10476 borders : borderConfig .Chars ,
105- headerStyle : style .HeaderStyle ,
77+ headerStyle : HeaderStyle {},
78+ }
79+
80+ // Apply options
81+ for _ , opt := range opts {
82+ opt (t )
10683 }
10784
10885 // Determine render mode based on column configuration
@@ -120,6 +97,43 @@ func NewTableWithStyle(writer io.Writer, columns []Column, style TableStyle) *Ta
12097 return t
12198}
12299
100+ // Border sets the border style (option).
101+ func Border (style BorderStyle ) TableOption {
102+ return func (t * Table ) {
103+ t .borderStyle = style
104+ t .borderConfig = GetBorderConfig (style )
105+ t .borders = t .borderConfig .Chars
106+ }
107+ }
108+
109+ // Header sets the header style (option).
110+ func Header (style HeaderStyle ) TableOption {
111+ return func (t * Table ) {
112+ t .headerStyle = style
113+ }
114+ }
115+
116+ // Align sets the align flag (option).
117+ func Align (align bool ) TableOption {
118+ return func (t * Table ) {
119+ t .align = align
120+ }
121+ }
122+
123+ // BorderConfigOpt sets a custom border configuration (option).
124+ //
125+ // Example:
126+ //
127+ // cfg := termhyo.GetBorderConfig(termhyo.BoxDrawingStyle)
128+ // cfg.Left = false
129+ // table := termhyo.NewTable(os.Stdout, columns, termhyo.BorderConfigOpt(cfg))
130+ func BorderConfigOpt (cfg BorderConfig ) TableOption {
131+ return func (t * Table ) {
132+ t .borderConfig = cfg
133+ t .borders = cfg .Chars
134+ }
135+ }
136+
123137// determineRenderMode decides whether to use buffered or streaming mode.
124138func (t * Table ) determineRenderMode () RenderMode {
125139 hasAutoWidth := false
@@ -403,13 +417,16 @@ func (t *Table) formatCell(content string, width int, align Alignment) string {
403417func (t * Table ) RenderBorderLine (position string ) error {
404418 var builder strings.Builder
405419
406- switch position {
407- case "top" :
408- builder .WriteString (t .borders ["top_left" ])
409- case "bottom" :
410- builder .WriteString (t .borders ["bottom_left" ])
411- default : // middle
412- builder .WriteString (t .borders ["left_cross" ])
420+ // left border (only if enabled)
421+ if t .borderConfig .Left {
422+ switch position {
423+ case "top" :
424+ builder .WriteString (t .borders ["top_left" ])
425+ case "bottom" :
426+ builder .WriteString (t .borders ["bottom_left" ])
427+ default :
428+ builder .WriteString (t .borders ["left_cross" ])
429+ }
413430 }
414431
415432 for i , col := range t .columns {
@@ -420,7 +437,8 @@ func (t *Table) RenderBorderLine(position string) error {
420437 }
421438 builder .WriteString (strings .Repeat (t .borders ["horizontal" ], cellWidth ))
422439
423- if i < len (t .columns )- 1 {
440+ // Draw vertical separator between columns only if enabled
441+ if t .borderConfig .Vertical && i < len (t .columns )- 1 {
424442 switch position {
425443 case "top" :
426444 builder .WriteString (t .borders ["top_cross" ])
@@ -432,13 +450,16 @@ func (t *Table) RenderBorderLine(position string) error {
432450 }
433451 }
434452
435- switch position {
436- case "top" :
437- builder .WriteString (t .borders ["top_right" ])
438- case "bottom" :
439- builder .WriteString (t .borders ["bottom_right" ])
440- default :
441- builder .WriteString (t .borders ["right_cross" ])
453+ // right border (only if enabled)
454+ if t .borderConfig .Right {
455+ switch position {
456+ case "top" :
457+ builder .WriteString (t .borders ["top_right" ])
458+ case "bottom" :
459+ builder .WriteString (t .borders ["bottom_right" ])
460+ default :
461+ builder .WriteString (t .borders ["right_cross" ])
462+ }
442463 }
443464
444465 builder .WriteString ("\n " )
@@ -463,7 +484,7 @@ func (t *Table) SetRenderer(renderer Renderer) {
463484// SetBorderStyle changes the border style of the table.
464485func (t * Table ) SetBorderStyle (style BorderStyle ) {
465486 t .borderStyle = style
466- t .borderConfig = getBorderConfig (style )
487+ t .borderConfig = GetBorderConfig (style )
467488 t .borders = t .borderConfig .Chars
468489}
469490
0 commit comments