From 5144ebf4007b3eb2bf154cd102caa8e99ef96874 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Tue, 11 Apr 2017 11:37:39 +0200 Subject: [PATCH] Colorize output in familiar colors. --- framework/Argv/doc/Horde/Argv/UPGRADING | 25 +++++++++++ .../Argv/lib/Horde/Argv/HelpFormatter.php | 41 ++++++++++++++++++- .../lib/Horde/Argv/IndentedHelpFormatter.php | 21 ++++++---- .../lib/Horde/Argv/TitledHelpFormatter.php | 23 +++++++---- framework/Argv/package.xml | 20 +++++---- .../Argv/test/Horde/Argv/CallbackTest.php | 29 +++++++++---- .../test/Horde/Argv/ConflictOverrideTest.php | 31 ++++++++++---- .../Argv/test/Horde/Argv/ConflictTestCase.php | 11 +++-- .../test/Horde/Argv/ExpandDefaultsTest.php | 8 +++- framework/Argv/test/Horde/Argv/HelpTest.php | 13 +++++- .../Argv/test/Horde/Argv/ProgNameTest.php | 24 ++++++++--- 11 files changed, 196 insertions(+), 50 deletions(-) create mode 100644 framework/Argv/doc/Horde/Argv/UPGRADING diff --git a/framework/Argv/doc/Horde/Argv/UPGRADING b/framework/Argv/doc/Horde/Argv/UPGRADING new file mode 100644 index 00000000000..b8344fa1588 --- /dev/null +++ b/framework/Argv/doc/Horde/Argv/UPGRADING @@ -0,0 +1,25 @@ +====================== + Upgrading Horde_Argv +====================== + +:Contact: dev@lists.horde.org + +.. contents:: Contents +.. section-numbering:: + + +This lists the API changes between releases of the package. + + +Upgrading to 2.1.0 +================== + + - Horde_Argv_HelpFormatter + + - highlightHeading(), highlightOption() + + These methods have been added. + + - __construct() + + The $color parameter has been added. diff --git a/framework/Argv/lib/Horde/Argv/HelpFormatter.php b/framework/Argv/lib/Horde/Argv/HelpFormatter.php index 649efce8dd8..da3788dce63 100644 --- a/framework/Argv/lib/Horde/Argv/HelpFormatter.php +++ b/framework/Argv/lib/Horde/Argv/HelpFormatter.php @@ -25,6 +25,8 @@ * Instance attributes: * parser : Horde_Argv_Parser * the controlling Horde_Argv_Parser instance + * _color : Horde_Cli_Color @since Horde_Argv 2.1.0 + * a color formatter * indent_increment : int * the number of columns to indent per nesting level * max_help_position : int @@ -69,9 +71,15 @@ abstract class Horde_Argv_HelpFormatter public $parser = null; - public function __construct($indent_increment, $max_help_position, - $width = null, $short_first = false) + public function __construct( + $indent_increment, $max_help_position, $width = null, + $short_first = false, $color = null + ) { + if (is_null($color)) { + $color = new Horde_Cli_Color(); + } + $this->_color = $color; $this->indent_increment = $indent_increment; $this->help_position = $this->max_help_position = $max_help_position; if (is_null($width)) { @@ -127,6 +135,20 @@ abstract public function formatUsage($usage); abstract public function formatHeading($heading); + /** + * Highlights with the heading color. + * + * @since Horde_Argv 2.1.0 + * + * @param string $heading A heading text. + * + * @retun string The colored text. + */ + public function highlightHeading($heading) + { + return $this->_color->brown($heading); + } + /** * Format a paragraph of free-form text for inclusion in the * help output at the current indentation level. @@ -209,6 +231,7 @@ public function formatOption($option) ); $indent_first = 0; } + $opts = $this->highlightOption($opts); $result[] = $opts; if ($option->help) { $help_text = $this->expandDefault($option); @@ -227,6 +250,20 @@ public function formatOption($option) return implode('', $result); } + /** + * Highlights with the option color. + * + * @since Horde_Argv 2.1.0 + * + * @param string $option An option text. + * + * @retun string The colored text. + */ + public function highlightOption($option) + { + return $this->_color->green($option); + } + public function storeOptionStrings($parser) { $this->indent(); diff --git a/framework/Argv/lib/Horde/Argv/IndentedHelpFormatter.php b/framework/Argv/lib/Horde/Argv/IndentedHelpFormatter.php index ff45ce4ee78..f6d3c8f0bd5 100644 --- a/framework/Argv/lib/Horde/Argv/IndentedHelpFormatter.php +++ b/framework/Argv/lib/Horde/Argv/IndentedHelpFormatter.php @@ -27,22 +27,29 @@ class Horde_Argv_IndentedHelpFormatter extends Horde_Argv_HelpFormatter { public function __construct( - $indent_increment = 2, - $max_help_position = 24, - $width = null, - $short_first = true) + $indent_increment = 2, $max_help_position = 24, $width = null, + $short_first = true, $color = null + ) { - parent::__construct($indent_increment, $max_help_position, $width, $short_first); + parent::__construct( + $indent_increment, $max_help_position, $width, $short_first, $color + ); } public function formatUsage($usage) { - return sprintf(Horde_Argv_Translation::t("Usage:") . " %s\n", $usage); + return sprintf( + $this->highlightHeading(Horde_Argv_Translation::t("Usage:")) + . " %s\n", + $usage + ); } public function formatHeading($heading) { - return sprintf('%' . $this->current_indent . "s%s:\n", '', $heading); + return $this->highlightHeading(sprintf( + '%' . $this->current_indent . "s%s:\n", '', $heading + )); } } diff --git a/framework/Argv/lib/Horde/Argv/TitledHelpFormatter.php b/framework/Argv/lib/Horde/Argv/TitledHelpFormatter.php index dfacbe75ff2..b11096815e8 100644 --- a/framework/Argv/lib/Horde/Argv/TitledHelpFormatter.php +++ b/framework/Argv/lib/Horde/Argv/TitledHelpFormatter.php @@ -27,23 +27,32 @@ class Horde_Argv_TitledHelpFormatter extends Horde_Argv_HelpFormatter { public function __construct( - $indent_increment = 0, - $max_help_position = 24, - $width = null, - $short_first = false) + $indent_increment = 0, $max_help_position = 24, $width = null, + $short_first = false, $color = null + ) { - parent::__construct($indent_increment, $max_help_position, $width, $short_first); + parent::__construct( + $indent_increment, $max_help_position, $width, $short_first, $color + ); } public function formatUsage($usage) { - return sprintf("%s %s\n", $this->formatHeading(Horde_Argv_Translation::t("Usage")), $usage); + return sprintf( + "%s %s\n", + $this->formatHeading(Horde_Argv_Translation::t("Usage")), + $usage + ); } public function formatHeading($heading) { $prefix = array('=', '-'); - return sprintf("%s\n%s\n", $heading, str_repeat($prefix[$this->level], strlen($heading))); + return $this->highlightHeading(sprintf( + "%s\n%s\n", + $heading, + str_repeat($prefix[$this->level], strlen($heading)) + )); } } diff --git a/framework/Argv/package.xml b/framework/Argv/package.xml index bb69d561ac9..2df42358c55 100644 --- a/framework/Argv/package.xml +++ b/framework/Argv/package.xml @@ -17,10 +17,10 @@ mike@maintainable.com yes - 2016-02-01 + 2017-04-11 - 2.0.13 - 1.0.0 + 2.1.0 + 1.1.0 stable @@ -28,7 +28,8 @@ BSD-2-Clause -* +* [jan] Colorize output. +* [jan] Add Horde_Argv_HelpFormatter#highlightHeading() and Horde_Argv_HelpFormatter#highlightOption(). @@ -41,6 +42,7 @@ + @@ -424,6 +426,7 @@ + @@ -918,15 +921,16 @@ - 2.0.13 - 1.0.0 + 2.1.0 + 1.1.0 stable stable - 2016-02-01 + 2017-04-11 BSD-2-Clause -* +* [jan] Colorize output. +* [jan] Add Horde_Argv_HelpFormatter#highlightHeading() and Horde_Argv_HelpFormatter#highlightOption(). diff --git a/framework/Argv/test/Horde/Argv/CallbackTest.php b/framework/Argv/test/Horde/Argv/CallbackTest.php index cbbaa8a1a57..828389f37a2 100644 --- a/framework/Argv/test/Horde/Argv/CallbackTest.php +++ b/framework/Argv/test/Horde/Argv/CallbackTest.php @@ -61,19 +61,32 @@ public function testCallback() public function testCallbackHelp() { - // This test was prompted by SF bug #960515 -- the point is - // not to inspect the help text, just to make sure that - // formatHelp() doesn't crash. - $parser = new Horde_Argv_Parser(array('usage' => Horde_Argv_Option::SUPPRESS_USAGE)); + // This test was prompted by SF bug #960515 -- the point is not to + // inspect the help text, just to make sure that formatHelp() doesn't + // crash. + $parser = new Horde_Argv_Parser(array( + 'usage' => Horde_Argv_Option::SUPPRESS_USAGE, + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); $parser->removeOption('-h'); - $parser->addOption('-t', '--test', - array('action' => 'callback', 'callback' => array($this, 'returnNull'), - 'type' => 'string', 'help' => 'foo')); + $parser->addOption( + '-t', '--test', + array( + 'action' => 'callback', + 'callback' => array($this, 'returnNull'), + 'type' => 'string', + 'help' => 'foo' + ) + ); $expectedHelp = "Options:\n -t TEST, --test=TEST foo\n"; $this->assertHelp($parser, $expectedHelp); } public function returnNull() - {} + { + } } diff --git a/framework/Argv/test/Horde/Argv/ConflictOverrideTest.php b/framework/Argv/test/Horde/Argv/ConflictOverrideTest.php index 902adffb6ac..f4ae2bf6586 100644 --- a/framework/Argv/test/Horde/Argv/ConflictOverrideTest.php +++ b/framework/Argv/test/Horde/Argv/ConflictOverrideTest.php @@ -16,14 +16,31 @@ class Horde_Argv_ConflictOverrideTest extends Horde_Argv_TestCase public function setUp() { parent::setUp(); - $this->parser = new Horde_Argv_InterceptingParser(array('usage' => Horde_Argv_Option::SUPPRESS_USAGE)); + $this->parser = new Horde_Argv_InterceptingParser(array( + 'usage' => Horde_Argv_Option::SUPPRESS_USAGE, + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); $this->parser->setConflictHandler('resolve'); - $this->parser->addOption('-n', '--dry-run', - array('action' => 'store_true', 'dest' => 'dry_run', - 'help' => "don't do anything")); - $this->parser->addOption('--dry-run', '-n', - array('action' => 'store_const', 'const' => 42, 'dest' => 'dry_run', - 'help' => 'dry run mode')); + $this->parser->addOption( + '-n', '--dry-run', + array( + 'action' => 'store_true', + 'dest' => 'dry_run', + 'help' => "don't do anything" + ) + ); + $this->parser->addOption( + '--dry-run', '-n', + array( + 'action' => 'store_const', + 'const' => 42, + 'dest' => 'dry_run', + 'help' => 'dry run mode' + ) + ); } public function testConflictOverrideOpts() diff --git a/framework/Argv/test/Horde/Argv/ConflictTestCase.php b/framework/Argv/test/Horde/Argv/ConflictTestCase.php index d3e181ccffc..987866f7ce4 100644 --- a/framework/Argv/test/Horde/Argv/ConflictTestCase.php +++ b/framework/Argv/test/Horde/Argv/ConflictTestCase.php @@ -18,9 +18,14 @@ public function setUp() 'help' => 'increment verbosity')) ); - $this->parser = new Horde_Argv_InterceptingParser( - array('usage' => Horde_Argv_Option::SUPPRESS_USAGE, 'optionList' => $options) - ); + $this->parser = new Horde_Argv_InterceptingParser(array( + 'usage' => Horde_Argv_Option::SUPPRESS_USAGE, + 'optionList' => $options, + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); } public function showVersion($option, $opt, $value, $parser) diff --git a/framework/Argv/test/Horde/Argv/ExpandDefaultsTest.php b/framework/Argv/test/Horde/Argv/ExpandDefaultsTest.php index 6f109500cbd..ff9017c04f9 100644 --- a/framework/Argv/test/Horde/Argv/ExpandDefaultsTest.php +++ b/framework/Argv/test/Horde/Argv/ExpandDefaultsTest.php @@ -16,7 +16,13 @@ class Horde_Argv_ExpandDefaultsTest extends Horde_Argv_TestCase public function setUp() { parent::setUp(); - $this->parser = new Horde_Argv_Parser(array('prog' => 'test')); + $this->parser = new Horde_Argv_Parser(array( + 'prog' => 'test', + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); $this->help_prefix = 'Usage: test [options] Options: diff --git a/framework/Argv/test/Horde/Argv/HelpTest.php b/framework/Argv/test/Horde/Argv/HelpTest.php index b7dc33cef7b..5a9a18380a3 100644 --- a/framework/Argv/test/Horde/Argv/HelpTest.php +++ b/framework/Argv/test/Horde/Argv/HelpTest.php @@ -111,7 +111,13 @@ public function makeParser($columns) putenv('COLUMNS=' . $columns); - return new Horde_Argv_InterceptingParser(array('optionList' => $options)); + return new Horde_Argv_InterceptingParser(array( + 'optionList' => $options, + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); } public function assertHelpEquals($expectedOutput) @@ -147,7 +153,10 @@ public function testHelpLongOptsFirst() public function testHelpTitleFormatter() { - $this->parser->formatter = new Horde_Argv_TitledHelpFormatter(); + $this->parser->formatter = new Horde_Argv_TitledHelpFormatter( + 0, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ); $this->assertHelpEquals(self::$expected_help_title_formatter); } diff --git a/framework/Argv/test/Horde/Argv/ProgNameTest.php b/framework/Argv/test/Horde/Argv/ProgNameTest.php index 1dbb601feb3..3b434a85a2a 100644 --- a/framework/Argv/test/Horde/Argv/ProgNameTest.php +++ b/framework/Argv/test/Horde/Argv/ProgNameTest.php @@ -37,11 +37,19 @@ public function assertVersion($parser, $expectedVersion) public function testDefaultProgName() { - // Make sure that program name is taken from $_SERVER['argv'][0] by default. + // Make sure that program name is taken from $_SERVER['argv'][0] by + // default. $saveArgv = $_SERVER['argv']; try { $_SERVER['argv'][0] = 'foo/bar/baz.php'; - $parser = new Horde_Argv_Parser(array('usage' => "%prog ...", 'version' => "%prog 1.2")); + $parser = new Horde_Argv_Parser(array( + 'usage' => "%prog ...", + 'version' => "%prog 1.2", + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); $expectedUsage = "Usage: baz.php ...\n"; } catch (Exception $e) { $_SERVER['argv'] = $saveArgv; @@ -59,9 +67,15 @@ public function testDefaultProgName() public function testCustomProgName() { - $parser = new Horde_Argv_Parser(array('prog' => 'thingy', - 'version' => "%prog 0.1", - 'usage' => "%prog arg arg")); + $parser = new Horde_Argv_Parser(array( + 'prog' => 'thingy', + 'version' => "%prog 0.1", + 'usage' => "%prog arg arg", + 'formatter' => new Horde_Argv_IndentedHelpFormatter( + 2, 24, null, true, + new Horde_Cli_Color(Horde_Cli_Color::FORMAT_NONE) + ) + )); $parser->removeOption('-h'); $parser->removeOption('--version'); $expectedUsage = "Usage: thingy arg arg\n";