cvs: pear-core /PEAR/Frontend CLI.php
| From: | Helgi �ormar �orbj�rnsson | Date: | Sat, 12 Jul 2008 18:59:39 +0000 |
| Subject: | cvs: pear-core /PEAR/Frontend CLI.php | ||
| Groups: | php.pear.cvs php.pear.core | ||
| Request: | Send a blank email to pear-core+get-7643@lists.php.net to get a copy of this message | ||
dufuz Sat Jul 12 18:59:39 2008 UTC
Modified files:
/pear-core/PEAR/Frontend CLI.php
Log:
Cosmetics
http://cvs.php.net/viewvc.cgi/pear-core/PEAR/Frontend/CLI.php?r1=1.68&r2=1.69&diff_format=u Index: pear-core/PEAR/Frontend/CLI.php diff -u pear-core/PEAR/Frontend/CLI.php:1.68 pear-core/PEAR/Frontend/CLI.php:1.69 --- pear-core/PEAR/Frontend/CLI.php:1.68 Thu Jan 3 20:26:36 2008 +++ pear-core/PEAR/Frontend/CLI.php Sat Jul 12 18:59:39 2008 @@ -16,7 +16,7 @@ * @author Greg Beaver <cellog@php.net> * @copyright 1997-2008 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: CLI.php,v 1.68 2008/01/03 20:26:36 cellog Exp $ + * @version CVS: $Id: CLI.php,v 1.69 2008/07/12 18:59:39 dufuz Exp $ * @link http://pear.php.net/package/PEAR * @since File available since Release 0.1 */ @@ -347,13 +347,13 @@ } $testprompts = array_keys($prompts); $result = $defaults; + if (!defined('STDIN')) { - $fp = fopen('php://stdin', 'r'); - } else { - $fp = STDIN; + define('STDIN', fopen('php://stdin', 'r')); } + reset($prompts); - if (count($prompts) == 1 && $types[key($prompts)] == 'yesno') { + if (count($prompts) === 1 && $types[key($prompts)] == 'yesno') { foreach ($prompts as $key => $prompt) { $type = $types[$key]; $default = @$defaults[$key]; @@ -362,14 +362,8 @@ print "[$default] "; } print ": "; - if (version_compare(phpversion(), '5.0.0', '<')) { - $line = fgets($fp, 2048); - } else { - if (!defined('STDIN')) { - define('STDIN', fopen('php://stdin', 'r')); - } - $line = fgets(STDIN, 2048); - } + + $line = fgets(STDIN, 2048); if ($default && trim($line) == "") { $result[$key] = $default; } else { @@ -378,6 +372,7 @@ } return $result; } + while (true) { $descLength = max(array_map('strlen', $prompts)); $descFormat = "%-{$descLength}s"; @@ -385,24 +380,26 @@ $i = 0; foreach ($prompts as $n => $var) { - printf("%2d. $descFormat : %s\n", ++$i, $prompts[$n], isset($result[$n]) ? - $result[$n] : null); + $res = isset($result[$n]) ? $result[$n] : null; + printf("%2d. $descFormat : %s\n", ++$i, $prompts[$n], $res); } print "\n1-$last, 'all', 'abort', or Enter to continue: "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (empty($tmp)) { break; } + if ($tmp == 'abort') { return false; } + if (isset($testprompts[(int)$tmp - 1])) { $var = $testprompts[(int)$tmp - 1]; $desc = $prompts[$var]; $current = @$result[$var]; print "$desc [$current] : "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (trim($tmp) !== '') { $result[$var] = trim($tmp); } @@ -410,16 +407,18 @@ foreach ($prompts as $var => $desc) { $current = $result[$var]; print "$desc [$current] : "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (trim($tmp) !== '') { $result[$var] = trim($tmp); } } } } - if (!defined('STDIN')) { - fclose($fp); + + if (!defined('STDOUT')) { + fclose(STDIN); } + return $result; } @@ -484,12 +483,14 @@ if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) { $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0); } + if (strpos($col, "\n") !== false) { $multiline = explode("\n", $col); $w = 0; foreach ($multiline as $n => $line) { - if (strlen($line) > $w) { - $w = strlen($line); + $len = strlen($line); + if ($len > $w) { + $w = $len; } } $lines = sizeof($multiline); @@ -504,6 +505,7 @@ } else { $this->params['widest'][$i] = $w; } + $tmp = count_chars($columns[$i], 1); // handle unix, mac and windows formats $lines = (isset($tmp[10]) ? $tmp[10] : (isset($tmp[13]) ? $tmp[13] : 0)) + 1; @@ -511,15 +513,17 @@ $highest = $lines; } } + if (sizeof($columns) > $this->params['ncols']) { $this->params['ncols'] = sizeof($columns); } + $new_row = array( - 'data' => $columns, - 'height' => $highest, + 'data' => $columns, + 'height' => $highest, 'rowparams' => $rowparams, 'colparams' => $colparams, - ); + ); $this->params['table_data'][] = $new_row; } @@ -537,9 +541,11 @@ if (!empty($caption)) { $this->_displayHeading($caption); } - if (count($table_data) == 0) { + + if (count($table_data) === 0) { return; } + if (!isset($width)) { $width = $widest; } else { @@ -549,56 +555,63 @@ } } } + $border = false; if (empty($border)) { - $cellstart = ''; - $cellend = ' '; - $rowend = ''; - $padrowend = false; + $cellstart = ''; + $cellend = ' '; + $rowend = ''; + $padrowend = false; $borderline = ''; } else { - $cellstart = '| '; - $cellend = ' '; - $rowend = '|'; - $padrowend = true; + $cellstart = '| '; + $cellend = ' '; + $rowend = '|'; + $padrowend = true; $borderline = '+'; foreach ($width as $w) { $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 < count($table_data); $i++) { extract($table_data[$i]); if (!is_array($rowparams)) { $rowparams = array(); } + if (!is_array($colparams)) { $colparams = array(); } + $rowlines = array(); if ($height > 1) { - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]); - if (sizeof($rowlines[$c]) < $height) { + if (count($rowlines[$c]) < $height) { $rowlines[$c] = array_pad($rowlines[$c], $height, ''); } } } else { - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { $rowlines[$c] = array($data[$c]); } } + for ($r = 0; $r < $height; $r++) { $rowtext = ''; - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { if (isset($colparams[$c])) { $attribs = array_merge($rowparams, $colparams); } else { $attribs = $rowparams; } + $w = isset($width[$c]) ? $width[$c] : 0; //$cell = $data[$c]; $cell = $rowlines[$c][$r]; @@ -606,9 +619,11 @@ if ($l > $w) { $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 @@ -617,13 +632,16 @@ $rowtext .= $cellstart . $cell . $cellend; } + if (!$border) { $rowtext = rtrim($rowtext); } + $rowtext .= $rowend; $this->_displayLine($rowtext); } } + if ($borderline) { $this->_displayLine($borderline); } @@ -753,6 +771,7 @@ array('bold' => true), $opts); } + foreach($data['data'] as $row) { $this->_tableRow($row, null, $opts); } @@ -767,7 +786,6 @@ // }}} // {{{ log(text) - function log($text, $append_crlf = true) { if ($append_crlf) { @@ -776,7 +794,6 @@ return $this->_display($text); } - // }}} // {{{ bold($text) @@ -787,8 +804,5 @@ } return $this->term['bold'] . $text . $this->term['normal']; } - // }}} -} - -?> +} \ No newline at end of file
http://cvs.php.net/viewvc.cgi/pear-core/PEAR/Frontend/CLI.php?r1=1.68&r2=1.69&diff_format=u Index: pear-core/PEAR/Frontend/CLI.php diff -u pear-core/PEAR/Frontend/CLI.php:1.68 pear-core/PEAR/Frontend/CLI.php:1.69 --- pear-core/PEAR/Frontend/CLI.php:1.68 Thu Jan 3 20:26:36 2008 +++ pear-core/PEAR/Frontend/CLI.php Sat Jul 12 18:59:39 2008 @@ -16,7 +16,7 @@ * @author Greg Beaver <cellog@php.net> * @copyright 1997-2008 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: CLI.php,v 1.68 2008/01/03 20:26:36 cellog Exp $ + * @version CVS: $Id: CLI.php,v 1.69 2008/07/12 18:59:39 dufuz Exp $ * @link http://pear.php.net/package/PEAR * @since File available since Release 0.1 */ @@ -347,13 +347,13 @@ } $testprompts = array_keys($prompts); $result = $defaults; + if (!defined('STDIN')) { - $fp = fopen('php://stdin', 'r'); - } else { - $fp = STDIN; + define('STDIN', fopen('php://stdin', 'r')); } + reset($prompts); - if (count($prompts) == 1 && $types[key($prompts)] == 'yesno') { + if (count($prompts) === 1 && $types[key($prompts)] == 'yesno') { foreach ($prompts as $key => $prompt) { $type = $types[$key]; $default = @$defaults[$key]; @@ -362,14 +362,8 @@ print "[$default] "; } print ": "; - if (version_compare(phpversion(), '5.0.0', '<')) { - $line = fgets($fp, 2048); - } else { - if (!defined('STDIN')) { - define('STDIN', fopen('php://stdin', 'r')); - } - $line = fgets(STDIN, 2048); - } + + $line = fgets(STDIN, 2048); if ($default && trim($line) == "") { $result[$key] = $default; } else { @@ -378,6 +372,7 @@ } return $result; } + while (true) { $descLength = max(array_map('strlen', $prompts)); $descFormat = "%-{$descLength}s"; @@ -385,24 +380,26 @@ $i = 0; foreach ($prompts as $n => $var) { - printf("%2d. $descFormat : %s\n", ++$i, $prompts[$n], isset($result[$n]) ? - $result[$n] : null); + $res = isset($result[$n]) ? $result[$n] : null; + printf("%2d. $descFormat : %s\n", ++$i, $prompts[$n], $res); } print "\n1-$last, 'all', 'abort', or Enter to continue: "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (empty($tmp)) { break; } + if ($tmp == 'abort') { return false; } + if (isset($testprompts[(int)$tmp - 1])) { $var = $testprompts[(int)$tmp - 1]; $desc = $prompts[$var]; $current = @$result[$var]; print "$desc [$current] : "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (trim($tmp) !== '') { $result[$var] = trim($tmp); } @@ -410,16 +407,18 @@ foreach ($prompts as $var => $desc) { $current = $result[$var]; print "$desc [$current] : "; - $tmp = trim(fgets($fp, 1024)); + $tmp = trim(fgets(STDIN, 1024)); if (trim($tmp) !== '') { $result[$var] = trim($tmp); } } } } - if (!defined('STDIN')) { - fclose($fp); + + if (!defined('STDOUT')) { + fclose(STDIN); } + return $result; } @@ -484,12 +483,14 @@ if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) { $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0); } + if (strpos($col, "\n") !== false) { $multiline = explode("\n", $col); $w = 0; foreach ($multiline as $n => $line) { - if (strlen($line) > $w) { - $w = strlen($line); + $len = strlen($line); + if ($len > $w) { + $w = $len; } } $lines = sizeof($multiline); @@ -504,6 +505,7 @@ } else { $this->params['widest'][$i] = $w; } + $tmp = count_chars($columns[$i], 1); // handle unix, mac and windows formats $lines = (isset($tmp[10]) ? $tmp[10] : (isset($tmp[13]) ? $tmp[13] : 0)) + 1; @@ -511,15 +513,17 @@ $highest = $lines; } } + if (sizeof($columns) > $this->params['ncols']) { $this->params['ncols'] = sizeof($columns); } + $new_row = array( - 'data' => $columns, - 'height' => $highest, + 'data' => $columns, + 'height' => $highest, 'rowparams' => $rowparams, 'colparams' => $colparams, - ); + ); $this->params['table_data'][] = $new_row; } @@ -537,9 +541,11 @@ if (!empty($caption)) { $this->_displayHeading($caption); } - if (count($table_data) == 0) { + + if (count($table_data) === 0) { return; } + if (!isset($width)) { $width = $widest; } else { @@ -549,56 +555,63 @@ } } } + $border = false; if (empty($border)) { - $cellstart = ''; - $cellend = ' '; - $rowend = ''; - $padrowend = false; + $cellstart = ''; + $cellend = ' '; + $rowend = ''; + $padrowend = false; $borderline = ''; } else { - $cellstart = '| '; - $cellend = ' '; - $rowend = '|'; - $padrowend = true; + $cellstart = '| '; + $cellend = ' '; + $rowend = '|'; + $padrowend = true; $borderline = '+'; foreach ($width as $w) { $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 < count($table_data); $i++) { extract($table_data[$i]); if (!is_array($rowparams)) { $rowparams = array(); } + if (!is_array($colparams)) { $colparams = array(); } + $rowlines = array(); if ($height > 1) { - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]); - if (sizeof($rowlines[$c]) < $height) { + if (count($rowlines[$c]) < $height) { $rowlines[$c] = array_pad($rowlines[$c], $height, ''); } } } else { - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { $rowlines[$c] = array($data[$c]); } } + for ($r = 0; $r < $height; $r++) { $rowtext = ''; - for ($c = 0; $c < sizeof($data); $c++) { + for ($c = 0; $c < count($data); $c++) { if (isset($colparams[$c])) { $attribs = array_merge($rowparams, $colparams); } else { $attribs = $rowparams; } + $w = isset($width[$c]) ? $width[$c] : 0; //$cell = $data[$c]; $cell = $rowlines[$c][$r]; @@ -606,9 +619,11 @@ if ($l > $w) { $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 @@ -617,13 +632,16 @@ $rowtext .= $cellstart . $cell . $cellend; } + if (!$border) { $rowtext = rtrim($rowtext); } + $rowtext .= $rowend; $this->_displayLine($rowtext); } } + if ($borderline) { $this->_displayLine($borderline); } @@ -753,6 +771,7 @@ array('bold' => true), $opts); } + foreach($data['data'] as $row) { $this->_tableRow($row, null, $opts); } @@ -767,7 +786,6 @@ // }}} // {{{ log(text) - function log($text, $append_crlf = true) { if ($append_crlf) { @@ -776,7 +794,6 @@ return $this->_display($text); } - // }}} // {{{ bold($text) @@ -787,8 +804,5 @@ } return $this->term['bold'] . $text . $this->term['normal']; } - // }}} -} - -?> +} \ No newline at end of file