Skip to content

Commit

Permalink
Enhance terminal reporting and disable colors when not supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Sep 24, 2016
1 parent a0a0690 commit c68e435
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 40 deletions.
1 change: 0 additions & 1 deletion spec/Suite/Cli/Cli.spec.php
Expand Up @@ -39,7 +39,6 @@
$this->check(Cli::color("String", "n;yellow;100"), "\e[0;33;110mSrting\e[0m");
$this->check(Cli::color("String", "4;red;100"), "\e[0;31;110mSrting\e[0m");
$this->check(Cli::color("String", "n;100;100"), "\e[0;100;100mSrting\e[0m");
$this->check(Cli::color("String", "n;light yellow;100"), "\e[0;133;110mSrting\e[0m");

});

Expand Down
4 changes: 2 additions & 2 deletions spec/Suite/Cli/Kahlan.spec.php
Expand Up @@ -113,7 +113,7 @@
/ __ \ (_| | | | | | (_| | | | |
\/ \/\__,_|_| |_|_|\__,_|_| |_|
\033[2;39;49mThe PHP Test Framework for Freedom, Truth, and Justice.\033[0m
\033[0;90;49mThe PHP Test Framework for Freedom, Truth and Justice.\033[0m
version \033[0;32;49m{$version}\033[0m
Expand Down Expand Up @@ -143,7 +143,7 @@
/ __ \ (_| | | | | | (_| | | | |
\/ \/\__,_|_| |_|_|\__,_|_| |_|
\033[2;39;49mThe PHP Test Framework for Freedom, Truth, and Justice.\033[0m
\033[0;90;49mThe PHP Test Framework for Freedom, Truth and Justice.\033[0m
Usage: kahlan [options]
Expand Down
2 changes: 1 addition & 1 deletion spec/Suite/Reporter/Terminal.spec.php
Expand Up @@ -30,7 +30,7 @@
it("returns the baseline", function () {

$actual = $this->terminal->kahlanBaseline();
expect($actual)->toBe("The PHP Test Framework for Freedom, Truth, and Justice.");
expect($actual)->toBe("The PHP Test Framework for Freedom, Truth and Justice.");

});

Expand Down
35 changes: 18 additions & 17 deletions src/Cli/Cli.php
Expand Up @@ -10,15 +10,23 @@ class Cli
*/
protected static $_vt100 = [
'colors' => [
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37,
'default' => 39
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'light-grey' => 37,
'dark-grey' => 90,
'light-red' => 91,
'light-green' => 92,
'light-yellow' => 93,
'light-blue' => 94,
'light-magenta' => 95,
'light-cyan' => 99,
'white' => 97,
'default' => 39
],
'formats' => [
'n' => 0, //normal
Expand Down Expand Up @@ -99,16 +107,9 @@ protected static function _vt100($name)
if (is_numeric($name)) {
return $name;
}
$value = 0;
$items = explode(' ', $name);

if (($name = array_shift($items)) === 'light') {
$value += 100;
$name = array_shift($items);
}

if (isset(static::$_vt100['colors'][$name])) {
$value += static::$_vt100['colors'][$name];
$value = static::$_vt100['colors'][$name];
} else {
$value = 39;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/Kahlan.php
Expand Up @@ -223,7 +223,7 @@ protected function _version()
$terminal = $this->_terminal();
if (!$this->commandLine()->get('no-header')) {
$terminal->write($terminal->kahlan() ."\n\n");
$terminal->write($terminal->kahlanBaseline(), 'd');
$terminal->write($terminal->kahlanBaseline(), 'dark-grey');
$terminal->write("\n\n");
}

Expand All @@ -244,7 +244,7 @@ protected function _help()
$terminal = $this->_terminal();
if (!$this->commandLine()->get('no-header')) {
$terminal->write($terminal->kahlan() ."\n\n");
$terminal->write($terminal->kahlanBaseline(), 'd');
$terminal->write($terminal->kahlanBaseline(), 'dark-grey');
$terminal->write("\n\n");
}
$help = <<<EOD
Expand Down
4 changes: 2 additions & 2 deletions src/Reporter/Dot.php
Expand Up @@ -59,10 +59,10 @@ public function specEnd($log = null)
{
switch ($log->type()) {
case 'passed':
$this->_write('.');
$this->_write($this->_symbols['dot']);
break;
case 'skipped':
$this->_write('S', 'd');
$this->_write('S', 'light-grey');
break;
case 'pending':
$this->_write('P', 'cyan');
Expand Down
66 changes: 52 additions & 14 deletions src/Reporter/Terminal.php
Expand Up @@ -56,6 +56,17 @@ class Terminal extends Reporter
*/
protected $_output = null;

/**
* Default symbol map.
*
* @var array
*/
protected $_symbols = [
'ok' => '✓',
'err' => '✖',
'dot' => '․'
];

/**
* The constructor.
*
Expand All @@ -73,9 +84,36 @@ public function __construct($config = [])
];
$config += $defaults;

$this->_colors = $config['colors'];
$this->_header = $config['header'];
$this->_output = $config['output'];

$this->useColors($config['colors']);

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->_symbols['ok'] = '\u221A';
$this->_symbols['err'] = '\u00D7';
$this->_symbols['dot'] = '.';
}
}

/**
* Enable/disable color
*
* @param boolean $enable A boolean.
*/
public function useColors($enable = true)
{
if (!$enable) {
$this->_colors = false;
return;
}

$term = getenv('TERM');
if (getenv('COLORTERM') || preg_match('~screen|^xterm|^vt100|color|ansi|cygwin|linux~i', $term)) {
$this->_colors = true;
return;
}
$this->_colors = false;
}

/**
Expand All @@ -90,7 +128,7 @@ public function start($args)
return;
}
$this->write($this->kahlan() . "\n\n");
$this->write($this->kahlanBaseline() . "\n", 'd');
$this->write($this->kahlanBaseline() . "\n", 'dark-grey');
$this->write("\nWorking Directory: ", 'blue');
$this->write(getcwd() . "\n");
}
Expand Down Expand Up @@ -118,7 +156,7 @@ public function kahlan()
*/
public function kahlanBaseline()
{
return "The PHP Test Framework for Freedom, Truth, and Justice.";
return "The PHP Test Framework for Freedom, Truth and Justice.";
}

/**
Expand Down Expand Up @@ -176,32 +214,32 @@ protected function _reportSpecMessage($log)

switch ($log->type()) {
case 'passed':
$this->write('✔', 'green');
$this->write($this->_symbols['ok'], 'light-green');
$this->write(' ');
$this->write("{$message}\n", 'd');
$this->write("{$message}\n", 'dark-grey');
break;
case 'skipped':
$this->write('✔', 'd');
$this->write($this->_symbols['ok'], 'light-grey');
$this->write(' ');
$this->write("{$message}\n", 'd');
$this->write("{$message}\n", 'light-grey');
break;
case 'pending':
$this->write('✔', 'cyan');
$this->write($this->_symbols['ok'], 'cyan');
$this->write(' ');
$this->write("{$message}\n", 'cyan');
break;
case 'excluded':
$this->write('✔', 'yellow');
$this->write($this->_symbols['ok'], 'yellow');
$this->write(' ');
$this->write("{$message}\n", 'yellow');
break;
case 'failed':
$this->write('✘', 'red');
$this->write($this->_symbols['err'], 'red');
$this->write(' ');
$this->write("{$message}\n", 'red');
break;
case 'errored':
$this->write('✘', 'red');
$this->write($this->_symbols['err'], 'red');
$this->write(' ');
$this->write("{$message}\n", 'red');
break;
Expand Down Expand Up @@ -438,7 +476,7 @@ public function _reportSummary($summary)
$this->write(", ");
$this->write("{$excluded} Excluded", 'yellow');
$this->write(", ");
$this->write("{$skipped} Skipped", 'd');
$this->write("{$skipped} Skipped", 'light-grey');
$this->write("\n\n");
$this->write('Passed ' . ($passed), 'green');
$this->write(" of {$total} ");
Expand Down Expand Up @@ -499,7 +537,7 @@ protected function _summarizeSkipped($summary)
foreach ([
'pending' => 'cyan',
'excluded' => 'yellow',
'skipped' => '90'
'skipped' => 'light-grey'
] as $type => $color) {
if (!$logs = $summary->logs($type)) {
continue;
Expand All @@ -511,7 +549,7 @@ protected function _summarizeSkipped($summary)
$this->write(ucfirst($type) . " specification" . ($count > 1 ? 's' : '') . ": {$count}\n");

foreach ($logs as $log) {
$this->write("{$log->file()}, line {$log->line()}\n", 'd');
$this->write("{$log->file()}, line {$log->line()}\n", 'dark-grey');
}
$this->prefix('');
$this->write("\n");
Expand Down
7 changes: 6 additions & 1 deletion src/Reporter/Verbose.php
Expand Up @@ -12,6 +12,7 @@ public function start($args)
{
parent::start($args);
$this->write("\n");
$this->_indent++;
}

/**
Expand All @@ -22,8 +23,11 @@ public function start($args)
public function suiteStart($suite = null)
{
$messages = $suite->messages();
if (count($messages) === 2) {
$this->write("\n");
}
$message = end($messages);
$this->write("{$message}\n", "b;");
$this->write("{$message}\n");
$this->_indent++;
}

Expand Down Expand Up @@ -54,6 +58,7 @@ public function specEnd($log = null)
*/
public function end($summary)
{
$this->_indent--;
$this->write("\n");

foreach ($summary->logs() as $log) {
Expand Down

0 comments on commit c68e435

Please sign in to comment.