@@ -43,7 +43,7 @@ class Table
43
43
*
44
44
* @var array
45
45
*/
46
- private $ columnWidths = array ();
46
+ private $ effectiveColumnWidths = array ();
47
47
48
48
/**
49
49
* Number of columns cache.
@@ -67,6 +67,13 @@ class Table
67
67
*/
68
68
private $ columnStyles = array ();
69
69
70
+ /**
71
+ * User set column widths.
72
+ *
73
+ * @var array
74
+ */
75
+ private $ columnWidths = array ();
76
+
70
77
private static $ styles ;
71
78
72
79
public function __construct (OutputInterface $ output )
@@ -186,6 +193,38 @@ public function getColumnStyle($columnIndex)
186
193
return $ this ->getStyle ();
187
194
}
188
195
196
+ /**
197
+ * Sets the minimum width of a column.
198
+ *
199
+ * @param int $columnIndex Column index
200
+ * @param int $width Minimum column width in characters
201
+ *
202
+ * @return Table
203
+ */
204
+ public function setColumnWidth ($ columnIndex , $ width )
205
+ {
206
+ $ this ->columnWidths [intval ($ columnIndex )] = intval ($ width );
207
+
208
+ return $ this ;
209
+ }
210
+
211
+ /**
212
+ * Sets the minimum width of all columns.
213
+ *
214
+ * @param array $widths
215
+ *
216
+ * @return Table
217
+ */
218
+ public function setColumnWidths (array $ widths )
219
+ {
220
+ $ this ->columnWidths = array ();
221
+ foreach ($ widths as $ index => $ width ) {
222
+ $ this ->setColumnWidth ($ index , $ width );
223
+ }
224
+
225
+ return $ this ;
226
+ }
227
+
189
228
public function setHeaders (array $ headers )
190
229
{
191
230
$ headers = array_values ($ headers );
@@ -296,7 +335,7 @@ private function renderRowSeparator()
296
335
297
336
$ markup = $ this ->style ->getCrossingChar ();
298
337
for ($ column = 0 ; $ column < $ count ; ++$ column ) {
299
- $ markup .= str_repeat ($ this ->style ->getHorizontalBorderChar (), $ this ->columnWidths [$ column ]).$ this ->style ->getCrossingChar ();
338
+ $ markup .= str_repeat ($ this ->style ->getHorizontalBorderChar (), $ this ->effectiveColumnWidths [$ column ]).$ this ->style ->getCrossingChar ();
300
339
}
301
340
302
341
$ this ->output ->writeln (sprintf ($ this ->style ->getBorderFormat (), $ markup ));
@@ -342,11 +381,11 @@ private function renderRow(array $row, $cellFormat)
342
381
private function renderCell (array $ row , $ column , $ cellFormat )
343
382
{
344
383
$ cell = isset ($ row [$ column ]) ? $ row [$ column ] : '' ;
345
- $ width = $ this ->columnWidths [$ column ];
384
+ $ width = $ this ->effectiveColumnWidths [$ column ];
346
385
if ($ cell instanceof TableCell && $ cell ->getColspan () > 1 ) {
347
386
// add the width of the following columns(numbers of colspan).
348
387
foreach (range ($ column + 1 , $ column + $ cell ->getColspan () - 1 ) as $ nextColumn ) {
349
- $ width += $ this ->getColumnSeparatorWidth () + $ this ->columnWidths [$ nextColumn ];
388
+ $ width += $ this ->getColumnSeparatorWidth () + $ this ->effectiveColumnWidths [$ nextColumn ];
350
389
}
351
390
}
352
391
@@ -572,7 +611,7 @@ private function calculateColumnsWidth($rows)
572
611
$ lengths [] = $ this ->getCellWidth ($ row , $ column );
573
612
}
574
613
575
- $ this ->columnWidths [$ column ] = max ($ lengths ) + strlen ($ this ->style ->getCellRowContentFormat ()) - 2 ;
614
+ $ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + strlen ($ this ->style ->getCellRowContentFormat ()) - 2 ;
576
615
}
577
616
}
578
617
@@ -596,26 +635,28 @@ private function getColumnSeparatorWidth()
596
635
*/
597
636
private function getCellWidth (array $ row , $ column )
598
637
{
638
+ $ cellWidth = 0 ;
639
+
599
640
if (isset ($ row [$ column ])) {
600
641
$ cell = $ row [$ column ];
601
642
$ cellWidth = Helper::strlenWithoutDecoration ($ this ->output ->getFormatter (), $ cell );
602
643
if ($ cell instanceof TableCell && $ cell ->getColspan () > 1 ) {
603
644
// we assume that cell value will be across more than one column.
604
645
$ cellWidth = $ cellWidth / $ cell ->getColspan ();
605
646
}
606
-
607
- return $ cellWidth ;
608
647
}
609
648
610
- return 0 ;
649
+ $ columnWidth = isset ($ this ->columnWidths [$ column ]) ? $ this ->columnWidths [$ column ] : 0 ;
650
+
651
+ return max ($ cellWidth , $ columnWidth );
611
652
}
612
653
613
654
/**
614
655
* Called after rendering to cleanup cache data.
615
656
*/
616
657
private function cleanup ()
617
658
{
618
- $ this ->columnWidths = array ();
659
+ $ this ->effectiveColumnWidths = array ();
619
660
$ this ->numberOfColumns = null ;
620
661
}
621
662
0 commit comments