PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file CLI.php

Documentation is available at CLI.php

  1. <?php
  2. /*
  3.   +----------------------------------------------------------------------+
  4.   | PHP Version 5                                                        |
  5.   +----------------------------------------------------------------------+
  6.   | Copyright (c) 1997-2004 The PHP Group                                |
  7.   +----------------------------------------------------------------------+
  8.   | This source file is subject to version 3.0 of the PHP license,       |
  9.   | that is bundled with this package in the file LICENSE, and is        |
  10.   | available through the world-wide-web at the following url:           |
  11.   | http://www.php.net/license/3_0.txt.                                  |
  12.   | If you did not receive a copy of the PHP license and are unable to   |
  13.   | obtain it through the world-wide-web, please send a note to          |
  14.   | license@php.net so we can mail you a copy immediately.               |
  15.   +----------------------------------------------------------------------+
  16.   +----------------------------------------------------------------------+
  17.  
  18.   $Id: CLI.php,v 1.41 2004/02/17 05:49:16 ssb Exp $
  19. */
  20.  
  21. require_once "PEAR.php";
  22.  
  23. class PEAR_Frontend_CLI extends PEAR
  24. {
  25.     // {{{ properties
  26.  
  27.     /**
  28.      * What type of user interface this frontend is for.
  29.      * @var string 
  30.      * @access public
  31.      */
  32.     var $type = 'CLI';
  33.     var $lp = ''; // line prefix
  34.  
  35.     var $params = array();
  36.     var $term = array(
  37.         'bold' => '',
  38.         'normal' => '',
  39.         );
  40.  
  41.     // }}}
  42.  
  43.     // {{{ constructor
  44.  
  45.     function PEAR_Frontend_CLI()
  46.     {
  47.         parent::PEAR();
  48.         $term = getenv('TERM'); //(cox) $_ENV is empty for me in 4.1.1
  49.         if (function_exists('posix_isatty') && !posix_isatty(1)) {
  50.             // output is being redirected to a file or through a pipe
  51.         } elseif ($term) {
  52.             // XXX can use ncurses extension here, if available
  53.             if (preg_match('/^(xterm|vt220|linux)/', $term)) {
  54.                 $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109);
  55.                 $this->term['normal']=sprintf("%c%c%c", 27, 91, 109);
  56.             } elseif (preg_match('/^vt100/', $term)) {
  57.                 $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0);
  58.                 $this->term['normal']=sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0);
  59.             }
  60.         } elseif (OS_WINDOWS) {
  61.             // XXX add ANSI codes here
  62.         }
  63.     }
  64.  
  65.     // }}}
  66.  
  67.     // {{{ displayLine(text)
  68.  
  69.     function displayLine($text)
  70.     {
  71.         trigger_error("PEAR_Frontend_CLI::displayLine deprecated", E_USER_ERROR);
  72.     }
  73.  
  74.     function _displayLine($text)
  75.     {
  76.         print "$this->lp$text\n";
  77.     }
  78.  
  79.     // }}}
  80.     // {{{ display(text)
  81.  
  82.     function display($text)
  83.     {
  84.         trigger_error("PEAR_Frontend_CLI::display deprecated", E_USER_ERROR);
  85.     }
  86.  
  87.     function _display($text)
  88.     {
  89.         print $text;
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ displayError(eobj)
  94.  
  95.     /**
  96.      * @param object PEAR_Error object
  97.      */
  98.     function displayError($eobj)
  99.     {
  100.         return $this->_displayLine($eobj->getMessage());
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ displayFatalError(eobj)
  105.  
  106.     /**
  107.      * @param object PEAR_Error object
  108.      */
  109.     function displayFatalError($eobj)
  110.     {
  111.         $this->displayError($eobj);
  112.         exit(1);
  113.     }
  114.  
  115.     // }}}
  116.     // {{{ displayHeading(title)
  117.  
  118.     function displayHeading($title)
  119.     {
  120.         trigger_error("PEAR_Frontend_CLI::displayHeading deprecated", E_USER_ERROR);
  121.     }
  122.  
  123.     function _displayHeading($title)
  124.     {
  125.         print $this->lp.$this->bold($title)."\n";
  126.         print $this->lp.str_repeat("=", strlen($title))."\n";
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ userDialog(prompt, [type], [default])
  131.  
  132.     function userDialog($command, $prompts, $types = array(), $defaults = array())
  133.     {
  134.         $result = array();
  135.         if (is_array($prompts)) {
  136.             $fp = fopen("php://stdin", "r");
  137.             foreach ($prompts as $key => $prompt) {
  138.                 $type = $types[$key];
  139.                 $default = @$defaults[$key];
  140.                 if ($type == 'password') {
  141.                     system('stty -echo');
  142.                 }
  143.                 print "$this->lp$prompt ";
  144.                 if ($default) {
  145.                     print "[$default] ";
  146.                 }
  147.                 print ": ";
  148.                 $line = fgets($fp, 2048);
  149.                 if ($type == 'password') {
  150.                     system('stty echo');
  151.                     print "\n";
  152.                 }
  153.                 if ($default && trim($line) == "") {
  154.                     $result[$key] = $default;
  155.                 } else {
  156.                     $result[$key] = $line;
  157.                 }
  158.             }
  159.             fclose($fp);
  160.         }
  161.         return $result;
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ userConfirm(prompt, [default])
  166.  
  167.     function userConfirm($prompt, $default = 'yes')
  168.     {
  169.         trigger_error("PEAR_Frontend_CLI::userConfirm not yet converted", E_USER_ERROR);
  170.         static $positives = array('y', 'yes', 'on', '1');
  171.         static $negatives = array('n', 'no', 'off', '0');
  172.         print "$this->lp$prompt [$default] : ";
  173.         $fp = fopen("php://stdin", "r");
  174.         $line = fgets($fp, 2048);
  175.         fclose($fp);
  176.         $answer = strtolower(trim($line));
  177.         if (empty($answer)) {
  178.             $answer = $default;
  179.         }
  180.         if (in_array($answer, $positives)) {
  181.             return true;
  182.         }
  183.         if (in_array($answer, $negatives)) {
  184.             return false;
  185.         }
  186.         if (in_array($default, $positives)) {
  187.             return true;
  188.         }
  189.         return false;
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ startTable([params])
  194.  
  195.     function startTable($params = array())
  196.     {
  197.         trigger_error("PEAR_Frontend_CLI::startTable deprecated", E_USER_ERROR);
  198.     }
  199.  
  200.     function _startTable($params = array())
  201.     {
  202.         $params['table_data'] = array();
  203.         $params['widest'] = array();  // indexed by column
  204.         $params['highest'] = array(); // indexed by row
  205.         $params['ncols'] = 0;
  206.         $this->params = $params;
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ tableRow(columns, [rowparams], [colparams])
  211.  
  212.     function tableRow($columns, $rowparams = array(), $colparams = array())
  213.     {
  214.         trigger_error("PEAR_Frontend_CLI::tableRow deprecated", E_USER_ERROR);
  215.     }
  216.  
  217.     function _tableRow($columns, $rowparams = array(), $colparams = array())
  218.     {
  219.         $highest = 1;
  220.         for ($i = 0; $i < sizeof($columns); $i++) {
  221.             $col = &$columns[$i];
  222.             if (isset($colparams[$i]) && !empty($colparams[$i]['wrap'])) {
  223.                 $col = wordwrap($col, $colparams[$i]['wrap'], "\n", 0);
  224.             }
  225.             if (strpos($col, "\n") !== false) {
  226.                 $multiline = explode("\n", $col);
  227.                 $w = 0;
  228.                 foreach ($multiline as $n => $line) {
  229.                     if (strlen($line) > $w) {
  230.                         $w = strlen($line);
  231.                     }
  232.                 }
  233.                 $lines = sizeof($multiline);
  234.             } else {
  235.                 $w = strlen($col);
  236.             }
  237.             if ($w > @$this->params['widest'][$i]) {
  238.                 $this->params['widest'][$i] = $w;
  239.             }
  240.             $tmp = count_chars($columns[$i], 1);
  241.             // handle unix, mac and windows formats
  242.             $lines = (isset($tmp[10]) ? $tmp[10] : @$tmp[13]) + 1;
  243.             if ($lines > $highest) {
  244.                 $highest = $lines;
  245.             }
  246.         }
  247.         if (sizeof($columns) > $this->params['ncols']) {
  248.             $this->params['ncols'] = sizeof($columns);
  249.         }
  250.         $new_row = array(
  251.             'data' => $columns,
  252.             'height' => $highest,
  253.             'rowparams' => $rowparams,
  254.             'colparams' => $colparams,
  255.             );
  256.         $this->params['table_data'][] = $new_row;
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ endTable()
  261.  
  262.     function endTable()
  263.     {
  264.         trigger_error("PEAR_Frontend_CLI::endTable deprecated", E_USER_ERROR);
  265.     }
  266.  
  267.     function _endTable()
  268.     {
  269.         extract($this->params);
  270.         if (!empty($caption)) {
  271.             $this->_displayHeading($caption);
  272.         }
  273.         if (count($table_data) == 0) {
  274.             return;
  275.         }
  276.         if (!isset($width)) {
  277.             $width = $widest;
  278.         } else {
  279.             for ($i = 0; $i < $ncols; $i++) {
  280.                 if (!isset($width[$i])) {
  281.                     $width[$i] = $widest[$i];
  282.                 }
  283.             }
  284.         }
  285.         $border = false;
  286.         if (empty($border)) {
  287.             $cellstart = '';
  288.             $cellend = ' ';
  289.             $rowend = '';
  290.             $padrowend = false;
  291.             $borderline = '';
  292.         } else {
  293.             $cellstart = '| ';
  294.             $cellend = ' ';
  295.             $rowend = '|';
  296.             $padrowend = true;
  297.             $borderline = '+';
  298.             foreach ($width as $w) {
  299.                 $borderline .= str_repeat('-', $w + strlen($cellstart) + strlen($cellend) - 1);
  300.                 $borderline .= '+';
  301.             }
  302.         }
  303.         if ($borderline) {
  304.             $this->_displayLine($borderline);
  305.         }
  306.         for ($i = 0; $i < sizeof($table_data); $i++) {
  307.             extract($table_data[$i]);
  308.             if (!is_array($rowparams)) {
  309.                 $rowparams = array();
  310.             }
  311.             if (!is_array($colparams)) {
  312.                 $colparams = array();
  313.             }
  314.             $rowlines = array();
  315.             if ($height > 1) {
  316.                 for ($c = 0; $c < sizeof($data); $c++) {
  317.                     $rowlines[$c] = preg_split('/(\r?\n|\r)/', $data[$c]);
  318.                     if (sizeof($rowlines[$c]) < $height) {
  319.                         $rowlines[$c] = array_pad($rowlines[$c], $height, '');
  320.                     }
  321.                 }
  322.             } else {
  323.                 for ($c = 0; $c < sizeof($data); $c++) {
  324.                     $rowlines[$c] = array($data[$c]);
  325.                 }
  326.             }
  327.             for ($r = 0; $r < $height; $r++) {
  328.                 $rowtext = '';
  329.                 for ($c = 0; $c < sizeof($data); $c++) {
  330.                     if (isset($colparams[$c])) {
  331.                         $attribs = array_merge($rowparams, $colparams);
  332.                     } else {
  333.                         $attribs = $rowparams;
  334.                     }
  335.                     $w = isset($width[$c]) ? $width[$c] : 0;
  336.                     //$cell = $data[$c];
  337.                     $cell = $rowlines[$c][$r];
  338.                     $l = strlen($cell);
  339.                     if ($l > $w) {
  340.                         $cell = substr($cell, 0, $w);
  341.                     }
  342.                     if (isset($attribs['bold'])) {
  343.                         $cell = $this->bold($cell);
  344.                     }
  345.                     if ($l < $w) {
  346.                         // not using str_pad here because we may
  347.                         // add bold escape characters to $cell
  348.                         $cell .= str_repeat(' ', $w - $l);
  349.                     }
  350.  
  351.                     $rowtext .= $cellstart . $cell . $cellend;
  352.                 }
  353.                 if (!$border) {
  354.                     $rowtext = rtrim($rowtext);
  355.                 }
  356.                 $rowtext .= $rowend;
  357.                 $this->_displayLine($rowtext);
  358.             }
  359.         }
  360.         if ($borderline) {
  361.             $this->_displayLine($borderline);
  362.         }
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ outputData()
  367.  
  368.     function outputData($data, $command = '_default')
  369.     {
  370.         switch ($command) {
  371.             case 'install':
  372.             case 'upgrade':
  373.             case 'upgrade-all':
  374.                 if (isset($data['release_warnings'])) {
  375.                     $this->_displayLine('');
  376.                     $this->_startTable(array(
  377.                         'border' => false,
  378.                         'caption' => 'Release Warnings'
  379.                         ));
  380.                     $this->_tableRow(array($data['release_warnings']), null, array(1 => array('wrap' => 55)));
  381.                     $this->_endTable();
  382.                     $this->_displayLine('');
  383.                 }
  384.                 $this->_displayLine($data['data']);
  385.                 break;
  386.             case 'search':
  387.                 $this->_startTable($data);
  388.                 if (isset($data['headline']) && is_array($data['headline'])) {
  389.                     $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  390.                 }
  391.  
  392.                 foreach($data['data'] as $category) {
  393.                     foreach($category as $pkg) {
  394.                         $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  395.                     }
  396.                 };
  397.                 $this->_endTable();
  398.                 break;
  399.             case 'list-all':
  400.                 $this->_startTable($data);
  401.                 if (isset($data['headline']) && is_array($data['headline'])) {
  402.                     $this->_tableRow($data['headline'], array('bold' => true), array(1 => array('wrap' => 55)));
  403.                 }
  404.  
  405.                 foreach($data['data'] as $category) {
  406.                     foreach($category as $pkg) {
  407.                         unset($pkg[3]);
  408.                         unset($pkg[4]);
  409.                         $this->_tableRow($pkg, null, array(1 => array('wrap' => 55)));
  410.                     }
  411.                 };
  412.                 $this->_endTable();
  413.                 break;
  414.             case 'config-show':
  415.                 $data['border'] = false;
  416.                 $opts = array(0 => array('wrap' => 30),
  417.                               1 => array('wrap' => 20),
  418.                               2 => array('wrap' => 35));
  419.                 $this->_startTable($data);
  420.                 if (isset($data['headline']) && is_array($data['headline'])) {
  421.                     $this->_tableRow($data['headline'],
  422.                                      array('bold' => true),
  423.                                      $opts);
  424.                 }
  425.                 foreach($data['data'] as $group) {
  426.                     foreach($group as $value) {
  427.                         if ($value[2] == '') {
  428.                             $value[2] = "<not set>";
  429.                         }
  430.                         $this->_tableRow($value, null, $opts);
  431.                     }
  432.                 }
  433.                 $this->_endTable();
  434.                 break;
  435.             case 'remote-info':
  436.                 $data = array(
  437.                     'caption' => 'Package details:',
  438.                     'border' => false,
  439.                     'data' => array(
  440.                         array("Latest",    $data['stable']),
  441.                         array("Installed", $data['installed']),
  442.                         array("Package",   $data['name']),
  443.                         array("License",   $data['license']),
  444.                         array("Category",  $data['category']),
  445.                         array("Summary",   $data['summary']),
  446.                         array("Description", $data['description']),
  447.                         ),
  448.                     );
  449.             default: {
  450.                 if (is_array($data)) {
  451.                     $this->_startTable($data);
  452.                     $count = count($data['data'][0]);
  453.                     if ($count == 2) {
  454.                         $opts = array(0 => array('wrap' => 25),
  455.                                       1 => array('wrap' => 48)
  456.                         );
  457.                     } elseif ($count == 3) {
  458.                         $opts = array(0 => array('wrap' => 30),
  459.                                       1 => array('wrap' => 20),
  460.                                       2 => array('wrap' => 35)
  461.                         );
  462.                     } else {
  463.                         $opts = null;
  464.                     }
  465.                     if (isset($data['headline']) && is_array($data['headline'])) {
  466.                         $this->_tableRow($data['headline'],
  467.                                          array('bold' => true),
  468.                                          $opts);
  469.                     }
  470.                     foreach($data['data'] as $row) {
  471.                         $this->_tableRow($row, null, $opts);
  472.                     }
  473.                     $this->_endTable();
  474.                 } else {
  475.                     $this->_displayLine($data);
  476.                 }
  477.             }
  478.         }
  479.     }
  480.  
  481.     // }}}
  482.     // {{{ log(text)
  483.  
  484.  
  485.     function log($text, $append_crlf = true)
  486.     {
  487.         if ($append_crlf) {
  488.             return $this->_displayLine($text);
  489.         }
  490.         return $this->_display($text);
  491.     }
  492.  
  493.  
  494.     // }}}
  495.     // {{{ bold($text)
  496.  
  497.     function bold($text)
  498.     {
  499.         if (empty($this->term['bold'])) {
  500.             return strtoupper($text);
  501.         }
  502.         return $this->term['bold'] . $text . $this->term['normal'];
  503.     }
  504.  
  505.     // }}}
  506. }
  507.  
  508. ?>

Documentation generated on Mon, 11 Mar 2019 14:23:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.