Skip to content

Commit 2309bb8

Browse files
committed
table: add AlignCenter
1 parent 77ef79e commit 2309bb8

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

internal/ascii/table/table.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ func (l literal[T]) renderValue(tupleIndex int, tuple T) string {
221221
const (
222222
AlignLeft Align = iota
223223
AlignRight
224+
AlignCenter
224225
)
225226

226227
type Align uint8
@@ -231,12 +232,15 @@ func pad(cur ascii.Cursor, toWidth int, align Align, s string) ascii.Cursor {
231232
if len(s) >= toWidth {
232233
return cur.WriteString(s)
233234
}
234-
startCur := cur
235-
if align == AlignRight {
236-
cur = cur.Right(toWidth - len(s))
235+
padding := 0
236+
switch align {
237+
case AlignRight:
238+
padding = toWidth - len(s)
239+
case AlignCenter:
240+
padding = (toWidth - len(s)) / 2
237241
}
238-
cur.WriteString(s)
239-
return startCur.Right(toWidth)
242+
cur.Right(padding).WriteString(s)
243+
return cur.Right(toWidth)
240244
}
241245

242246
const (

internal/ascii/table/table_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,28 @@ func TestTable(t *testing.T) {
3636
if td.HasArg("horizontally") {
3737
opts.Orientation = Horizontally
3838
}
39+
align := AlignRight
40+
if td.HasArg("left") {
41+
align = AlignLeft
42+
}
43+
if td.HasArg("center") {
44+
align = AlignCenter
45+
}
3946
switch td.Cmd {
4047
case "cats-autoincrement":
4148
def := Define[Cat](
4249
AutoIncrement[Cat]("idx", 3, AlignLeft),
4350
Div(),
44-
String("name", 7, AlignRight, func(c Cat) string { return c.Name }),
51+
String("name", 7, align, func(c Cat) string { return c.Name }),
4552
)
4653
wb.Reset(1)
4754
def.Render(wb.At(0, 0), opts, slices.Values(cats))
4855
return wb.String()
4956
case "cats-nodiv":
5057
def := Define[Cat](
5158
String("name", 6, AlignLeft, func(c Cat) string { return c.Name }),
52-
Int("age", 3, AlignRight, func(c Cat) int { return c.Age }),
53-
Int("cuteness", 8, AlignRight, func(c Cat) int { return c.Cuteness }),
59+
Int("age", 3, align, func(c Cat) int { return c.Age }),
60+
Int("cuteness", 8, align, func(c Cat) int { return c.Cuteness }),
5461
)
5562
wb.Reset(1)
5663
def.Render(wb.At(0, 0), opts, slices.Values(cats))
@@ -63,8 +70,8 @@ func TestTable(t *testing.T) {
6370
def := Define[Cat](
6471
String("name", 6, AlignLeft, func(c Cat) string { return c.Name }),
6572
Div(),
66-
Int("age", 3, AlignRight, func(c Cat) int { return c.Age }),
67-
Int("c", 1, AlignRight, func(c Cat) int { return c.Cuteness }),
73+
Int("age", 3, align, func(c Cat) int { return c.Age }),
74+
Int("c", 1, align, func(c Cat) int { return c.Cuteness }),
6875
)
6976
wb.Reset(1)
7077
def.Render(wb.At(0, 0), opts, slices.Values(c))

internal/ascii/table/testdata/table

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ Sugar 8 10
1111
Yaya 5 10
1212
Yuumi 5 10
1313

14+
cats-nodiv left
15+
----
16+
name age cuteness
17+
--------------------
18+
Chicken 5 10
19+
Heart 4 10
20+
Mai 2 10
21+
Poi 15 10
22+
Pigeon 2 10
23+
Sugar 8 10
24+
Yaya 5 10
25+
Yuumi 5 10
26+
27+
cats-nodiv center
28+
----
29+
name age cuteness
30+
--------------------
31+
Chicken 5 10
32+
Heart 4 10
33+
Mai 2 10
34+
Poi 15 10
35+
Pigeon 2 10
36+
Sugar 8 10
37+
Yaya 5 10
38+
Yuumi 5 10
39+
1440
cats-nodiv horizontally
1541
----
1642
name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi
@@ -49,6 +75,32 @@ Sugar | 8000000 10
4975
Yaya | 5000000 10
5076
Yuumi | 5000000 10
5177

78+
cats-column-too-wide left
79+
----
80+
name | age c
81+
--------+------------
82+
Chicken | 5000000 10
83+
Heart | 4000000 10
84+
Mai | 2000000 10
85+
Poi | 15000000 10
86+
Pigeon | 2000000 10
87+
Sugar | 8000000 10
88+
Yaya | 5000000 10
89+
Yuumi | 5000000 10
90+
91+
cats-column-too-wide center
92+
----
93+
name | age c
94+
--------+------------
95+
Chicken | 5000000 10
96+
Heart | 4000000 10
97+
Mai | 2000000 10
98+
Poi | 15000000 10
99+
Pigeon | 2000000 10
100+
Sugar | 8000000 10
101+
Yaya | 5000000 10
102+
Yuumi | 5000000 10
103+
52104
cats-column-too-wide horizontally
53105
----
54106
name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi

0 commit comments

Comments
 (0)