From: Stig Bakken Date: Fri, 22 Mar 2002 11:57:45 +0000 (+0000) Subject: * table support working X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~1121 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5cc2e77ca80f04a5c3b51c11bd962aa541f83c5b;p=php * table support working * added bold support for xterm/vt220/vt100 terminals --- diff --git a/pear/PEAR/Frontend/CLI.php b/pear/PEAR/Frontend/CLI.php index 22ff678528..feabe90c91 100644 --- a/pear/PEAR/Frontend/CLI.php +++ b/pear/PEAR/Frontend/CLI.php @@ -1,36 +1,84 @@ | + +----------------------------------------------------------------------+ + + $Id$ +*/ require_once "PEAR.php"; class PEAR_Frontend_CLI extends PEAR { + // {{{ properties + var $omode = 'plain'; var $params = array(); + var $term = array( + 'bold' => '', + 'normal' => '', + ); + + // }}} + + // {{{ constructor function PEAR_Frontend_CLI() { parent::PEAR(); - } - function _PEAR_Frontend_CLI() - { - parent::_PEAR(); - if ($this->omode) { - $this->endOutput(); + 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 } } + // }}} + + // For now, all the display functions print a "| " at the + // beginning of the line. This is just a temporary thing, it + // is for discovering commands that use print instead of + // the UI layer. + + // {{{ displayLine(text) + function displayLine($text) { print "| $text\n"; } + // }}} + // {{{ displayHeading(title) + function displayHeading($title) { - print "| ".strtoupper($title)."\n"; + print "| ".$this->bold($title)."\n"; print "| ".str_repeat("=", strlen($title))."\n"; } + // }}} + // {{{ userDialog(prompt, [type], [default]) + function userDialog($prompt, $type = 'text', $default = '') { if ($type == 'password') { @@ -54,6 +102,9 @@ class PEAR_Frontend_CLI extends PEAR return $line; } + // }}} + // {{{ userConfirm(prompt, [default]) + function userConfirm($prompt, $default = 'yes') { static $positives = array('y', 'yes', 'on', '1'); @@ -78,6 +129,9 @@ class PEAR_Frontend_CLI extends PEAR return false; } + // }}} + // {{{ startTable([params]) + function startTable($params = array()) { $this->omode = 'table'; @@ -87,11 +141,14 @@ class PEAR_Frontend_CLI extends PEAR $this->params = $params; } + // }}} + // {{{ tableRow(columns, [rowparams], [colparams]) + function tableRow($columns, $rowparams = array(), $colparams = array()) { for ($i = 0; $i < sizeof($columns); $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]); } @@ -109,6 +166,9 @@ class PEAR_Frontend_CLI extends PEAR $this->params['table_data'][] = $new_row; } + // }}} + // {{{ endTable() + function endTable() { $this->omode = ''; @@ -125,30 +185,72 @@ class PEAR_Frontend_CLI extends PEAR } } } - if (empty($params['border'])) { - $cellstart = " "; - $rowend = "\n"; + if (empty($border)) { + $cellstart = ' '; + $cellend = ' '; + $rowend = ''; $padrowend = false; + $borderline = ''; } else { - $cellstart = "| "; - $rowend = " |\n"; + $cellstart = '| '; + $cellend = ' '; + $rowend = '|'; $padrowend = true; - $borderline = "+"; + $borderline = '+'; 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++) { extract($table_data[$i]); + $rowtext = ''; for ($c = 0; $c < sizeof($data); $c++) { + if (isset($colparams[$c])) { + $attribs = array_merge($rowparams, $colparams); + } else { + $attribs = $rowparams; + } $w = $width[$c]; $l = strlen($data[$c]); + $cell = $data[$c]; 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']; + } + + // }}} } ?> \ No newline at end of file