Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow customization of all three border characters #4

Merged
merged 3 commits into from Feb 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 61 additions & 24 deletions Table.php
Expand Up @@ -142,11 +142,19 @@ class Console_Table
var $_charset = 'utf-8'; var $_charset = 'utf-8';


/** /**
* Border character. * Border characters.
* Allowed keys:
* - sect - intersection ("+")
* - rule - ruler, horizontal character ("-")
* - vert - vertical character ("|")
* *
* @var string * @var array
*/ */
var $_border = CONSOLE_TABLE_BORDER_ASCII; var $_border = array(
'sect' => '+',
'rule' => '-',
'vert' => '|',
);


/** /**
* Whether the data has ANSI colors. * Whether the data has ANSI colors.
Expand Down Expand Up @@ -174,7 +182,7 @@ function Console_Table($align = CONSOLE_TABLE_ALIGN_LEFT,
$charset = null, $color = false) $charset = null, $color = false)
{ {
$this->_defaultAlign = $align; $this->_defaultAlign = $align;
$this->_border = $border; $this->setBorder($border);
$this->_padding = $padding; $this->_padding = $padding;
$this->_ansiColor = $color; $this->_ansiColor = $color;
if ($this->_ansiColor) { if ($this->_ansiColor) {
Expand Down Expand Up @@ -249,6 +257,41 @@ function setCharset($charset)
setlocale(LC_CTYPE, $locale); setlocale(LC_CTYPE, $locale);
} }


/**
* Set the table border settings
*
* Border definition modes:
* - CONSOLE_TABLE_BORDER_ASCII: Default border with +, - and |
* - array with keys "sect" (intersection), "rule" (horizontal) and "vert"
* (vertical character)
* - single character string that sets all three of the array keys
*
* @param mixed $border Border definition
*
* @return void
* @see $_border
*/
function setBorder($border)
{
if ($border === CONSOLE_TABLE_BORDER_ASCII) {
$sect = '+';
$rule = '-';
$vert = '|';
} else if (is_string($border)) {
$sect = $rule = $vert = $border;
} else if ($border == '') {
$sect = $rule = $vert = '';
} else {
extract($border);
}

$this->_border = array(
'sect' => $sect,
'rule' => $rule,
'vert' => $vert,
);
}

/** /**
* Sets the alignment for the columns. * Sets the alignment for the columns.
* *
Expand Down Expand Up @@ -564,9 +607,7 @@ function _buildTable()
return ''; return '';
} }


$rule = $this->_border == CONSOLE_TABLE_BORDER_ASCII $vert = $this->_border['vert'];
? '|'
: $this->_border;
$separator = $this->_getSeparator(); $separator = $this->_getSeparator();


$return = array(); $return = array();
Expand All @@ -583,9 +624,9 @@ function _buildTable()
} }


if ($this->_data[$i] !== CONSOLE_TABLE_HORIZONTAL_RULE) { if ($this->_data[$i] !== CONSOLE_TABLE_HORIZONTAL_RULE) {
$row_begin = $rule . str_repeat(' ', $this->_padding); $row_begin = $vert . str_repeat(' ', $this->_padding);
$row_end = str_repeat(' ', $this->_padding) . $rule; $row_end = str_repeat(' ', $this->_padding) . $vert;
$implode_char = str_repeat(' ', $this->_padding) . $rule $implode_char = str_repeat(' ', $this->_padding) . $vert
. str_repeat(' ', $this->_padding); . str_repeat(' ', $this->_padding);
$return[] = $row_begin $return[] = $row_begin
. implode($implode_char, $this->_data[$i]) . $row_end; . implode($implode_char, $this->_data[$i]) . $row_end;
Expand Down Expand Up @@ -620,12 +661,8 @@ function _getSeparator()
return; return;
} }


if ($this->_border == CONSOLE_TABLE_BORDER_ASCII) { $rule = $this->_border['rule'];
$rule = '-'; $sect = $this->_border['sect'];
$sect = '+';
} else {
$rule = $sect = $this->_border;
}


$return = array(); $return = array();
foreach ($this->_cell_lengths as $cl) { foreach ($this->_cell_lengths as $cl) {
Expand Down Expand Up @@ -669,12 +706,10 @@ function _getHeaderLine()
} }
} }


$rule = $this->_border == CONSOLE_TABLE_BORDER_ASCII $vert = $this->_border['vert'];
? '|' $row_begin = $vert . str_repeat(' ', $this->_padding);
: $this->_border; $row_end = str_repeat(' ', $this->_padding) . $vert;
$row_begin = $rule . str_repeat(' ', $this->_padding); $implode_char = str_repeat(' ', $this->_padding) . $vert
$row_end = str_repeat(' ', $this->_padding) . $rule;
$implode_char = str_repeat(' ', $this->_padding) . $rule
. str_repeat(' ', $this->_padding); . str_repeat(' ', $this->_padding);


$separator = $this->_getSeparator(); $separator = $this->_getSeparator();
Expand Down Expand Up @@ -719,8 +754,10 @@ function _updateRowsCols($rowdata = null)
} }


// Set default column alignments // Set default column alignments
for ($i = count($this->_col_align); $i < $this->_max_cols; $i++) { for ($i = 0; $i < $this->_max_cols; $i++) {
$this->_col_align[$i] = $pad; if (!isset($this->_col_align[$i])) {
$this->_col_align[$i] = $pad;
}
} }
} }


Expand Down
24 changes: 24 additions & 0 deletions tests/border-ascii.phpt
@@ -0,0 +1,24 @@
--TEST--
Border: default ASCII mode
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, CONSOLE_TABLE_BORDER_ASCII);
$table->setHeaders(array('City', 'Mayor'));
$table->addRow(array('Leipzig', 'Major Tom'));
$table->addRow(array('New York', 'Towerhouse'));

echo $table->getTable();
?>
--EXPECT--
+----------+------------+
| City | Mayor |
+----------+------------+
| Leipzig | Major Tom |
| New York | Towerhouse |
+----------+------------+
27 changes: 27 additions & 0 deletions tests/border-custom.phpt
@@ -0,0 +1,27 @@
--TEST--
Border: new custom mode
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table(
CONSOLE_TABLE_ALIGN_LEFT,
array('rule' => '=', 'vert' => ':', 'sect' => '*')
);
$table->setHeaders(array('City', 'Mayor'));
$table->addRow(array('Leipzig', 'Major Tom'));
$table->addRow(array('New York', 'Towerhouse'));

echo $table->getTable();
?>
--EXPECT--
*==========*============*
: City : Mayor :
*==========*============*
: Leipzig : Major Tom :
: New York : Towerhouse :
*==========*============*
27 changes: 27 additions & 0 deletions tests/border-custom2.phpt
@@ -0,0 +1,27 @@
--TEST--
Border: new custom mode, alternative style
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table(
CONSOLE_TABLE_ALIGN_LEFT,
array('rule' => '=', 'vert' => '', 'sect' => '')
);
$table->setHeaders(array('City', 'Mayor'));
$table->addRow(array('Leipzig', 'Major Tom'));
$table->addRow(array('New York', 'Towerhouse'));

echo $table->getTable();
?>
--EXPECT--
======================
City Mayor
======================
Leipzig Major Tom
New York Towerhouse
======================
24 changes: 24 additions & 0 deletions tests/border-dot.phpt
@@ -0,0 +1,24 @@
--TEST--
Border: custom border character
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, '.');
$table->setHeaders(array('City', 'Mayor'));
$table->addRow(array('Leipzig', 'Major Tom'));
$table->addRow(array('New York', 'Towerhouse'));

echo $table->getTable();
?>
--EXPECT--
.........................
. City . Mayor .
.........................
. Leipzig . Major Tom .
. New York . Towerhouse .
.........................
21 changes: 21 additions & 0 deletions tests/border-empty.phpt
@@ -0,0 +1,21 @@
--TEST--
Border: empty character
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table(CONSOLE_TABLE_ALIGN_LEFT, '');
$table->setHeaders(array('City', 'Mayor'));
$table->addRow(array('Leipzig', 'Major Tom'));
$table->addRow(array('New York', 'Towerhouse'));

echo $table->getTable() . "\n";
?>
--EXPECT--
City Mayor
Leipzig Major Tom
New York Towerhouse
23 changes: 23 additions & 0 deletions tests/bug20181.phpt
@@ -0,0 +1,23 @@
--TEST--
Bug #20181: setAlign() on non-zero column
--FILE--
<?php
error_reporting(E_ALL | E_NOTICE);
if (file_exists(dirname(__FILE__) . '/../Table.php')) {
require_once dirname(__FILE__) . '/../Table.php';
} else {
require_once 'Console/Table.php';
}
$table = new Console_Table();
$table->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
$table->setHeaders(array('f', 'bar'));
$table->addRow(array('baz', 'b'));

echo $table->getTable();
?>
--EXPECT--
+-----+-----+
| f | bar |
+-----+-----+
| baz | b |
+-----+-----+