Skip to content

Commit 6cdc993

Browse files
committed
feat!: introduce type-safe Alignment enum to replace string-based alignment
BREAKING CHANGE: Replace string-based text alignment with typed Alignment enum - Add Alignment type with constants: AlignLeft, AlignCenter, AlignRight, AlignDefault - Update Column.Align and Cell.Align fields to use Alignment type instead of string - Update all functions accepting alignment parameters to use Alignment type - Replace string literals ("left", "center", "right") with type-safe constants - Update all examples, tests, and documentation to use new Alignment constants Benefits: - Compile-time type safety prevents typos in alignment values - IDE autocompletion support for alignment options - Clearer API with explicitly defined alignment options - Better maintainability and refactoring support Migration: - Replace `Align: "left"` with `Align: termhyo.AlignLeft` - Replace `Align: "center"` with `Align: termhyo.AlignCenter` - Replace `Align: "right"` with `Align: termhyo.AlignRight` - Empty string ("") is now represented by `AlignDefault`
1 parent 27155c4 commit 6cdc993

File tree

17 files changed

+161
-116
lines changed

17 files changed

+161
-116
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ import (
7272

7373
func main() {
7474
columns := []termhyo.Column{
75-
{Title: "ID", Width: 0, Align: "right"},
76-
{Title: "Name", Width: 0, Align: "left"},
77-
{Title: "Score", Width: 0, Align: "center"},
75+
{Title: "ID", Width: 0, Align: termhyo.AlignRight},
76+
{Title: "Name", Width: 0, Align: termhyo.AlignLeft},
77+
{Title: "Score", Width: 0, Align: termhyo.AlignCenter},
7878
}
7979

8080
table := termhyo.NewTable(os.Stdout, columns)
@@ -90,6 +90,32 @@ func main() {
9090
table := termhyo.NewTableWithStyle(os.Stdout, columns, termhyo.ASCIIStyle)
9191
```
9292

93+
### Text Alignment
94+
95+
termhyo provides type-safe alignment options:
96+
97+
```go
98+
// Available alignment constants
99+
termhyo.AlignLeft // Left-aligned text
100+
termhyo.AlignCenter // Center-aligned text
101+
termhyo.AlignRight // Right-aligned text
102+
termhyo.AlignDefault // Default/unspecified alignment (defaults to left)
103+
104+
// Column-level alignment
105+
columns := []termhyo.Column{
106+
{Title: "ID", Align: termhyo.AlignRight},
107+
{Title: "Name", Align: termhyo.AlignLeft},
108+
{Title: "Score", Align: termhyo.AlignCenter},
109+
}
110+
111+
// Cell-level alignment (overrides column alignment)
112+
table.AddRowCells(
113+
termhyo.Cell{Content: "1", Align: termhyo.AlignCenter},
114+
termhyo.Cell{Content: "Alice"}, // Uses column alignment
115+
termhyo.Cell{Content: "85"},
116+
)
117+
```
118+
93119
### Custom Border Configuration
94120

95121
```go

column.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
package termhyo
22

3+
// Alignment represents text alignment options.
4+
type Alignment string
5+
6+
const (
7+
// AlignDefault represents default/unspecified alignment.
8+
AlignDefault Alignment = ""
9+
// AlignLeft aligns text to the left.
10+
AlignLeft Alignment = "left"
11+
// AlignCenter aligns text to the center.
12+
AlignCenter Alignment = "center"
13+
// AlignRight aligns text to the right.
14+
AlignRight Alignment = "right"
15+
)
16+
17+
// String returns the string representation of the alignment.
18+
func (a Alignment) String() string {
19+
return string(a)
20+
}
21+
322
// Column defines column properties.
423
type Column struct {
5-
Title string // Column header title
6-
Width int // Column width (0 = auto-width)
7-
MaxWidth int // Maximum width for auto-width columns (0 = no limit)
8-
Align string // Alignment: "left", "center", "right"
24+
Title string // Column header title
25+
Width int // Column width (0 = auto-width)
26+
MaxWidth int // Maximum width for auto-width columns (0 = no limit)
27+
Align Alignment // Alignment: AlignLeft, AlignCenter, AlignRight
928
}
1029

1130
// Cell represents a table cell.
1231
type Cell struct {
13-
Content string // Cell content
14-
Align string // Cell-specific alignment override
32+
Content string // Cell content
33+
Align Alignment // Cell-specific alignment override
1534
}
1635

1736
// Row represents a table row.

examples/basic/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
func main() {
1010
// Define columns
1111
columns := []termhyo.Column{
12-
{Title: "ID", Width: 0, Align: "right"},
13-
{Title: "Name", Width: 0, Align: "left"},
14-
{Title: "Score", Width: 0, Align: "center"},
15-
{Title: "Grade", Width: 0, Align: "center"},
12+
{Title: "ID", Width: 0, Align: termhyo.AlignRight},
13+
{Title: "Name", Width: 0, Align: termhyo.AlignLeft},
14+
{Title: "Score", Width: 0, Align: termhyo.AlignCenter},
15+
{Title: "Grade", Width: 0, Align: termhyo.AlignCenter},
1616
}
1717

1818
// Create table with default style

examples/combining/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
func main() {
1010
// Define columns
1111
columns := []termhyo.Column{
12-
{Title: "Text Type", Width: 0, Align: "left"},
13-
{Title: "Example", Width: 0, Align: "left"},
14-
{Title: "Description", Width: 0, Align: "left"},
12+
{Title: "Text Type", Width: 0, Align: termhyo.AlignLeft},
13+
{Title: "Example", Width: 0, Align: termhyo.AlignLeft},
14+
{Title: "Description", Width: 0, Align: termhyo.AlignLeft},
1515
}
1616

1717
// Create table with default style

examples/custom_borders/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func main() {
1111
// Define columns
1212
columns := []termhyo.Column{
13-
{Title: "Style", Width: 0, Align: "left"},
14-
{Title: "Description", Width: 0, Align: "left"},
15-
{Title: "Borders", Width: 0, Align: "center"},
13+
{Title: "Style", Width: 0, Align: termhyo.AlignLeft},
14+
{Title: "Description", Width: 0, Align: termhyo.AlignLeft},
15+
{Title: "Borders", Width: 0, Align: termhyo.AlignCenter},
1616
}
1717

1818
// Test different border configurations
@@ -46,9 +46,9 @@ func main() {
4646
// Example of custom border configuration - only internal separators
4747
fmt.Printf("=== Custom: Internal separators only ===\n")
4848
customColumns := []termhyo.Column{
49-
{Title: "Column1", Width: 0, Align: "left"},
50-
{Title: "Column2", Width: 0, Align: "center"},
51-
{Title: "Column3", Width: 0, Align: "right"},
49+
{Title: "Column1", Width: 0, Align: termhyo.AlignLeft},
50+
{Title: "Column2", Width: 0, Align: termhyo.AlignCenter},
51+
{Title: "Column3", Width: 0, Align: termhyo.AlignRight},
5252
}
5353

5454
customTable := termhyo.NewTable(os.Stdout, customColumns)

examples/header_full_line/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
func main() {
1111
// Define columns
1212
columns := []termhyo.Column{
13-
{Title: "ID", Width: 0, Align: "right"},
14-
{Title: "Product", Width: 0, Align: "left"},
15-
{Title: "Price", Width: 0, Align: "right"},
16-
{Title: "Status", Width: 0, Align: "center"},
13+
{Title: "ID", Width: 0, Align: termhyo.AlignRight},
14+
{Title: "Product", Width: 0, Align: termhyo.AlignLeft},
15+
{Title: "Price", Width: 0, Align: termhyo.AlignRight},
16+
{Title: "Status", Width: 0, Align: termhyo.AlignCenter},
1717
}
1818

1919
fmt.Println("=== Default (No Header Style) ===")

examples/header_styles/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
func main() {
1111
// Define columns
1212
columns := []termhyo.Column{
13-
{Title: "ID", Width: 0, Align: "right"},
14-
{Title: "Product Name", Width: 0, Align: "left"},
15-
{Title: "Price", Width: 0, Align: "right"},
16-
{Title: "Status", Width: 0, Align: "center"},
13+
{Title: "ID", Width: 0, Align: termhyo.AlignRight},
14+
{Title: "Product Name", Width: 0, Align: termhyo.AlignLeft},
15+
{Title: "Price", Width: 0, Align: termhyo.AlignRight},
16+
{Title: "Status", Width: 0, Align: termhyo.AlignCenter},
1717
}
1818

1919
fmt.Println("=== Default Header Style (with separator line) ===")

examples/japanese/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
func main() {
1010
// Define columns
1111
columns := []termhyo.Column{
12-
{Title: "ID", Width: 0, Align: "right"},
13-
{Title: "名前", Width: 0, Align: "left"},
14-
{Title: "スコア", Width: 0, Align: "center"},
15-
{Title: "グレード", Width: 0, Align: "center"},
16-
{Title: "コメント", Width: 0, Align: "left"},
12+
{Title: "ID", Width: 0, Align: termhyo.AlignRight},
13+
{Title: "名前", Width: 0, Align: termhyo.AlignLeft},
14+
{Title: "スコア", Width: 0, Align: termhyo.AlignCenter},
15+
{Title: "グレード", Width: 0, Align: termhyo.AlignCenter},
16+
{Title: "コメント", Width: 0, Align: termhyo.AlignLeft},
1717
}
1818

1919
// Create table with default style

examples/markdown/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
func main() {
1010
// Define columns with fixed width for streaming Markdown
1111
columns := []termhyo.Column{
12-
{Title: "Time", Width: 0, Align: "left"},
13-
{Title: "Event", Width: 0, Align: "left"},
14-
{Title: "Status", Width: 0, Align: "center"},
12+
{Title: "Time", Width: 0, Align: termhyo.AlignLeft},
13+
{Title: "Event", Width: 0, Align: termhyo.AlignLeft},
14+
{Title: "Status", Width: 0, Align: termhyo.AlignCenter},
1515
}
1616

1717
// Create table with Markdown style

examples/styles/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func main() {
1111
// Define columns
1212
columns := []termhyo.Column{
13-
{Title: "Style", Width: 15, Align: "left"},
14-
{Title: "Description", Width: 30, Align: "left"},
15-
{Title: "Unicode", Width: 10, Align: "center"},
13+
{Title: "Style", Width: 15, Align: termhyo.AlignLeft},
14+
{Title: "Description", Width: 30, Align: termhyo.AlignLeft},
15+
{Title: "Unicode", Width: 10, Align: termhyo.AlignCenter},
1616
}
1717

1818
// Sample data

0 commit comments

Comments
 (0)