Skip to content

Commit

Permalink
[jan] Add color() method to colorize output with any supported color.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Mar 6, 2017
1 parent 9253504 commit 6e74d21
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
21 changes: 21 additions & 0 deletions framework/Cli/doc/Horde/Cli/UPGRADING
@@ -0,0 +1,21 @@
=====================
Upgrading Horde_Cli
=====================

: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_Cli

- color()

This method has been added.
3 changes: 3 additions & 0 deletions framework/Cli/doc/Horde/Cli/colors.php
Expand Up @@ -12,6 +12,9 @@
$cli->writeln($cli->yellow('Yellow'));
$cli->writeln($cli->green('Green'));
$cli->writeln($cli->blue('Blue'));
$cli->writeln($cli->color('magenta', 'Magenta'));
$cli->writeln($cli->color('cyan', 'Cyan'));
$cli->writeln($cli->color('lightgray', 'Light Gray'));
$cli->writeln();

/* These messages are automatically colorized based on the message type. */
Expand Down
74 changes: 56 additions & 18 deletions framework/Cli/lib/Horde/Cli.php
Expand Up @@ -60,20 +60,42 @@ class Horde_Cli
*
* @var string
*/
protected $_red_start = '';
protected $_green_start = '';
protected $_yellow_start = '';
protected $_blue_start = '';
protected $_red_start = '';
protected $_green_start = '';
protected $_brown_start = '';
protected $_blue_start = '';
protected $_magenta_start = '';
protected $_cyan_start = '';
protected $_lightgray_start = '';
protected $_white_start = '';
protected $_darkgray_start = '';
protected $_lightred_start = '';
protected $_lightgreen_start = '';
protected $_yellow_start = '';
protected $_lightblue_start = '';
protected $_lightmagenta_start = '';
protected $_lightcyan_start = '';

/**
* The strings to mark the end of coloured text.
*
* @var string
*/
protected $_red_end = '';
protected $_green_end = '';
protected $_yellow_end = '';
protected $_blue_end = '';
protected $_red_end = '';
protected $_green_end = '';
protected $_brown_end = '';
protected $_blue_end = '';
protected $_magenta_end = '';
protected $_cyan_end = '';
protected $_lightgray_end = '';
protected $_white_end = '';
protected $_darkgray_end = '';
protected $_lightred_end = '';
protected $_lightgreen_end = '';
protected $_yellow_end = '';
protected $_lightblue_end = '';
protected $_lightmagenta_end = '';
protected $_lightcyan_end = '';

/**
* Terminal foreground color codes. Not used yet.
Expand Down Expand Up @@ -138,11 +160,11 @@ public function __construct()
if (preg_match('/^(xterm|vt220|linux)/', $term)) {
$this->_clearscreen = "\x1b[2J\x1b[H";
$this->_bold_start = "\x1b[1m";
$this->_red_start = "\x1b[01;31m";
$this->_green_start = "\x1b[01;32m";
$this->_yellow_start = "\x1b[01;33m";
$this->_blue_start = "\x1b[01;34m";
$this->_bold_end = $this->_red_end = $this->_green_end = $this->_yellow_end = $this->_blue_end = "\x1b[0m";
$this->_bold_end = "\x1b[0m";
foreach ($this->_terminalForegrounds as $color => $value) {
$this->{'_' . $color . '_start'} = $value;
$this->{'_' . $color . '_end'} = "\x1b[0m";
}
} elseif (preg_match('/^vt100/', $term)) {
$this->_clearscreen = "\x1b[2J\x1b[H";
$this->_bold_start = "\x1b[1m";
Expand All @@ -155,11 +177,11 @@ public function __construct()

$this->_bold_start = '<strong>';
$this->_bold_end = '</strong>';
$this->_red_start = '<span style="color:red">';
$this->_green_start = '<span style="color:green">';
$this->_yellow_start = '<span style="color:yellow">';
$this->_blue_start = '<span style="color:blue">';
$this->_red_end = $this->_green_end = $this->_yellow_end = $this->_blue_end = '</span>';
foreach (array_keys($this->_terminalForegrounds) as $color) {
$this->{'_' . $color . '_start'} =
'<span style="color:' . $color . '">';
$this->{'_' . $color . '_end'} = '</span>';
}
}

// We really want to call this at the end of the script, not in the
Expand Down Expand Up @@ -217,6 +239,22 @@ public function bold($text)
return $this->_bold_start . $text . $this->_bold_end;
}

/**
* Returns a colored version of $text.
*
* @since Horde_Cli 2.1.0
*
* @param string $color The color to use. @see $_foregroundColors
* @param string $text The text to print in this color.
*
* @return string The colored text.
*/
public function color($color, $text)
{
return $this->{'_' . $color . '_start'} . $text
. $this->{'_' . $color . '_end'};
}

/**
* Returns a red version of $text.
*
Expand Down
20 changes: 12 additions & 8 deletions framework/Cli/package.xml
Expand Up @@ -17,18 +17,18 @@
<email>chuck@horde.org</email>
<active>yes</active>
</lead>
<date>2016-07-12</date>
<date>2017-03-06</date>
<version>
<release>2.0.8</release>
<api>2.0.0</api>
<release>2.1.0</release>
<api>2.1.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Add color() method to colorize output with any supported color.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand All @@ -37,8 +37,10 @@
<dir name="Cli">
<file name="colors.php" role="doc" />
<file name="COPYING" role="doc" />
<file name="UPGRADING" role="doc" />
</dir> <!-- /doc/Horde/Cli -->
</dir> <!-- /doc/Horde -->
<file name="horde.yml" role="doc" />
</dir> <!-- /doc -->
<dir name="lib">
<dir name="Horde">
Expand Down Expand Up @@ -335,8 +337,10 @@
</dependencies>
<phprelease>
<filelist>
<install as="horde.yml" name="doc/horde.yml" />
<install as="Horde/Cli/colors.php" name="doc/Horde/Cli/colors.php" />
<install as="COPYING" name="doc/Horde/Cli/COPYING" />
<install as="UPGRADING" name="doc/Horde/Cli/UPGRADING" />
<install as="Horde/Cli.php" name="lib/Horde/Cli.php" />
<install as="Horde/Cli/Translation.php" name="lib/Horde/Cli/Translation.php" />
<install as="locale/Horde_Cli.pot" name="locale/Horde_Cli.pot" />
Expand Down Expand Up @@ -765,15 +769,15 @@ Add &apos;default&apos; parameter to Horde_CLI::prompt().
</release>
<release>
<version>
<release>2.0.8</release>
<api>2.0.0</api></version>
<release>2.1.0</release>
<api>2.1.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-07-12</date>
<date>2017-03-06</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Add color() method to colorize output with any supported color.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 6e74d21

Please sign in to comment.