Skip to content

Commit

Permalink
Add option to strip empty columns from rendered table
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Apr 23, 2021
1 parent ef92f2a commit bf5d8c2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/TableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ class TableRenderer extends Hardcoded implements Renderer
private $headRowDelimiter;
private $headColDelimiter;
private $outlineVertical;
private $stripEmptyCols = false;

public function __construct(\Iterator $rows)
{
$this->rowsIterator = $rows;
}

public function stripEmptyColumns()
{
$this->stripEmptyCols = true;
return $this;
}

public function setColDelimiter($delimiter = ' ')
{
$this->colDelimiter = $delimiter;
Expand Down Expand Up @@ -75,6 +82,7 @@ public function setOutlineVertical($outlineVertical)

private $lines = array();
private $length = array();
private $maxValueLength = array();
private $keys = array();
private $rows = array();
private $rowDelimiterLine;
Expand All @@ -92,6 +100,9 @@ private function findLines()
if (!isset($this->length[$key]) || $this->length[$key] < $stringLength) {
$this->length[$key] = $stringLength;
}
if ($rowIndex > 0 && (!isset($this->maxValueLength[$key]) || $this->maxValueLength[$key] < $stringLength)) {
$this->maxValueLength[$key] = $stringLength;
}
$this->lines [$rowIndex][$lineIndex][$key] = $line;
}
}
Expand Down Expand Up @@ -139,6 +150,10 @@ protected function buildLine($row, $pad = ' ')
{
$line = '';
foreach ($this->length as $key => $maxLength) {
if ($this->stripEmptyCols && empty($this->maxValueLength[$key])) {
continue;
}

/** @var \Yaoi\Cli\View\Text $value */
$value = isset($row[$key]) ? $row[$key] : null;

Expand Down

0 comments on commit bf5d8c2

Please sign in to comment.