Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bpanahij
Created July 10, 2013 00:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bpanahij/5962568 to your computer and use it in GitHub Desktop.
Save bpanahij/5962568 to your computer and use it in GitHub Desktop.
Copied from http://tonylandis.com/php/php-text-tables-class/ and modified to add color.
<?php
/**
* See for Original: http://tonylandis.com/php/php-text-tables-class/
* Modified by Brian Johnson on 2013-07-09
*/
class ArrayToColorTextTable
{
/**
* @var array The array for processing
*/
private $rows;
/**
* @var int The column width settings
*/
private $cs = array();
/**
* @var int The Row lines settings
*/
private $rs = array();
/**
* @var int The Column index of keys
*/
private $keys = array();
/**
* @var int Max Column Height (returns)
*/
private $mH = 2;
/**
* @var int Max Row Width (chars)
*/
private $mW = 30;
private $head = false;
private $pcen = "+";
private $prow = "-";
private $pcol = "|";
/** Prepare array into textual format
*
* @param array $rows The input array
* @param bool $head Show heading
* @param int $maxWidth Max Column Height (returns)
* @param int $maxHeight Max Row Width (chars)
*/
public function __construct($rows, $totals)
{
if (is_array($totals))
{
$rows = $this->addTotalRow($rows, $totals);
}
$this->rows =& $rows;
$this->cs = array();
$this->rs = array();
if (!$xc = count($this->rows))
{
return false;
}
$this->keys = array_keys($this->rows[0]);
$columns = count($this->keys);
for ($x = 0; $x < $xc; $x++)
{
for ($y = 0; $y < $columns; $y++)
{
$this->setMax($x, $y, $this->rows[$x][$this->keys[$y]]);
}
}
}
/**
* Show the headers using the key values of the array for the titles
*
* @param bool $bool
*/
public function showHeaders($bool)
{
if ($bool)
{
$this->setHeading();
}
}
/**
* Set the maximum width (number of characters) per column before truncating
*
* @param int $maxWidth
*/
public function setMaxWidth($maxWidth)
{
$this->mW = (int)$maxWidth;
}
/**
* Set the maximum height (number of lines) per row before truncating
*
* @param int $maxHeight
*/
public function setMaxHeight($maxHeight)
{
$this->mH = (int)$maxHeight;
}
/**
* Prints the data to a text table
*
* @param bool $return Set to 'true' to return text rather than printing
* @return mixed
*/
public function render($return = false)
{
if ($return)
{
ob_start(null, 0, true);
}
echo chr(27) . '[0;30;47m';
$this->printLine();
$this->printHeading();
$rowCount = count($this->rows);
for ($index = 0; $index < $rowCount; $index++)
{
$this->printRow($index);
}
echo chr(27) . '[0;30;47m';
$this->printLine();
echo chr(27) . "[0m";
if ($return)
{
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}
public static function sumColumns($row, $index, &$totals)
{
foreach ($row as $column => $value)
{
if (is_numeric($totals[$column]))
{
$totals[$column] = number_format($totals[$column] + $value, 2, '.', ',');
}
}
}
private function addTotalRow($rows, $totals)
{
array_walk($rows, array('self', 'sumColumns'), &$totals);
$rows[] = array();
$rows[] = $totals;
return $rows;
}
private function setHeading()
{
$data = array();
foreach ($this->keys as $colKey => $value)
{
$this->setMax(false, $colKey, $value);
$data[$colKey] = strtoupper($value);
}
if (!is_array($data))
{
return false;
}
$this->head = $data;
}
private function printLine($nl = true)
{
print $this->pcen;
foreach ($this->cs as $key => $val)
print $this->prow .
str_pad('', $val, $this->prow, STR_PAD_RIGHT) .
$this->prow .
$this->pcen;
if ($nl)
{
print "\n";
}
}
private function printHeading()
{
if (!is_array($this->head))
{
return false;
}
print $this->pcol;
foreach ($this->cs as $key => $val)
print ' ' .
str_pad($this->head[$key], $val, ' ', STR_PAD_BOTH) .
' ' .
$this->pcol;
print "\n";
$this->printLine();
}
private function printRow($rowKey)
{
if ($this->rows[$rowKey]['color'])
{
echo chr(27) . '[' . $this->rows[$rowKey]['color'] . 'm';
} else {
echo chr(27) . '[0;30;47m';
}
for ($line = 1; $line <= $this->rs[$rowKey]; $line++)
{
print $this->pcol;
for ($colKey = 0; $colKey < count($this->keys); $colKey++)
{
print " ";
print str_pad(substr($this->rows[$rowKey][$this->keys[$colKey]], ($this->mW * ($line - 1)), $this->mW), $this->cs[$colKey], ' ', STR_PAD_RIGHT);
print " " . $this->pcol;
}
print "\n";
if ($this->rows[$rowKey]['color'])
{
echo chr(27) . "[0m";
}
}
}
private function setMax($rowKey, $colKey, &$colVal)
{
$w = mb_strlen($colVal);
$h = 1;
if ($w > $this->mW)
{
$h = ceil($w % $this->mW);
if ($h > $this->mH)
{
$h = $this->mH;
}
$w = $this->mW;
}
if (!isset($this->cs[$colKey]) || $this->cs[$colKey] < $w)
{
$this->cs[$colKey] = $w;
}
if ($rowKey !== false && (!isset($this->rs[$rowKey]) || $this->rs[$rowKey] < $h))
{
$this->rs[$rowKey] = $h;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment