Patch for Console_Table to enable alignment of columns
| From: | Michael Richter | Date: | Sat, 16 Jul 2005 13:04:16 +0000 |
| Subject: | Patch for Console_Table to enable alignment of columns | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-38670@lists.php.net to get a copy of this message | ||
Hi,
i needed alignment support in Console_Table so I wrote a patch:
--------------------------------------------------------------------------
--- Table.php 2004-02-11 14:13:44.000000000 +0100
+++ Table.php 2005-07-16 14:49:27.351917464 +0200
@@ -84,6 +84,12 @@
var $_padding;
/**
+ * Alignment of the columns
+ * @var array
+ */
+ var $_col_align;
+
+ /**
* Constructor
*/
function Console_Table()
@@ -94,6 +100,7 @@
$this->_max_cols = 0; $this->_max_rows = 0; $this->_padding = 1; + $this->_col_align = array();} /** @@ -174,6 +181,25 @@ } /** + * Sets the alignment for the columns + * @param integer $col_id Column ID + * @param integer $align Alignment to set for this column
+ * -1 = left, 0 = center, 1 = right+ */ + function setAlign($col_id, $align = -1) + { + // -1 = left, 0 = center, 1 = right + if ($align == 0) { + $pad = STR_PAD_BOTH; + } elseif ($align > 0) { + $pad = STR_PAD_LEFT; + } else { + $pad = STR_PAD_RIGHT; + } + $this->_col_align[$col_id] = $pad; + } + + /**
* Returns the table in wonderful* ASCII art
*/@@ -214,7 +240,7 @@ for ($i=0; $i<count($rows); $i++) { for ($j=0; $j<count($rows[$i]); $j++) { if (strlen($rows[$i][$j]) < $this->_cell_lengths[$j]) { - $rows[$i][$j] = str_pad($rows[$i][$j], $this->_cell_lengths[$j], ' '); + $rows[$i][$j] = str_pad($rows[$i][$j], $this->_cell_lengths[$j], ' ', $this->_col_align[$j]); } } @@ -268,7 +294,7 @@ for ($i=0; $i<count($this->_headers); $i++) { if (strlen($this->_headers[$i]) < $this->_cell_lengths[$i]) { - $this->_headers[$i] = str_pad($this->_headers[$i], $this->_cell_lengths[$i], ' '); + $this->_headers[$i] = str_pad($this->_headers[$i], $this->_cell_lengths[$i], ' ', $this->_col_align[$i]); } } @@ -293,6 +319,11 @@ // Update max rows ksort($this->_data); $this->_max_rows = end(array_keys($this->_data)) + 1; + + // Set default column alignments + for ($i = count($this->_col_align); $i < $this->_max_cols; $i++) { + $this->_col_align[$i] = STR_PAD_RIGHT; + } } /** --------------------------------------------------------------------------