@@ -86,7 +86,6 @@ func (hd HorizontalDividers) Contains(rowIdx int) bool {
86
86
87
87
// RenderOptions specifies the options for rendering a table.
88
88
type RenderOptions struct {
89
- Orientation Orientation
90
89
HorizontalDividers HorizontalDividers
91
90
}
92
91
@@ -95,115 +94,68 @@ type RenderOptions struct {
95
94
func (d * Layout [T ]) Render (start ascii.Cursor , opts RenderOptions , rows ... T ) ascii.Cursor {
96
95
cur := start
97
96
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 ) {
123
106
rowCur = rowCur .Down (1 )
124
- rowCur .WriteString ("| " )
107
+ rowCur .WriteString ("- " )
125
108
}
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 )
139
110
}
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 {
143
114
rowCur := cur
144
- for rowIdx := range vals {
115
+ rowCur .WriteString ("|" )
116
+ for rowIdx := range tuples {
145
117
if opts .HorizontalDividers .Contains (rowIdx ) {
146
118
rowCur = rowCur .Down (1 )
147
- rowCur .RepeatByte ( width , '-' )
119
+ rowCur .WriteString ( "+" )
148
120
}
149
121
rowCur = rowCur .Down (1 )
150
- pad ( rowCur , width , align , vals [ rowIdx ] )
122
+ rowCur . WriteString ( "|" )
151
123
}
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 ]))
153
137
}
154
- rowCur := start .Down (len (vals ) + 1 )
138
+ header := f .header ()
139
+ align := f .align ()
140
+ pad (cur , width , align , header )
141
+ rowCur := cur
155
142
for rowIdx := range vals {
156
143
if opts .HorizontalDividers .Contains (rowIdx ) {
157
144
rowCur = rowCur .Down (1 )
145
+ rowCur .RepeatByte (width , '-' )
158
146
}
147
+ rowCur = rowCur .Down (1 )
148
+ pad (rowCur , width , align , vals [rowIdx ])
159
149
}
160
- return rowCur
150
+ cur = cur . Right ( width )
161
151
}
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 )
201
156
}
202
- tupleIndex ++
203
- cur = cur .Right (width + colSpacing )
204
- colSpacing = 2
205
157
}
206
- return start . Down ( len ( d . fields ))
158
+ return rowCur
207
159
}
208
160
209
161
// 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 {
287
239
return cur .Right (toWidth )
288
240
}
289
241
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
-
299
242
func String [T any ](header string , width int , align Align , fn func (r T ) string ) Field [T ] {
300
243
return makeFuncField (header , width , align , func (tupleIndex int , r T ) string {
301
244
return fn (r )
0 commit comments