Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

allow customization of all three border characters #4

Merged
merged 3 commits into from

2 participants

@cweiske
Owner

This patch allows you to define your own border styles by giving you control over all characters used in border generation.

It keeps full BC. You should merge PR #3 before. Also logged as https://pear.php.net/bugs/bug.php?id=20182

@yunosh yunosh merged commit d22b17a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
85 Table.php
@@ -142,11 +142,19 @@ class Console_Table
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.
@@ -174,7 +182,7 @@ function Console_Table($align = CONSOLE_TABLE_ALIGN_LEFT,
$charset = null, $color = false)
{
$this->_defaultAlign = $align;
- $this->_border = $border;
+ $this->setBorder($border);
$this->_padding = $padding;
$this->_ansiColor = $color;
if ($this->_ansiColor) {
@@ -250,6 +258,41 @@ function setCharset($charset)
}
/**
+ * 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.
*
* @param integer $col_id The column number.
@@ -564,9 +607,7 @@ function _buildTable()
return '';
}
- $rule = $this->_border == CONSOLE_TABLE_BORDER_ASCII
- ? '|'
- : $this->_border;
+ $vert = $this->_border['vert'];
$separator = $this->_getSeparator();
$return = array();
@@ -583,9 +624,9 @@ function _buildTable()
}
if ($this->_data[$i] !== CONSOLE_TABLE_HORIZONTAL_RULE) {
- $row_begin = $rule . str_repeat(' ', $this->_padding);
- $row_end = str_repeat(' ', $this->_padding) . $rule;
- $implode_char = str_repeat(' ', $this->_padding) . $rule
+ $row_begin = $vert . str_repeat(' ', $this->_padding);
+ $row_end = str_repeat(' ', $this->_padding) . $vert;
+ $implode_char = str_repeat(' ', $this->_padding) . $vert
. str_repeat(' ', $this->_padding);
$return[] = $row_begin
. implode($implode_char, $this->_data[$i]) . $row_end;
@@ -620,12 +661,8 @@ function _getSeparator()
return;
}
- if ($this->_border == CONSOLE_TABLE_BORDER_ASCII) {
- $rule = '-';
- $sect = '+';
- } else {
- $rule = $sect = $this->_border;
- }
+ $rule = $this->_border['rule'];
+ $sect = $this->_border['sect'];
$return = array();
foreach ($this->_cell_lengths as $cl) {
@@ -669,12 +706,10 @@ function _getHeaderLine()
}
}
- $rule = $this->_border == CONSOLE_TABLE_BORDER_ASCII
- ? '|'
- : $this->_border;
- $row_begin = $rule . str_repeat(' ', $this->_padding);
- $row_end = str_repeat(' ', $this->_padding) . $rule;
- $implode_char = str_repeat(' ', $this->_padding) . $rule
+ $vert = $this->_border['vert'];
+ $row_begin = $vert . str_repeat(' ', $this->_padding);
+ $row_end = str_repeat(' ', $this->_padding) . $vert;
+ $implode_char = str_repeat(' ', $this->_padding) . $vert
. str_repeat(' ', $this->_padding);
$separator = $this->_getSeparator();
@@ -719,8 +754,10 @@ function _updateRowsCols($rowdata = null)
}
// Set default column alignments
- for ($i = count($this->_col_align); $i < $this->_max_cols; $i++) {
- $this->_col_align[$i] = $pad;
+ for ($i = 0; $i < $this->_max_cols; $i++) {
+ if (!isset($this->_col_align[$i])) {
+ $this->_col_align[$i] = $pad;
+ }
}
}
View
24 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 |
++----------+------------+
View
27 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 :
+*==========*============*
View
27 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
+======================
View
24 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 .
+.........................
View
21 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
View
23 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 |
++-----+-----+
Something went wrong with that request. Please try again.