Skip to content

Commit

Permalink
Colorize output in familiar colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Apr 11, 2017
1 parent 3f619a1 commit 5144ebf
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 50 deletions.
25 changes: 25 additions & 0 deletions 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.
41 changes: 39 additions & 2 deletions framework/Argv/lib/Horde/Argv/HelpFormatter.php
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
21 changes: 14 additions & 7 deletions framework/Argv/lib/Horde/Argv/IndentedHelpFormatter.php
Expand Up @@ -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
));
}

}
23 changes: 16 additions & 7 deletions framework/Argv/lib/Horde/Argv/TitledHelpFormatter.php
Expand Up @@ -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))
));
}

}
20 changes: 12 additions & 8 deletions framework/Argv/package.xml
Expand Up @@ -17,18 +17,19 @@
<email>mike@maintainable.com</email>
<active>yes</active>
</lead>
<date>2016-02-01</date>
<date>2017-04-11</date>
<version>
<release>2.0.13</release>
<api>1.0.0</api>
<release>2.1.0</release>
<api>1.1.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
*
* [jan] Colorize output.
* [jan] Add Horde_Argv_HelpFormatter#highlightHeading() and Horde_Argv_HelpFormatter#highlightOption().
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand All @@ -41,6 +42,7 @@
<file name="DOCS_ORIGIN" role="doc" />
<file name="EXTEND" role="doc" />
<file name="README" role="doc" />
<file name="UPGRADING" role="doc" />
</dir> <!-- /doc/Horde/Argv -->
</dir> <!-- /doc/Horde -->
</dir> <!-- /doc -->
Expand Down Expand Up @@ -424,6 +426,7 @@
<install as="DOCS_ORIGIN" name="doc/Horde/Argv/DOCS_ORIGIN" />
<install as="EXTEND" name="doc/Horde/Argv/EXTEND" />
<install as="README" name="doc/Horde/Argv/README" />
<install as="UPGRADING" name="doc/Horde/Argv/UPGRADING" />
<install as="Horde/Argv/AmbiguousOptionException.php" name="lib/Horde/Argv/AmbiguousOptionException.php" />
<install as="Horde/Argv/BadOptionException.php" name="lib/Horde/Argv/BadOptionException.php" />
<install as="Horde/Argv/Exception.php" name="lib/Horde/Argv/Exception.php" />
Expand Down Expand Up @@ -918,15 +921,16 @@
</release>
<release>
<version>
<release>2.0.13</release>
<api>1.0.0</api></version>
<release>2.1.0</release>
<api>1.1.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-02-01</date>
<date>2017-04-11</date>
<license uri="http://www.horde.org/licenses/bsd">BSD-2-Clause</license>
<notes>
*
* [jan] Colorize output.
* [jan] Add Horde_Argv_HelpFormatter#highlightHeading() and Horde_Argv_HelpFormatter#highlightOption().
</notes>
</release>
</changelog>
Expand Down
29 changes: 21 additions & 8 deletions framework/Argv/test/Horde/Argv/CallbackTest.php
Expand Up @@ -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()
{}
{
}
}
31 changes: 24 additions & 7 deletions framework/Argv/test/Horde/Argv/ConflictOverrideTest.php
Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions framework/Argv/test/Horde/Argv/ConflictTestCase.php
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion framework/Argv/test/Horde/Argv/ExpandDefaultsTest.php
Expand Up @@ -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:
Expand Down
13 changes: 11 additions & 2 deletions framework/Argv/test/Horde/Argv/HelpTest.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 5144ebf

Please sign in to comment.