Skip to content

Commit

Permalink
Added --no-color option
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Dec 28, 2014
1 parent f2ea793 commit 9aee15b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
11 changes: 11 additions & 0 deletions spec/Console/ConsoleFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,15 @@
expect($formattedText)->toEqual("\x1b[3mtest\x1b[23m");
});
});

context('disableANSI', function() {
it('disables formatting using ANSI escape codes', function() {
$str = 'test';
$formatter = new ConsoleFormatter;
$formatter->disableANSI();

expect($formatter->green($str))->toEqual($str);
expect($formatter->italic($str))->toEqual($str);
});
});
});
3 changes: 2 additions & 1 deletion spec/Console/ConsoleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
'stop' => true,
'version' => false,
'watch' => false,
'namespace' => false
'namespace' => false,
'no-color' => false
]);
});

Expand Down
3 changes: 1 addition & 2 deletions src/Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ class Console
'version' => ['--version', '-v', 'Display version number'],
'watch' => ['--watch', '-w', 'Watch files for changes and rerun specs'],
'namespace' => ['--namespace', '-n', 'Only use namespaced functions'],
'no-color' => ['--no-color', '-C', 'Disable terminal colors'],

// TODO: Implement options below
// 'no-color' => ['--no-color', '-n', 'Disable terminal colors'],
// 'generate' => ['--generate', '-g', 'Generate suites for classes in path', 'path'],
// 'processes' => ['--processes', '-p', 'Number of processes to use', 'processes'],
// 'verbose' => ['--verbose', '-V', 'Enable verbose output']
];
Expand Down
71 changes: 43 additions & 28 deletions src/Console/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,48 @@
namespace pho\Console;

/**
* Console formater class
* Console formatter class
*
* @package pho\Console
* @method string black(string $input) Set string color for console output to black
* @method string grey(string $input) Set string color for console output to grey
* @method string red(string $input) Set string color for console output to red
* @method string green(string $input) Set string color for console output to green
* @method string cyan(string $input) Set string color for console output to cyan
* @method string yellow(string $input) Set string color for console output to yellow
* @method string white(string $input) Set string color for console output to white
* @method string bold(string $input) Set string style for console output to bold
* @method string italic(string $input) Set string style for console output to italic
* @method void disableColors() Disable color output
* @method string black(string $input) Set string color for output to black
* @method string grey(string $input) Set string color for output to grey
* @method string red(string $input) Set string color for output to red
* @method string green(string $input) Set string color for output to green
* @method string cyan(string $input) Set string color for output to cyan
* @method string yellow(string $input) Set string color for output to yellow
* @method string white(string $input) Set string color for output to white
* @method string bold(string $input) Set string style for output to bold
* @method string italic(string $input) Set string style for output to italic
*/
class ConsoleFormatter
{
private static $foregroundColours = [
'black' => ["\033[30m", "\033[0m"],
'grey' => ["\033[90m", "\033[0m"],
'red' => ["\033[31m", "\033[0m"],
'green' => ["\033[32m", "\033[0m"],
'cyan' => ["\033[36m", "\033[0m"],
'yellow' => ["\033[33m", "\033[0m"],
'white' => ["\033[37m", "\033[0m"],
private static $foregroundColors = [
'black' => ["\033[30m", "\033[0m"],
'grey' => ["\033[90m", "\033[0m"],
'red' => ["\033[31m", "\033[0m"],
'green' => ["\033[32m", "\033[0m"],
'cyan' => ["\033[36m", "\033[0m"],
'yellow' => ["\033[33m", "\033[0m"],
'white' => ["\033[37m", "\033[0m"],
];

private static $styles = [
'bold' => ["\x1b[1m", "\x1b[22m"],
'italic' => ["\x1b[3m", "\x1b[23m"],
];

private $enabled = true;

/**
* Disables string formatting using ANSI escape sequences. After being
* invoked, any calls to a color or style function will result in the plain
* string being returned.
*/
public function disableANSI() {
$this->enabled = false;
}

/**
* Given a multidimensional array, formats the text such that each entry
* is left aligned with all other entries in the given column. The method
Expand Down Expand Up @@ -74,16 +86,16 @@ public function alignText($array, $delimiter = '')
}

/**
* Sets the text colour to one of those defined in $foregroundColours.
* Sets the text color to one of those defined in $foregroundColors.
*
* @param string $colour A colour corresponding to one of the keys in the
* $foregroundColours array
* @param string $color A color corresponding to one of the keys in the
* $foregroundColors array
* @param string $text The text to be modified
* @return string The original text surrounded by ANSI escape codes
*/
private function applyForeground($colour, $text)
private function applyForeground($color, $text)
{
list($startCode, $endCode) = self::$foregroundColours[$colour];
list($startCode, $endCode) = self::$foregroundColors[$color];

return $startCode . $text . $endCode;
}
Expand All @@ -104,19 +116,22 @@ private function applyStyle($style, $text)
}

/**
* Applies the passed text colour or style to the string.
* Applies the passed text color or style to the string. If disabled,
* it simply returns the passed string.
*
* @param string $method A colour corresponding to one of the keys in the
* $foregroundColours array
* @param string $method A color corresponding to one of the keys in the
* $foregroundColors array
* @param array $args An array with a single element: the text to modify
* @return string The original text surrounded by ANSI escape codes
*
* @throws \Exception If $method doesn't correspond to any of the text
* colours or styles defined in this class
* colors or styles defined in this class
*/
public function __call($method, $args)
{
if (array_key_exists($method, self::$foregroundColours)) {
if (!$this->enabled) {
return $args[0];
} elseif (array_key_exists($method, self::$foregroundColors)) {
return $this->applyForeground($method, $args[0]);
} elseif (array_key_exists($method, self::$styles)) {
return $this->applyStyle($method, $args[0]);
Expand Down
5 changes: 5 additions & 0 deletions src/pho.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ function expect($actual)
$console = new Console(array_slice($argv, 1), 'php://stdout');
$console->parseArguments();

// Disable color output if necessary
if ($console->options['no-color']) {
$console->formatter->disableANSI();
}

// Exit if necessary
if ($console->getErrorStatus() !== null) {
exit($console->getErrorStatus());
Expand Down

0 comments on commit 9aee15b

Please sign in to comment.