Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from conrad10781/master
Browse files Browse the repository at this point in the history
Update CliTable.php
  • Loading branch information
jc21 authored Sep 3, 2023
2 parents a9ff1fc + 9b68c09 commit 0398162
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions src/jc21/CliTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class CliTable {
**/
protected $useColors = true;

/**
* Center content?
*
* @var bool
* @access protected
*
**/
protected $centerContent = true;

/**
* Table Border Color
*
Expand Down Expand Up @@ -119,9 +128,10 @@ class CliTable {
* @param string $itemName
* @param bool $useColors
*/
public function __construct($itemName = 'Row', $useColors = true) {
public function __construct($itemName = 'Row', $useColors = true, $centerContent = false) {
$this->setItemName($itemName);
$this->setUseColors($useColors);
$this->setCenterContent($centerContent);
$this->defineColors();
}

Expand All @@ -138,6 +148,18 @@ public function setUseColors($bool) {
}


/**
* setCenterContent
*
* @access public
* @param bool $bool
* @return void
*/
public function setCenterContent($bool) {
$this->centerContent = (bool) $bool;
}


/**
* getUseColors
*
Expand All @@ -149,6 +171,17 @@ public function getUseColors() {
}


/**
* getCenterContent
*
* @access public
* @return bool
*/
public function getCenterContent() {
return $this->centerContent;
}


/**
* setTableColor
*
Expand Down Expand Up @@ -351,7 +384,8 @@ public function get() {
if (!isset($columnLengths[$key])) {
$columnLengths[$key] = 0;
}
$columnLengths[$key] = max($columnLengths[$key], strlen($value));
$c = chr(27);
$columnLengths[$key] = max($columnLengths[$key], strlen(preg_replace("/({$c}\[(.*?)m)/s", '', $value)));
}
$rowCount++;
}
Expand All @@ -364,18 +398,32 @@ public function get() {

$response = '';

$screenWidth = trim(exec("tput cols"));

// Idea here is we're column the accumulated length of the data
// Then adding the quantity of column lengths to accommodate for the extra characters
// for when vertical pipes are placed between each column
$dataWidth = mb_strlen($this->getTableTop($columnLengths)) + count($columnLengths);

$spacing = '';

// Only try and center when content is less than available space
if ($this->getCenterContent() && (($dataWidth/2) < $screenWidth)) {
$spacing = str_repeat(' ', ($screenWidth-($dataWidth/2))/2);
}

// Now draw the table!
$response .= $this->getTableTop($columnLengths);
$response .= $spacing . $this->getTableTop($columnLengths);
if ($this->getShowHeaders()) {
$response .= $this->getFormattedRow($headerData, $columnLengths, true);
$response .= $this->getTableSeperator($columnLengths);
$response .= $spacing . $this->getFormattedRow($headerData, $columnLengths, true);
$response .= $spacing . $this->getTableSeperator($columnLengths);
}

foreach ($cellData as $row) {
$response .= $this->getFormattedRow($row, $columnLengths);
$response .= $spacing . $this->getFormattedRow($row, $columnLengths);
}

$response .= $this->getTableBottom($columnLengths);
$response .= $spacing . $this->getTableBottom($columnLengths);

return $response;
}
Expand All @@ -400,7 +448,8 @@ protected function getFormattedRow($rowData, $columnLengths, $header = false) {
$color = $this->fields[$key]['color'];
}

$fieldLength = mb_strwidth($field) + 1;
$c = chr(27);
$fieldLength = mb_strwidth(preg_replace("/({$c}\[(.*?)m)/", '', $field)) + 1;
$field = ' '.($this->getUseColors() ? $this->getColorFromName($color) : '').$field;
$response .= $field;

Expand Down

0 comments on commit 0398162

Please sign in to comment.