/** * Returns a standard help output for your program. * * This method generates a help text as it's commonly known from Unix * command line programs. The output will contain the synopsis, your * provided program description and the selected parameter help * as also provided by {@link ezcConsoleInput::getHelp()}. The returned * string can directly be printed to the console. * * The $paramGrouping option can be used to group options in the help * output. The structure of this array parameter is as follows: * * <code> * array( * 'First section' => array( * 'input', * 'output' * 'overwrite', * ), * 'Second section' => array( * 'v', * 'h', * ), * ) * </code> * * As can be seen, short option names are possible as well as long ones. * The key of the first array level is the name of the section, which is * assigned to an array of options to group under this section. The $params * parameter still influences if an option as displayed at all. * * @param string $programDesc The description of your program. * @param int $width The width to adjust the output text to. * @param bool $long Set this to true for getting the long * help version. * @param array(string) $params Set of option names to generate help * for, default is all. * @param array(string=>array(string)) $paramGrouping * @return string The generated help text. */ public function getHelpText($programDesc, $width = 80, $long = false, array $params = null, $paramGrouping = null) { $help = $this->getHelp($long, $params == null ? array() : $params, $paramGrouping); // Determine max length of first column text. $maxLength = 0; foreach ($help as $row) { $maxLength = max($maxLength, iconv_strlen($row[0], 'UTF-8')); } // Width of left column $leftColWidth = $maxLength + 2; // Width of righ column $rightColWidth = $width - $leftColWidth; $res = 'Usage: ' . $this->getSynopsis($params) . PHP_EOL; $res .= $this->stringTool->wordwrap($programDesc, $width, PHP_EOL); $res .= PHP_EOL . PHP_EOL; foreach ($help as $row) { $rowParts = explode("\n", $this->stringTool->wordwrap($row[1], $rightColWidth)); $res .= $this->stringTool->strPad($row[0], $leftColWidth, ' '); $res .= $rowParts[0] . PHP_EOL; // @TODO: Fix function call in loop header for ($i = 1; $i < sizeof($rowParts); $i++) { $res .= str_repeat(' ', $leftColWidth) . $rowParts[$i] . PHP_EOL; } } return $res; }
/** * Returns auto broken rows from an array of cells. * The data provided by a user may not fit into a cell calculated by the * class. In this case, the data can be automatically wrapped. The table * row then spans over multiple physical console lines. * * @param array(string) $cells Array of cells in one row. * @param array(int) $colWidth Columns widths array. * @return array(string) Physical rows generated out of this row. */ private function breakRows($cells, $colWidth) { $rows = array(); // Iterate through cells of the row foreach ($colWidth as $cell => $width) { $data = $cells[$cell]->content; // Physical row id, start with 0 for each row $row = 0; // Split into multiple physical rows if manual breaks exist $dataLines = explode("\n", $data); foreach ($dataLines as $dataLine) { // Does the physical row fit? if (iconv_strlen($dataLine, 'UTF-8') > $colWidth[$cell]) { switch ($this->properties['options']->colWrap) { case ezcConsoleTable::WRAP_AUTO: $subLines = explode("\n", $this->stringTool->wordwrap($dataLine, $colWidth[$cell], "\n", true)); foreach ($subLines as $lineNo => $line) { $rows[$row++][$cell] = $line; } break; case ezcConsoleTable::WRAP_CUT: $rows[$row++][$cell] = iconv_substr($dataLine, 0, $colWidth[$cell], 'UTF-8'); break; case ezcConsoleTable::WRAP_NONE: default: $rows[$row++][$cell] = $dataLine; break; } } else { $rows[$row++][$cell] = $dataLine; } } } return $rows; }