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

Update CliTable.php #7

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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