cvs: pear /Console_Table Table.php

From: Date: Sat, 16 Jul 2005 16:54:44 +0000
Subject: cvs: pear /Console_Table Table.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-33085@lists.php.net to get a copy of this message
richard Sat Jul 16 12:54:44 2005 EDT Modified files: /pear/Console_Table Table.php Log: Add support for column alignment Patch from Michael Richter

http://cvs.php.net/diff.php/pear/Console_Table/Table.php?r1=1.7&r2=1.8&ty=u Index: pear/Console_Table/Table.php diff -u pear/Console_Table/Table.php:1.7 pear/Console_Table/Table.php:1.8 --- pear/Console_Table/Table.php:1.7 Mon Jan 24 14:01:58 2005 +++ pear/Console_Table/Table.php Sat Jul 16 12:54:44 2005 @@ -1,43 +1,46 @@ <?php -// +-----------------------------------------------------------------------+ -// | Copyright (c) 2002-2003 Richard Heyes | -// | All rights reserved. | -// | | -// | Redistribution and use in source and binary forms, with or without | -// | modification, are permitted provided that the following conditions | -// | are met: | -// | | -// | o Redistributions of source code must retain the above copyright | -// | notice, this list of conditions and the following disclaimer. | -// | o Redistributions in binary form must reproduce the above copyright | -// | notice, this list of conditions and the following disclaimer in the | -// | documentation and/or other materials provided with the distribution.| -// | o The names of the authors may not be used to endorse or promote | -// | products derived from this software without specific prior written | -// | permission. | -// | | -// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | -// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | -// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | -// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | -// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | -// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | -// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | -// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | -// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | -// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | -// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | -// | | -// +-----------------------------------------------------------------------+ -// | Author: Richard Heyes <richard@phpguru.org> | -// +-----------------------------------------------------------------------+ -// -// $Id: Table.php,v 1.7 2005/01/24 19:01:58 richard Exp $ -// +// +-----------------------------------------------------------------------+ +// | Copyright (c) 2002-2003 Richard Heyes | +// | All rights reserved. | +// | | +// | Redistribution and use in source and binary forms, with or without | +// | modification, are permitted provided that the following conditions | +// | are met: | +// | | +// | o Redistributions of source code must retain the above copyright | +// | notice, this list of conditions and the following disclaimer. | +// | o Redistributions in binary form must reproduce the above copyright | +// | notice, this list of conditions and the following disclaimer in the | +// | documentation and/or other materials provided with the distribution.| +// | o The names of the authors may not be used to endorse or promote | +// | products derived from this software without specific prior written | +// | permission. | +// | | +// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | +// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | +// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | +// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | +// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | +// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | +// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | +// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | +// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | +// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | +// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | +// | | +// +-----------------------------------------------------------------------+ +// | Author: Richard Heyes <richard@phpguru.org> | +// +-----------------------------------------------------------------------+ +// +// $Id: Table.php,v 1.8 2005/07/16 16:54:44 richard Exp $ +// // Utility for printing tables from cmdline scripts // define('CONSOLE_TABLE_HORIZONTAL_RULE', 1); +define('CONSOLE_TABLE_ALIGN_LEFT', -1); +define('CONSOLE_TABLE_ALIGN_CENTER', 0); +define('CONSOLE_TABLE_ALIGN_RIGHT', 1); class Console_Table { @@ -47,24 +50,28 @@ */ var $_headers; + /** * The data of the table * @var array */ var $_data; + /** * The max number of columns in a row * @var integer */ var $_max_cols; + /** * The max number of rows in the table * @var integer */ var $_max_rows; + /** * Lengths of the columns, calculated * when rows are added to the table. @@ -72,35 +79,56 @@ */ var $_cell_lengths; + /** * Some options that configure various * things * @var array; */ var $_options; - + + /** * How many spaces to use to pad the table * @var integer */ var $_padding; - + + /** * Column filters * @var array */ var $_filters; - + + /** * Columns to calculate totals for * @var array */ var $_calculateTotals; + + /** + * Alignment of the columns + * @var array + */ + var $_col_align; + + + /** + * Default alignment of columns + * @var int + */ + var $_defaultAlign; + + /** * Constructor + * + * @param int $align Default alignment */ - function Console_Table() + function Console_Table($align = CONSOLE_TABLE_ALIGN_LEFT) { $this->_headers = array(); $this->_data = array(); @@ -109,12 +137,15 @@ $this->_max_rows = 0; $this->_padding = 1; $this->_filters = array(); + $this->_col_align = array(); + $this->_defaultAlign = $align; } - + + /** * Converts an array to a table. Must be a two dimensional array. * (Static) - * + * * @param array $headers Headers for the table * @param array $data Data for the table * @param bool $returnObject Whether to return the Console_Table object (default: No) @@ -127,20 +158,21 @@ $table = &new Console_Table(); $table->setHeaders($headers); - + foreach ($data as $row) { $table->addRow(array_values($row)); } return $returnObject ? $table : $table->getTable(); } - + + /** * Adds a filter to the object. Filters are standard php callbacks which are * run on the data before table generation is performed. Filters are applied * in the order they're added. the callback function must accept a single * argument, which is a single table cell. - * + * * @param int $col Column to apply filter to * @param callback $callback PHP callback to apply */ @@ -148,11 +180,38 @@ { $this->_filters[] = array($col, &$callback); } - + + + /** + * 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 + * OR use the constants: + * CONSOLE_TABLE_ALIGN_LEFT + * CONSOLE_TABLE_ALIGN_CENTER + * CONSOLE_TABLE_ALIGN_RIGHT + */ + function setAlign($col_id, $align = CONSOLE_TABLE_ALIGN_LEFT) + { + // -1 = left, 0 = center, 1 = right + if ($align == CONSOLE_TABLE_ALIGN_CENTER) { + $pad = STR_PAD_BOTH; + + } elseif ($align == CONSOLE_TABLE_ALIGN_RIGHT) { + $pad = STR_PAD_LEFT; + + } else { + $pad = STR_PAD_RIGHT; + } + $this->_col_align[$col_id] = $pad; + } + + /** * Specifies which columns are to have totals calculated for them and * added as a new row at the bottom. - * + * * @param array $cols Array of column IDs (0 is first column, 1 is second etc) */ function calculateTotalsFor($cols) @@ -160,6 +219,7 @@ $this->_calculateTotals = $cols; } + /** * Sets the headers for the columns * @@ -171,6 +231,7 @@ $this->_updateRowsCols($headers); } + /** * Adds a row to the table * @@ -183,7 +244,8 @@ $this->_updateRowsCols($row); } - + + /** * Inserts a row after a given row number in the table. If $row_id * is not given it will prepend the row. @@ -197,7 +259,8 @@ $this->_updateRowsCols($row); } - + + /** * Adds a column to the table * @@ -214,7 +277,8 @@ $this->_updateRowsCols(); $this->_max_cols = max($this->_max_cols, $col_id + 1); } - + + /** * Adds data to the table. Argument should be * a two dimensional array containing the data @@ -237,6 +301,7 @@ } } + /** * Adds a Horizontal Seperator to the table */ @@ -258,14 +323,15 @@ return $this->_buildTable(); } - + + /** * Calculates totals for columns */ function _calculateTotals() { if (!empty($this->_calculateTotals)) { - + $this->addSeparator(); $totals = array(); @@ -277,12 +343,13 @@ } } } - + $this->_data[] = $totals; $this->_updateRowsCols(); } } + /** * Applies any column filters to the data */ @@ -300,6 +367,7 @@ } } + /** * Ensures column and row counts are correct */ @@ -320,10 +388,11 @@ } } - + ksort($this->_data); } + /** * Builds the table */ @@ -335,10 +404,10 @@ for ($i=0; $i<count($rows); $i++) { for ($j=0; $j<count($rows[$i]); $j++) { if ($rows[$i] != CONSOLE_TABLE_HORIZONTAL_RULE AND 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]); } } - + if ($rows[$i] != CONSOLE_TABLE_HORIZONTAL_RULE) { $row_begin = '|' . str_repeat(' ', $this->_padding); $row_end = str_repeat(' ', $this->_padding) . '|'; @@ -358,7 +427,8 @@ return $return; } - + + /** * Creates a horizontal separator for header * separation and table start/end etc @@ -375,10 +445,11 @@ $implode_char = str_repeat('-', $this->_padding) . '+' . str_repeat('-', $this->_padding); $return = $row_begin . implode($implode_char, $return) . $row_end; - + return $return; } + /** * Returns header line for the table */ @@ -393,10 +464,10 @@ 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]); } } - + $row_begin = '|' . str_repeat(' ', $this->_padding); $row_end = str_repeat(' ', $this->_padding) . '|'; $implode_char = str_repeat(' ', $this->_padding) . '|' . str_repeat(' ', $this->_padding); @@ -407,6 +478,7 @@ return implode("\r\n", $return); } + /** * Update max cols/rows */ @@ -418,8 +490,22 @@ // Update max rows ksort($this->_data); $this->_max_rows = end(array_keys($this->_data)) + 1; + + switch ($this->_defaultAlign) { + case CONSOLE_TABLE_ALIGN_CENTER: $pad = STR_PAD_BOTH; break; + case CONSOLE_TABLE_ALIGN_RIGHT: $pad = STR_PAD_LEFT; break; + default: + case CONSOLE_TABLE_ALIGN_LEFT: $pad = STR_PAD_RIGHT; break; + } + + // Set default column alignments + for ($i = count($this->_col_align); $i<$this->_max_cols; $i++) { + $this->_col_align[$i] = $pad; + } + } + /** * This function given a row of data will * calculate the max length for each column
« previous php.pear.cvs (#33085) next »