Add table support to outputtxt

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1290 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-05-13 12:00:47 +00:00
parent 4d0e0d7993
commit cfc5e3a43e

View File

@@ -7,6 +7,9 @@ require_once ("output.php");
/** Display in Text the datas provided */
class outputtxt extends output
{
/** Display an array on table format (TRUE) or tree format (FALSE) */
public $table = TRUE;
/** Display in Text the datas provided
@param mixed $data The data to be displayed */
public function out ($data)
@@ -26,7 +29,92 @@ class outputtxt extends output
elseif (is_integer ($data))
echo "$data\n";
elseif (is_array ($data))
{
if ($this->table === true)
{
// TABLE FORMAT
// Look at sizes of each field
$sizes = array ();
foreach ($data as $line)
{
if (! is_array ($line))
{
// 1 dimension array : force in tree format
$this->table = false;
break;
}
foreach ($line as $key=>$val)
{
if (is_array ($val))
{
// 3 dimensions array : force in tree format
$this->table = false;
break 2;
}
if (! isset ($sizes[$key]))
$sizes[$key] = strlen ($val);
else
$sizes[$key] = max ($sizes[$key], strlen ($val));
}
}
if ($this->table === true)
{
// Display the table if it is not a tree format
// Prepare the border
$line = reset ($data);
$border = "";
$border .= "+";
$col = 0;
foreach ($line as $key=>$val)
{
if ($col > 0)
$border .= "+";
$border .= str_repeat ("-", 2+$sizes[$key]);
$col ++;
}
$border .= "+\n";
echo "$border";
echo "| ";
// Display titles
$col = 0;
foreach ($line as $key=>$val)
{
if ($col > 0)
echo " | ";
$start = round (($sizes[$key]-strlen ($key))/2);
echo str_repeat (" ", $start);
echo $key;
echo str_repeat (" ", $sizes[$key]-$start-strlen ($key));
$col ++;
}
echo " |\n";
echo "$border";
// Display datas
foreach ($data as $line)
{
echo "| ";
$col = 0;
foreach ($line as $key=>$val)
{
if ($col > 0)
echo " | ";
echo sprintf ("%".$sizes[$key]."s", $val);
$col ++;
}
echo " |\n";
}
echo "$border";
}
}
if ($this->table === false)
{
// TREE FORMAT
echo substr (print_r ($data, TRUE), 8, -3)."\n";
}
}
elseif ($data === NULL)
echo "NULL\n";
else