* table support working

* added bold support for xterm/vt220/vt100 terminals
This commit is contained in:
Stig Bakken 2002-03-22 11:57:45 +00:00
parent 8ce0ceb372
commit 5cc2e77ca8

View file

@ -1,36 +1,84 @@
<?php <?php
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2002 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Stig Sæther Bakken <ssb@fast.no> |
+----------------------------------------------------------------------+
$Id$
*/
require_once "PEAR.php"; require_once "PEAR.php";
class PEAR_Frontend_CLI extends PEAR class PEAR_Frontend_CLI extends PEAR
{ {
// {{{ properties
var $omode = 'plain'; var $omode = 'plain';
var $params = array(); var $params = array();
var $term = array(
'bold' => '',
'normal' => '',
);
// }}}
// {{{ constructor
function PEAR_Frontend_CLI() function PEAR_Frontend_CLI()
{ {
parent::PEAR(); parent::PEAR();
if (isset($_ENV['TERM'])) {
if (preg_match('/^(xterm|vt220)/', $_ENV['TERM'])) {
$this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109);
$this->term['normal']=sprintf("%c%c%c", 27, 91, 109);
} elseif (preg_match('/^vt100/', $_ENV['TERM'])) {
$this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0);
$this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0);
}
} elseif (OS_WINDOWS) {
// XXX add ANSI codes here
}
} }
function _PEAR_Frontend_CLI() // }}}
{
parent::_PEAR(); // For now, all the display functions print a "| " at the
if ($this->omode) { // beginning of the line. This is just a temporary thing, it
$this->endOutput(); // is for discovering commands that use print instead of
} // the UI layer.
}
// {{{ displayLine(text)
function displayLine($text) function displayLine($text)
{ {
print "| $text\n"; print "| $text\n";
} }
// }}}
// {{{ displayHeading(title)
function displayHeading($title) function displayHeading($title)
{ {
print "| ".strtoupper($title)."\n"; print "| ".$this->bold($title)."\n";
print "| ".str_repeat("=", strlen($title))."\n"; print "| ".str_repeat("=", strlen($title))."\n";
} }
// }}}
// {{{ userDialog(prompt, [type], [default])
function userDialog($prompt, $type = 'text', $default = '') function userDialog($prompt, $type = 'text', $default = '')
{ {
if ($type == 'password') { if ($type == 'password') {
@ -54,6 +102,9 @@ class PEAR_Frontend_CLI extends PEAR
return $line; return $line;
} }
// }}}
// {{{ userConfirm(prompt, [default])
function userConfirm($prompt, $default = 'yes') function userConfirm($prompt, $default = 'yes')
{ {
static $positives = array('y', 'yes', 'on', '1'); static $positives = array('y', 'yes', 'on', '1');
@ -78,6 +129,9 @@ class PEAR_Frontend_CLI extends PEAR
return false; return false;
} }
// }}}
// {{{ startTable([params])
function startTable($params = array()) function startTable($params = array())
{ {
$this->omode = 'table'; $this->omode = 'table';
@ -87,11 +141,14 @@ class PEAR_Frontend_CLI extends PEAR
$this->params = $params; $this->params = $params;
} }
// }}}
// {{{ tableRow(columns, [rowparams], [colparams])
function tableRow($columns, $rowparams = array(), $colparams = array()) function tableRow($columns, $rowparams = array(), $colparams = array())
{ {
for ($i = 0; $i < sizeof($columns); $i++) { for ($i = 0; $i < sizeof($columns); $i++) {
if (!isset($this->params['widest'][$i]) || if (!isset($this->params['widest'][$i]) ||
strlen($columns[$i] > $this->params['widest'][$i])) strlen($columns[$i]) > $this->params['widest'][$i])
{ {
$this->params['widest'][$i] = strlen($columns[$i]); $this->params['widest'][$i] = strlen($columns[$i]);
} }
@ -109,6 +166,9 @@ class PEAR_Frontend_CLI extends PEAR
$this->params['table_data'][] = $new_row; $this->params['table_data'][] = $new_row;
} }
// }}}
// {{{ endTable()
function endTable() function endTable()
{ {
$this->omode = ''; $this->omode = '';
@ -125,30 +185,72 @@ class PEAR_Frontend_CLI extends PEAR
} }
} }
} }
if (empty($params['border'])) { if (empty($border)) {
$cellstart = " "; $cellstart = ' ';
$rowend = "\n"; $cellend = ' ';
$rowend = '';
$padrowend = false; $padrowend = false;
$borderline = '';
} else { } else {
$cellstart = "| "; $cellstart = '| ';
$rowend = " |\n"; $cellend = ' ';
$rowend = '|';
$padrowend = true; $padrowend = true;
$borderline = "+"; $borderline = '+';
foreach ($width as $w) { foreach ($width as $w) {
$borderline .= str_repeat('-', $w + 2); $borderline .= str_repeat('-', $w + strlen($cellstart) + strlen($cellend) - 1);
$borderline .= '+';
} }
} }
if ($borderline) {
$this->displayLine($borderline);
}
for ($i = 0; $i < sizeof($table_data); $i++) { for ($i = 0; $i < sizeof($table_data); $i++) {
extract($table_data[$i]); extract($table_data[$i]);
$rowtext = '';
for ($c = 0; $c < sizeof($data); $c++) { for ($c = 0; $c < sizeof($data); $c++) {
if (isset($colparams[$c])) {
$attribs = array_merge($rowparams, $colparams);
} else {
$attribs = $rowparams;
}
$w = $width[$c]; $w = $width[$c];
$l = strlen($data[$c]); $l = strlen($data[$c]);
$cell = $data[$c];
if ($l > $w) { if ($l > $w) {
print $cell = substr($cell, 0, $w);
}
if (isset($attribs['bold'])) {
$cell = $this->bold($cell);
}
if ($l < $w) {
// not using str_pad here because we may
// add bold escape characters to $cell
$cell .= str_repeat(' ', $w - $l);
}
$rowtext .= $cellstart . $cell . $cellend;
}
$rowtext .= $rowend;
$this->displayLine($rowtext);
}
if ($borderline) {
$this->displayLine($borderline);
} }
} }
// }}}
// {{{ bold($text)
function bold($text)
{
if (empty($this->term['bold'])) {
return strtoupper($text);
} }
return $this->term['bold'] . $text . $this->term['normal'];
} }
// }}}
} }
?> ?>