summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2014-03-19 22:56:29 +0100
committerAndreas Gohr <andi@splitbrain.org>2014-03-19 22:56:29 +0100
commitae1ce4a68df467af30fafe2e47a3c11983d7f8be (patch)
tree2e0b9c68657d559bb1a76bf9fd8afa09a4d7c35b
parent9fb664942d6f51d48d02b592980455259907d9a5 (diff)
downloadrpg-ae1ce4a68df467af30fafe2e47a3c11983d7f8be.tar.gz
rpg-ae1ce4a68df467af30fafe2e47a3c11983d7f8be.tar.bz2
wrap help texts to fit typial terminal width
-rwxr-xr-xbin/gittool.php5
-rw-r--r--inc/cli.php67
2 files changed, 54 insertions, 18 deletions
diff --git a/bin/gittool.php b/bin/gittool.php
index b429c8d1a..cb4ef3484 100755
--- a/bin/gittool.php
+++ b/bin/gittool.php
@@ -8,7 +8,6 @@ define('NOSESSION', 1);
require_once(DOKU_INC . 'inc/init.php');
-
/**
* Easily manage DokuWiki git repositories
*
@@ -25,10 +24,9 @@ class GitToolCLI extends DokuCLI {
protected function setup(DokuCLI_Options $options) {
$options->setHelp(
"Manage git repositories for DokuWiki and its plugins and templates.\n\n" .
- "EXAMPLE\n\n" .
"$> ./bin/gittool.php clone gallery template:ach\n" .
"$> ./bin/gittool.php repos\n" .
- "$> ./bin/gittool.php origin -v\n"
+ "$> ./bin/gittool.php origin -v"
);
$options->registerArgument(
@@ -104,7 +102,6 @@ class GitToolCLI extends DokuCLI {
}
}
-
/**
* Tries to install the given extensions using git clone
*
diff --git a/inc/cli.php b/inc/cli.php
index 2cba828f0..6380c9174 100644
--- a/inc/cli.php
+++ b/inc/cli.php
@@ -23,7 +23,6 @@ abstract class DokuCLI {
public function __construct() {
set_exception_handler(array($this, 'fatal'));
-
$this->options = new DokuCLI_Options();
$this->colors = new DokuCLI_Colors();
}
@@ -247,7 +246,7 @@ class DokuCLI_Options {
/** @var string current parsed command if any */
protected $command = '';
- /** @var array passed non-option arguments*/
+ /** @var array passed non-option arguments */
public $args = array();
/** @var string the executed script */
@@ -451,7 +450,7 @@ class DokuCLI_Options {
$this->args = $non_opts;
// if not done yet, check if first argument is a command and reexecute argument parsing if it is
- if(!$this->command && $this->args && isset($this->setup[$this->args[0]]) ) {
+ if(!$this->command && $this->args && isset($this->setup[$this->args[0]])) {
// it is a command!
$this->command = array_shift($this->args);
$this->parseOptions(); // second pass
@@ -502,7 +501,6 @@ class DokuCLI_Options {
$text .= "\n$command";
}
- if($hasopts) $text .= ' <OPTIONS>';
foreach($this->setup[$command]['args'] as $arg) {
if($arg['required']) {
$text .= ' <' . $arg['name'] . '>';
@@ -510,36 +508,45 @@ class DokuCLI_Options {
$text .= ' [<' . $arg['name'] . '>]';
}
}
- $text .= "\n\n";
+ $text .= "\n";
if($this->setup[$command]['help']) {
- $text .= ' ' . $this->setup[$command]['help'] . "\n\n";
+ $text .= "\n";
+ $text .= $this->tableFormat(
+ array(2, 72),
+ array('', $this->setup[$command]['help'] . "\n")
+ );
}
if($hasopts) {
- $text .= " OPTIONS\n\n";
-
+ $text .= "\n";
foreach($this->setup[$command]['opts'] as $long => $opt) {
$name = "--$long";
if($opt['short']) $name = '-' . $opt['short'] . ' ' . $name;
if($opt['needsarg']) $name .= ' <arg>';
- $text .= sprintf(" %-15s %s\n", $name, $opt['help']);
-
+ $text .= $this->tableFormat(
+ array(2, 20, 52),
+ array('', $name, $opt['help'])
+ );
}
}
if($hasargs) {
- $text .= " ARGUMENTS\n\n";
+ $text .= "\n";
foreach($this->setup[$command]['args'] as $arg) {
$name = '<' . $arg['name'] . '>';
- $text .= sprintf(" %-15s %s\n", $name, $arg['help']);
+
+ $text .= $this->tableFormat(
+ array(2, 20, 52),
+ array('', $name, $arg['help'])
+ );
}
}
- if($command == '' && $hascommands){
- $text .= "\n\nThis tool accepts a command as first parameter as outlined below:\n";
+ if($command == '' && $hascommands) {
+ $text .= "\nThis tool accepts a command as first parameter as outlined below:\n";
}
}
@@ -570,6 +577,38 @@ class DokuCLI_Options {
return $argv;
}
+ /**
+ * Displays text in multiple word wrapped columns
+ *
+ * @param array $widths list of column widths (in characters)
+ * @param array $texts list of texts for each column
+ * @return string
+ */
+ private function tableFormat($widths, $texts) {
+ $wrapped = array();
+ $maxlen = 0;
+
+ foreach($widths as $col => $width) {
+ $wrapped[$col] = explode("\n", wordwrap($texts[$col], $width - 1, true)); // -1 char border
+ $len = count($wrapped[$col]);
+ if($len > $maxlen) $maxlen = $len;
+
+ }
+
+ $out = '';
+ for($i = 0; $i < $maxlen; $i++) {
+ foreach($widths as $col => $width) {
+ if(isset($wrapped[$col][$i])) {
+ $val = $wrapped[$col][$i];
+ } else {
+ $val = '';
+ }
+ $out .= sprintf('%-' . $width . 's', $val);
+ }
+ $out .= "\n";
+ }
+ return $out;
+ }
}
/**