*
* @var array
*/
- private $columnWidths = array();
+ private $effectiveColumnWidths = array();
/**
* Number of columns cache.
*/
private $columnStyles = array();
+ /**
+ * User set column widths.
+ *
+ * @var array
+ */
+ private $columnWidths = array();
+
private static $styles;
public function __construct(OutputInterface $output)
return $this->getStyle();
}
+ /**
+ * Sets the minimum width of a column.
+ *
+ * @param int $columnIndex Column index
+ * @param int $width Minimum column width in characters
+ *
+ * @return $this
+ */
+ public function setColumnWidth($columnIndex, $width)
+ {
+ $this->columnWidths[(int) $columnIndex] = (int) $width;
+
+ return $this;
+ }
+
+ /**
+ * Sets the minimum width of all columns.
+ *
+ * @param array $widths
+ *
+ * @return $this
+ */
+ public function setColumnWidths(array $widths)
+ {
+ $this->columnWidths = array();
+ foreach ($widths as $index => $width) {
+ $this->setColumnWidth($index, $width);
+ }
+
+ return $this;
+ }
+
public function setHeaders(array $headers)
{
$headers = array_values($headers);
$markup = $this->style->getCrossingChar();
for ($column = 0; $column < $count; ++$column) {
- $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->columnWidths[$column]).$this->style->getCrossingChar();
+ $markup .= str_repeat($this->style->getHorizontalBorderChar(), $this->effectiveColumnWidths[$column]).$this->style->getCrossingChar();
}
$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
private function renderCell(array $row, $column, $cellFormat)
{
$cell = isset($row[$column]) ? $row[$column] : '';
- $width = $this->columnWidths[$column];
+ $width = $this->effectiveColumnWidths[$column];
if ($cell instanceof TableCell && $cell->getColspan() > 1) {
// add the width of the following columns(numbers of colspan).
foreach (range($column + 1, $column + $cell->getColspan() - 1) as $nextColumn) {
- $width += $this->getColumnSeparatorWidth() + $this->columnWidths[$nextColumn];
+ $width += $this->getColumnSeparatorWidth() + $this->effectiveColumnWidths[$nextColumn];
}
}
$lengths[] = $this->getCellWidth($row, $column);
}
- $this->columnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
+ $this->effectiveColumnWidths[$column] = max($lengths) + strlen($this->style->getCellRowContentFormat()) - 2;
}
}
*/
private function getCellWidth(array $row, $column)
{
+ $cellWidth = 0;
+
if (isset($row[$column])) {
$cell = $row[$column];
$cellWidth = Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell);
-
- return $cellWidth;
}
- return 0;
+ $columnWidth = isset($this->columnWidths[$column]) ? $this->columnWidths[$column] : 0;
+
+ return max($cellWidth, $columnWidth);
}
/**
*/
private function cleanup()
{
- $this->columnWidths = array();
+ $this->effectiveColumnWidths = array();
$this->numberOfColumns = null;
}