Skip to content

Commit

Permalink
1. update color control
Browse files Browse the repository at this point in the history
  • Loading branch information
scarwu committed Dec 11, 2018
1 parent 92ad6d6 commit 55e8c13
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 96 deletions.
80 changes: 40 additions & 40 deletions example/CLI/tasks/ColorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,63 @@ public function run()
$this->io->write('X', 'black');
$this->io->write('X', 'red');
$this->io->write('X', 'green');
$this->io->write('X', 'brown');
$this->io->write('X', 'yellow');
$this->io->write('X', 'blue');
$this->io->write('X', 'purple');
$this->io->write('X', 'magenta');
$this->io->write('X', 'cyan');
$this->io->write('X', 'light_gray');

$this->io->write('X', 'dark_gray');
$this->io->write('X', 'light_red');
$this->io->write('X', 'light_green');
$this->io->write('X', 'yellow');
$this->io->write('X', 'light_blue');
$this->io->write('X', 'light_purple');
$this->io->write('X', 'light_cyan');
$this->io->write('X', 'white');
$this->io->write('X', 'default');
$this->io->write('X', 'brightBlack');
$this->io->write('X', 'brightRed');
$this->io->write('X', 'brightGreen');
$this->io->write('X', 'brightYellow');
$this->io->write('X', 'brightBlue');
$this->io->write('X', 'brightMagenta');
$this->io->write('X', 'brightCyan');
$this->io->write('X', 'brightWhite');

$this->io->writeln();

// Background Colors Only
$this->io->write('X', null, 'black');
$this->io->write('X', null, 'red');
$this->io->write('X', null, 'green');
$this->io->write('X', null, 'brown');
$this->io->write('X', null, 'yellow');
$this->io->write('X', null, 'blue');
$this->io->write('X', null, 'purple');
$this->io->write('X', null, 'magenta');
$this->io->write('X', null, 'cyan');
$this->io->write('X', null, 'light_gray');

$this->io->write('X', null, 'dark_gray');
$this->io->write('X', null, 'light_red');
$this->io->write('X', null, 'light_green');
$this->io->write('X', null, 'yellow');
$this->io->write('X', null, 'light_blue');
$this->io->write('X', null, 'light_purple');
$this->io->write('X', null, 'light_cyan');
$this->io->write('X', null, 'white');
$this->io->write('X', null, 'default');
$this->io->write('X', null, 'brightBlack');
$this->io->write('X', null, 'brightRed');
$this->io->write('X', null, 'brightGreen');
$this->io->write('X', null, 'brightYellow');
$this->io->write('X', null, 'brightBlue');
$this->io->write('X', null, 'brightMagenta');
$this->io->write('X', null, 'brightCyan');
$this->io->write('X', null, 'brightWhite');

$this->io->writeln();

// Text & Background Colors
$this->io->write('X', 'white', 'black');
$this->io->write('X', 'light_cyan', 'red');
$this->io->write('X', 'light_purple', 'green');
$this->io->write('X', 'light_blue', 'brown');
$this->io->write('X', 'yellow', 'blue');
$this->io->write('X', 'light_green', 'purple');
$this->io->write('X', 'light_red', 'cyan');
$this->io->write('X', 'dark_gray', 'light_gray');

$this->io->write('X', 'light_gray', 'dark_gray');
$this->io->write('X', 'cyan', 'light_red');
$this->io->write('X', 'purple', 'light_green');
$this->io->write('X', 'blue', 'yellow');
$this->io->write('X', 'brown', 'light_blue');
$this->io->write('X', 'green', 'light_purple');
$this->io->write('X', 'red', 'light_cyan');
$this->io->write('X', 'black', 'white');
$this->io->write('X', 'black', 'brightWhite');
$this->io->write('X', 'red', 'brightCyan');
$this->io->write('X', 'green', 'brightMagenta');
$this->io->write('X', 'yellow', 'brightBlue');
$this->io->write('X', 'blue', 'brightYellow');
$this->io->write('X', 'magenta', 'brightGreen');
$this->io->write('X', 'cyan', 'brightRed');
$this->io->write('X', 'white', 'brightBlack');
$this->io->write('X', 'default', 'default');
$this->io->write('X', 'brightBlack', 'white');
$this->io->write('X', 'brightRed', 'cyan');
$this->io->write('X', 'brightGreen', 'magenta');
$this->io->write('X', 'brightYellow', 'blue');
$this->io->write('X', 'brightBlue', 'yellow');
$this->io->write('X', 'brightMagenta', 'green');
$this->io->write('X', 'brightCyan', 'red');
$this->io->write('X', 'brightWhite', 'black');

$this->io->writeln();
}
}
}
127 changes: 71 additions & 56 deletions src/Oni/CLI/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,59 @@ class IO extends Basic
*/
private $_configs = [];

// ANSI Escape Code
const ESC = "\x1b";
const CSI = self::ESC . '[';
const OSC = self::ESC . ']';

// Control Charater
const BEL = "\x07";
const SEP = ';';

/**
* @var array
*/
private static $textColor = [
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'brown' => '0;33',
'blue' => '0;34',
'purple' => '0;35',
'cyan' => '0;36',
'light_gray' => '0;37',

'dark_gray' => '1;30',
'light_red' => '1;31',
'light_green' => '1;32',
'yellow' => '1;33',
'light_blue' => '1;34',
'light_purple' => '1;35',
'light_cyan' => '1;36',
'white' => '1;37'
private static $fgColorMapping = [
'black' => '30',
'red' => '31',
'green' => '32',
'yellow' => '33',
'blue' => '34',
'magenta' => '35',
'cyan' => '36',
'white' => '37',
'default' => '39',
'brightBlack' => '90',
'brightRed' => '91',
'brightGreen' => '92',
'brightYellow' => '93',
'brightBlue' => '94',
'brightMagenta' => '95',
'brightCyan' => '96',
'brightWhite' => '97'
];

/**
* @var array
*/
private static $bgColor = [
'black' => '0;40',
'red' => '0;41',
'green' => '0;42',
'brown' => '0;43',
'blue' => '0;44',
'purple' => '0;45',
'cyan' => '0;46',
'light_gray' => '0;47',

'dark_gray' => '1;40',
'light_red' => '1;41',
'light_green' => '1;42',
'yellow' => '1;43',
'light_blue' => '1;44',
'light_purple' => '1;45',
'light_cyan' => '1;46',
'white' => '1;47'
private static $bgColorMapping = [
'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'white' => '47',
'default' => '49',
'brightBlack' => '100',
'brightRed' => '101',
'brightGreen' => '102',
'brightYellow' => '103',
'brightBlue' => '104',
'brightMagenta' => '105',
'brightCyan' => '106',
'brightWhite' => '107'
];

/**
Expand Down Expand Up @@ -258,12 +267,12 @@ public function read()
*
* @param string $text
* @param function $callback
* @param string $textColor
* @param string $fgColor
* @param string $bgColor
*
* @return string|bool
*/
public function ask($text, $callback = null, $textColor = null, $bgColor = null)
public function ask($text, $callback = null, $fgColor = null, $bgColor = null)
{
if (null === $callback) {
$callback = function() {
Expand All @@ -272,7 +281,7 @@ public function ask($text, $callback = null, $textColor = null, $bgColor = null)
}

do {
$this->write($text, $textColor, $bgColor);
$this->write($text, $fgColor, $bgColor);
} while (false === $callback($answer = $this->read()));

return $answer;
Expand Down Expand Up @@ -388,37 +397,43 @@ public function menuSelect($options) {
* Color
*
* @param string $text
* @param string $textColor
* @param string $fgColor
* @param string $bgColor
*
* @return string
*/
private function color($text, $textColor = null, $bgColor = null)
private function color($text, $fgColor = null, $bgColor = null)
{
if (isset(self::$textColor[$textColor])) {
$color = self::$textColor[$textColor];
$text = "\033[{$color}m{$text}\033[m";
$startCodes = [];
$stopCodes = [];

if (isset(self::$fgColorMapping[$fgColor])) {
$startCodes[] = self::$fgColorMapping[$fgColor];
$stopCodes[] = 39;
}

if (isset(self::$bgColor[$bgColor])) {
$color = self::$bgColor[$bgColor];
$text = "\033[{$color}m{$text}\033[m";
if (isset(self::$bgColorMapping[$bgColor])) {
$startCodes[] = self::$bgColorMapping[$bgColor];
$stopCodes[] = 49;
}

return $text;
$start = self::CSI . implode(';', $startCodes) . 'm';
$end = self::CSI . implode(';', $stopCodes) . 'm';

return "{$start}{$text}{$end}";
}

/**
* Write data to STDOUT
*
* @param string $text
* @param string $textColor
* @param string $fgColor
* @param string $bgColor
*/
public function write($text, $textColor = null, $bgColor = null)
public function write($text, $fgColor = null, $bgColor = null)
{
if (null !== $textColor || null !== $bgColor) {
$text = $this->color($text, $textColor, $bgColor);
if (null !== $fgColor || null !== $bgColor) {
$text = $this->color($text, $fgColor, $bgColor);
}

fwrite(STDOUT, $text);
Expand All @@ -431,9 +446,9 @@ public function write($text, $textColor = null, $bgColor = null)
* @param string $bgColor
* @param string $bgColor
*/
public function writeln($text = '', $textColor = null, $bgColor = null)
public function writeln($text = '', $fgColor = null, $bgColor = null)
{
$this->write("{$text}\n", $textColor, $bgColor);
$this->write("{$text}\n", $fgColor, $bgColor);
}

/**
Expand Down Expand Up @@ -473,7 +488,7 @@ public function notice($text)
*/
public function info($text)
{
$this->write("{$text}\n", 'dark_gray');
$this->write("{$text}\n", 'brightBlack');
}

/**
Expand All @@ -483,7 +498,7 @@ public function info($text)
*/
public function debug($text)
{
$this->write("{$text}\n", 'light_gray');
$this->write("{$text}\n", 'white');
}

/**
Expand Down

0 comments on commit 55e8c13

Please sign in to comment.