@@ -105,6 +105,8 @@ func (t *Table) initForRender() {
105
105
106
106
// find the longest continuous line in each column
107
107
t .initForRenderColumnLengths ()
108
+ t .initForRenderMaxRowLength ()
109
+ t .initForRenderPaddedColumns ()
108
110
109
111
// generate a separator row and calculate maximum row length
110
112
t .initForRenderRowSeparator ()
@@ -172,6 +174,50 @@ func (t *Table) initForRenderHideColumns() {
172
174
t .columnConfigMap = columnConfigMap
173
175
}
174
176
177
+ func (t * Table ) initForRenderMaxRowLength () {
178
+ t .maxRowLength = 0
179
+ if t .autoIndex {
180
+ t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingLeft )
181
+ t .maxRowLength += len (fmt .Sprint (len (t .rows )))
182
+ t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingRight )
183
+ if t .style .Options .SeparateColumns {
184
+ t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .MiddleSeparator )
185
+ }
186
+ }
187
+ if t .style .Options .SeparateColumns {
188
+ t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .MiddleSeparator ) * (t .numColumns - 1 )
189
+ }
190
+ for _ , maxColumnLength := range t .maxColumnLengths {
191
+ maxColumnLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingLeft + t .style .Box .PaddingRight )
192
+ t .maxRowLength += maxColumnLength
193
+ }
194
+ if t .style .Options .DrawBorder {
195
+ t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .Left + t .style .Box .Right )
196
+ }
197
+ }
198
+
199
+ func (t * Table ) initForRenderPaddedColumns () {
200
+ paddingSize := t .style .Size .WidthMin - t .maxRowLength
201
+ for paddingSize > 0 {
202
+ // distribute padding equally among all columns
203
+ numColumnsPadded := 0
204
+ for colIdx := 0 ; paddingSize > 0 && colIdx < t .numColumns ; colIdx ++ {
205
+ colWidthMax := t .getColumnWidthMax (colIdx )
206
+ if colWidthMax == 0 || t .maxColumnLengths [colIdx ] < colWidthMax {
207
+ t .maxColumnLengths [colIdx ]++
208
+ numColumnsPadded ++
209
+ paddingSize --
210
+ }
211
+ }
212
+
213
+ // avoid endless looping because all columns are at max size and cannot
214
+ // be expanded any further
215
+ if numColumnsPadded == 0 {
216
+ break
217
+ }
218
+ }
219
+ }
220
+
175
221
func (t * Table ) initForRenderRows () {
176
222
// auto-index: calc the index column's max length
177
223
t .autoIndexVIndexMaxLength = len (fmt .Sprint (len (t .rowsRaw )))
@@ -206,27 +252,11 @@ func (t *Table) initForRenderRowsStringify(rows []Row, hint renderHint) []rowStr
206
252
}
207
253
208
254
func (t * Table ) initForRenderRowSeparator () {
209
- t .maxRowLength = 0
210
- if t .autoIndex {
211
- t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingLeft )
212
- t .maxRowLength += len (fmt .Sprint (len (t .rows )))
213
- t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingRight )
214
- if t .style .Options .SeparateColumns {
215
- t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .MiddleSeparator )
216
- }
217
- }
218
- if t .style .Options .SeparateColumns {
219
- t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .MiddleSeparator ) * (t .numColumns - 1 )
220
- }
221
255
t .rowSeparator = make (rowStr , t .numColumns )
222
256
for colIdx , maxColumnLength := range t .maxColumnLengths {
223
257
maxColumnLength += text .RuneWidthWithoutEscSequences (t .style .Box .PaddingLeft + t .style .Box .PaddingRight )
224
- t .maxRowLength += maxColumnLength
225
258
t .rowSeparator [colIdx ] = text .RepeatAndTrim (t .style .Box .MiddleHorizontal , maxColumnLength )
226
259
}
227
- if t .style .Options .DrawBorder {
228
- t .maxRowLength += text .RuneWidthWithoutEscSequences (t .style .Box .Left + t .style .Box .Right )
229
- }
230
260
}
231
261
232
262
func (t * Table ) initForRenderSortRows () {
0 commit comments