Skip to content

Commit 07d0eea

Browse files
authored
Merge pull request #3 from noborus/improve-function-order
refactor: reorganize function order and improve code structure in table.go
2 parents 241ba8c + 4e185c0 commit 07d0eea

1 file changed

Lines changed: 70 additions & 70 deletions

File tree

table.go

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,6 @@ type Table struct {
3737
headerStyle HeaderStyle // styling for header row
3838
}
3939

40-
// GetBorderConfig returns the current border configuration.
41-
func (t *Table) GetBorderConfig() TableBorderConfig {
42-
return t.borderConfig
43-
}
44-
45-
// SetAutoAlign sets whether to skip alignment for all columns.
46-
func (t *Table) SetAutoAlign(autoAlign bool) {
47-
t.autoAlign = autoAlign
48-
// Recalculate render mode when alignment setting changes
49-
t.mode = t.determineRenderMode()
50-
51-
// Update renderer based on new mode
52-
if t.borderStyle == MarkdownStyle {
53-
t.renderer = &MarkdownRenderer{}
54-
} else if t.mode == StreamingMode {
55-
t.renderer = &Streaming{}
56-
} else {
57-
t.renderer = &Buffered{}
58-
}
59-
}
60-
61-
// GetAlign returns the current align setting.
62-
func (t *Table) GetAlign() bool {
63-
return t.autoAlign
64-
}
65-
6640
// NewTable creates a new table with the given columns and optional configuration.
6741
//
6842
// This function uses the Functional Option Pattern:
@@ -111,43 +85,6 @@ func NewTable(writer io.Writer, columns []Column, opts ...TableOption) *Table {
11185
return t
11286
}
11387

114-
// Border sets the border style (option).
115-
func Border(style BorderStyle) TableOption {
116-
return func(t *Table) {
117-
t.borderStyle = style
118-
t.borderConfig = GetBorderConfig(style)
119-
t.borders = t.borderConfig.Chars
120-
}
121-
}
122-
123-
// Header sets the header style (option).
124-
func Header(style HeaderStyle) TableOption {
125-
return func(t *Table) {
126-
t.headerStyle = style
127-
}
128-
}
129-
130-
// AutoAlign sets the align flag (option).
131-
func AutoAlign(autoAlign bool) TableOption {
132-
return func(t *Table) {
133-
t.autoAlign = autoAlign
134-
}
135-
}
136-
137-
// BorderConfig sets a custom border configuration (option).
138-
//
139-
// Example:
140-
//
141-
// cfg := termhyo.GetBorderConfig(termhyo.BoxDrawingStyle)
142-
// cfg.Left = false
143-
// table := termhyo.NewTable(os.Stdout, columns, termhyo.BorderConfig(cfg))
144-
func BorderConfig(cfg TableBorderConfig) TableOption {
145-
return func(t *Table) {
146-
t.borderConfig = cfg
147-
t.borders = cfg.Chars
148-
}
149-
}
150-
15188
// determineRenderMode decides whether to use buffered or streaming mode.
15289
func (t *Table) determineRenderMode() RenderMode {
15390
hasAutoWidth := false
@@ -235,7 +172,7 @@ func (t *Table) CalculateColumnWidths() {
235172
}
236173
}
237174

238-
// RenderHeader renders the table header.
175+
// RenderHeader renders the table header row, including the top border and header separator line if enabled.
239176
func (t *Table) RenderHeader() error {
240177
if len(t.columns) == 0 {
241178
return ErrNoColumns
@@ -392,7 +329,7 @@ func (t *Table) formatCell(content string, width int, align Alignment) string {
392329
return padString(content, width, align)
393330
}
394331

395-
// For non-padding-disabled mode, width is the content width
332+
// When padding is enabled, the cell width is "content width + padding on both sides"
396333
// Truncate if content is too long for the specified width
397334
if contentWidth > width {
398335
content = truncateString(content, width)
@@ -473,6 +410,69 @@ func (t *Table) RenderFooter() error {
473410
return nil
474411
}
475412

413+
// Border sets the border style (option).
414+
func Border(style BorderStyle) TableOption {
415+
return func(t *Table) {
416+
t.borderStyle = style
417+
t.borderConfig = GetBorderConfig(style)
418+
t.borders = t.borderConfig.Chars
419+
}
420+
}
421+
422+
// BorderConfig sets a custom border configuration (option).
423+
//
424+
// Example:
425+
//
426+
// cfg := termhyo.GetBorderConfig(termhyo.BoxDrawingStyle)
427+
// cfg.Left = false
428+
// table := termhyo.NewTable(os.Stdout, columns, termhyo.BorderConfig(cfg))
429+
func BorderConfig(cfg TableBorderConfig) TableOption {
430+
return func(t *Table) {
431+
t.borderConfig = cfg
432+
t.borders = cfg.Chars
433+
}
434+
}
435+
436+
// Header sets the header style (option).
437+
func Header(style HeaderStyle) TableOption {
438+
return func(t *Table) {
439+
t.headerStyle = style
440+
}
441+
}
442+
443+
// AutoAlign sets the align flag (option).
444+
func AutoAlign(autoAlign bool) TableOption {
445+
return func(t *Table) {
446+
t.autoAlign = autoAlign
447+
}
448+
}
449+
450+
// GetBorderConfig returns the current border configuration.
451+
func (t *Table) GetBorderConfig() TableBorderConfig {
452+
return t.borderConfig
453+
}
454+
455+
// SetAutoAlign sets whether to skip alignment for all columns.
456+
func (t *Table) SetAutoAlign(autoAlign bool) {
457+
t.autoAlign = autoAlign
458+
// Recalculate render mode when alignment setting changes
459+
t.mode = t.determineRenderMode()
460+
461+
// Update renderer based on new mode
462+
if t.borderStyle == MarkdownStyle {
463+
t.renderer = &MarkdownRenderer{}
464+
} else if t.mode == StreamingMode {
465+
t.renderer = &Streaming{}
466+
} else {
467+
t.renderer = &Buffered{}
468+
}
469+
}
470+
471+
// GetAutoAlign returns the current auto-align setting.
472+
func (t *Table) GetAutoAlign() bool {
473+
return t.autoAlign
474+
}
475+
476476
// SetRenderer allows setting a custom renderer.
477477
func (t *Table) SetRenderer(renderer Renderer) {
478478
t.renderer = renderer
@@ -501,6 +501,11 @@ func (t *Table) SetHeaderStyle(style HeaderStyle) {
501501
t.headerStyle = style
502502
}
503503

504+
// GetHeaderStyle returns the current header style.
505+
func (t *Table) GetHeaderStyle() HeaderStyle {
506+
return t.headerStyle
507+
}
508+
504509
// SetHeaderStyleWithoutSeparator sets the header style and disables the header separator line.
505510
// This is a convenience method for the common use case of styled headers not needing separators.
506511
func (t *Table) SetHeaderStyleWithoutSeparator(style HeaderStyle) {
@@ -548,8 +553,3 @@ func (t *Table) SetHeaderStyleMinimal(style HeaderStyle) {
548553
t.borderConfig.Vertical = false // No column separators either
549554
t.borders = t.borderConfig.Chars
550555
}
551-
552-
// GetHeaderStyle returns the current header style.
553-
func (t *Table) GetHeaderStyle() HeaderStyle {
554-
return t.headerStyle
555-
}

0 commit comments

Comments
 (0)