Skip to content

Commit 28a04cd

Browse files
committed
table: remove horizontal orientation
1 parent bfcb953 commit 28a04cd

File tree

4 files changed

+44
-160
lines changed

4 files changed

+44
-160
lines changed

internal/ascii/table/examples_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,3 @@ func ExampleDefine() {
5252
// Yaya | 5 | 10
5353
// Yuumi | 5 | 10
5454
}
55-
56-
func ExampleHorizontally() {
57-
type Cat struct {
58-
Name string
59-
Age int
60-
Cuteness int
61-
}
62-
63-
tbl := table.Define[Cat](
64-
table.String("name", 7, table.AlignRight, func(c Cat) string { return c.Name }),
65-
table.Div(),
66-
table.Int("age", 4, table.AlignRight, func(c Cat) int { return c.Age }),
67-
table.Int("cuteness", 8, table.AlignRight, func(c Cat) int { return c.Cuteness }),
68-
)
69-
70-
board := ascii.Make(8, 1)
71-
fmt.Println("Cool cats:")
72-
opts := table.RenderOptions{Orientation: table.Horizontally}
73-
tbl.Render(board.At(0, 0), opts, []Cat{
74-
{Name: "Chicken", Age: 5, Cuteness: 10},
75-
{Name: "Heart", Age: 4, Cuteness: 10},
76-
{Name: "Mai", Age: 2, Cuteness: 10},
77-
{Name: "Poi", Age: 150000000, Cuteness: 10},
78-
{Name: "Pigeon", Age: 2, Cuteness: 10},
79-
{Name: "Sugar", Age: 8, Cuteness: 1000000000},
80-
{Name: "Yaya", Age: 5, Cuteness: 10},
81-
{Name: "Yuumi", Age: 5, Cuteness: 10},
82-
{Name: "Yuumibestcatever", Age: 5, Cuteness: 100},
83-
}...)
84-
fmt.Println(board.String())
85-
// Output:
86-
// Cool cats:
87-
// name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi Yuumibestcatever
88-
// ---------+----------------------------------------------------------------------------------
89-
// age | 5 4 2 150000000 2 8 5 5 5
90-
// cuteness | 10 10 10 10 10 1000000000 10 10 100
91-
}

internal/ascii/table/table.go

Lines changed: 44 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ func (hd HorizontalDividers) Contains(rowIdx int) bool {
8686

8787
// RenderOptions specifies the options for rendering a table.
8888
type RenderOptions struct {
89-
Orientation Orientation
9089
HorizontalDividers HorizontalDividers
9190
}
9291

@@ -95,115 +94,68 @@ type RenderOptions struct {
9594
func (d *Layout[T]) Render(start ascii.Cursor, opts RenderOptions, rows ...T) ascii.Cursor {
9695
cur := start
9796

98-
if opts.Orientation == Vertically {
99-
tuples := rows
100-
vals := make([]string, len(tuples))
101-
for fieldIdx, c := range d.fields {
102-
if fieldIdx > 0 {
103-
// Each column is separated by a space from the previous column or
104-
// separator.
105-
rowCur := cur
106-
for rowIdx := range tuples {
107-
if opts.HorizontalDividers.Contains(rowIdx) {
108-
rowCur = rowCur.Down(1)
109-
rowCur.WriteString("-")
110-
}
111-
rowCur = rowCur.Down(1)
112-
}
113-
cur = cur.Right(1)
114-
}
115-
if _, ok := c.(divider); ok {
116-
rowCur := cur
117-
rowCur.WriteString("|")
118-
for rowIdx := range tuples {
119-
if opts.HorizontalDividers.Contains(rowIdx) {
120-
rowCur = rowCur.Down(1)
121-
rowCur.WriteString("+")
122-
}
97+
tuples := rows
98+
vals := make([]string, len(tuples))
99+
for fieldIdx, c := range d.fields {
100+
if fieldIdx > 0 {
101+
// Each column is separated by a space from the previous column or
102+
// separator.
103+
rowCur := cur
104+
for rowIdx := range tuples {
105+
if opts.HorizontalDividers.Contains(rowIdx) {
123106
rowCur = rowCur.Down(1)
124-
rowCur.WriteString("|")
107+
rowCur.WriteString("-")
125108
}
126-
cur = cur.Right(1)
127-
continue
128-
}
129-
f := c.(Field[T])
130-
for i, t := range tuples {
131-
vals[i] = f.renderValue(i, t)
132-
}
133-
134-
width := c.width()
135-
// If one of the values exceeds the column width, widen the column as
136-
// necessary.
137-
for i := range vals {
138-
width = max(width, len(vals[i]))
109+
rowCur = rowCur.Down(1)
139110
}
140-
header := f.header()
141-
align := f.align()
142-
pad(cur, width, align, header)
111+
cur = cur.Right(1)
112+
}
113+
if _, ok := c.(divider); ok {
143114
rowCur := cur
144-
for rowIdx := range vals {
115+
rowCur.WriteString("|")
116+
for rowIdx := range tuples {
145117
if opts.HorizontalDividers.Contains(rowIdx) {
146118
rowCur = rowCur.Down(1)
147-
rowCur.RepeatByte(width, '-')
119+
rowCur.WriteString("+")
148120
}
149121
rowCur = rowCur.Down(1)
150-
pad(rowCur, width, align, vals[rowIdx])
122+
rowCur.WriteString("|")
151123
}
152-
cur = cur.Right(width)
124+
cur = cur.Right(1)
125+
continue
126+
}
127+
f := c.(Field[T])
128+
for i, t := range tuples {
129+
vals[i] = f.renderValue(i, t)
130+
}
131+
132+
width := c.width()
133+
// If one of the values exceeds the column width, widen the column as
134+
// necessary.
135+
for i := range vals {
136+
width = max(width, len(vals[i]))
153137
}
154-
rowCur := start.Down(len(vals) + 1)
138+
header := f.header()
139+
align := f.align()
140+
pad(cur, width, align, header)
141+
rowCur := cur
155142
for rowIdx := range vals {
156143
if opts.HorizontalDividers.Contains(rowIdx) {
157144
rowCur = rowCur.Down(1)
145+
rowCur.RepeatByte(width, '-')
158146
}
147+
rowCur = rowCur.Down(1)
148+
pad(rowCur, width, align, vals[rowIdx])
159149
}
160-
return rowCur
150+
cur = cur.Right(width)
161151
}
162-
163-
headerColumnWidth := 1
164-
for i := range d.fields {
165-
headerColumnWidth = max(headerColumnWidth, d.fields[i].width())
166-
}
167-
168-
for i := range d.fields {
169-
if _, ok := d.fields[i].(divider); ok {
170-
cur.Down(i).RepeatByte(headerColumnWidth, '-')
171-
} else {
172-
pad(cur.Down(i), headerColumnWidth, AlignRight, d.fields[i].(Field[T]).header())
173-
}
174-
}
175-
cur = cur.Right(headerColumnWidth)
176-
for i := range d.fields {
177-
if _, ok := d.fields[i].(divider); ok {
178-
cur.Down(i).WriteString("-+-")
179-
} else {
180-
cur.Down(i).WriteString(" | ")
181-
}
182-
}
183-
cur = cur.Right(3)
184-
185-
tupleIndex := 0
186-
colSpacing := 0
187-
for _, t := range rows {
188-
width := 1
189-
for i := range d.fields {
190-
if f, ok := d.fields[i].(Field[T]); ok {
191-
width = max(width, len(f.renderValue(tupleIndex, t)))
192-
}
193-
}
194-
for i := range d.fields {
195-
if _, ok := d.fields[i].(divider); ok {
196-
cur.Down(i).RepeatByte(width+colSpacing, '-')
197-
} else {
198-
f := d.fields[i].(Field[T])
199-
pad(cur.Down(i).Right(colSpacing), width, d.fields[i].align(), f.renderValue(tupleIndex, t))
200-
}
152+
rowCur := start.Down(len(vals) + 1)
153+
for rowIdx := range vals {
154+
if opts.HorizontalDividers.Contains(rowIdx) {
155+
rowCur = rowCur.Down(1)
201156
}
202-
tupleIndex++
203-
cur = cur.Right(width + colSpacing)
204-
colSpacing = 2
205157
}
206-
return start.Down(len(d.fields))
158+
return rowCur
207159
}
208160

209161
// Element is the base interface, common to all table elements.
@@ -287,15 +239,6 @@ func pad(cur ascii.Cursor, toWidth int, align Align, s string) ascii.Cursor {
287239
return cur.Right(toWidth)
288240
}
289241

290-
const (
291-
Vertically Orientation = iota
292-
Horizontally
293-
)
294-
295-
// Orientation specifies the orientation of the table. The default orientation
296-
// is vertical.
297-
type Orientation uint8
298-
299242
func String[T any](header string, width int, align Align, fn func(r T) string) Field[T] {
300243
return makeFuncField(header, width, align, func(tupleIndex int, r T) string {
301244
return fn(r)

internal/ascii/table/table_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ func TestTable(t *testing.T) {
3434
wb := ascii.Make(1, 10)
3535
datadriven.RunTest(t, "testdata/table", func(t *testing.T, td *datadriven.TestData) string {
3636
var opts RenderOptions
37-
if td.HasArg("horizontally") {
38-
opts.Orientation = Horizontally
39-
}
4037
align := AlignRight
4138
if td.HasArg("left") {
4239
align = AlignLeft

internal/ascii/table/testdata/table

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ Sugar 8 10
5151
Yaya 5 10
5252
Yuumi 5 10
5353

54-
cats-nodiv horizontally
55-
----
56-
name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi
57-
age | 5 4 2 15 2 8 5 5
58-
cuteness | 10 10 10 10 10 10 10 10
59-
6054
cats-autoincrement
6155
----
6256
idx | name
@@ -88,12 +82,6 @@ idx | name
8882
----+--------
8983
7 | Yuumi
9084

91-
cats-autoincrement horizontally
92-
----
93-
idx | 0 1 2 3 4 5 6 7
94-
--------+-----------------------------------------------------
95-
name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi
96-
9785
cats-column-too-wide
9886
----
9987
name | age c
@@ -147,10 +135,3 @@ Pigeon | 2000000 10
147135
Sugar | 8000000 10
148136
Yaya | 5000000 10
149137
Yuumi | 5000000 10
150-
151-
cats-column-too-wide horizontally
152-
----
153-
name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi
154-
-------+------------------------------------------------------------------------
155-
age | 5000000 4000000 2000000 15000000 2000000 8000000 5000000 5000000
156-
c | 10 10 10 10 10 10 10 10

0 commit comments

Comments
 (0)