Commit 49076b7b authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Fixed issue with CLI ANSI Printer that was displaying incorrect...

[2.0] Fixed issue with CLI ANSI Printer that was displaying incorrect background and font formatting in some situations.
parent 1ddebef8
...@@ -126,9 +126,9 @@ abstract class AbstractPrinter ...@@ -126,9 +126,9 @@ abstract class AbstractPrinter
if (is_string($name)) { if (is_string($name)) {
$name = strtoupper($name); $name = strtoupper($name);
return isset($this->_styles[$name]) ? $this->_styles[$name] : null; return isset($this->_styles[$name]) ? $this->_styles[$name] : null;
} else {
return $name;
} }
return $name;
} }
/** /**
...@@ -174,7 +174,9 @@ abstract class AbstractPrinter ...@@ -174,7 +174,9 @@ abstract class AbstractPrinter
*/ */
public function writeln($message, $style = 'NONE') public function writeln($message, $style = 'NONE')
{ {
return $this->write($message . PHP_EOL, $style); $this->output($this->format($message, $style) . PHP_EOL);
return $this;
} }
/** /**
......
...@@ -64,9 +64,8 @@ class AnsiColorPrinter extends AbstractPrinter ...@@ -64,9 +64,8 @@ class AnsiColorPrinter extends AbstractPrinter
} }
$style = $this->getStyle($style); $style = $this->getStyle($style);
$str = $this->_getForegroundString($style->getForeground()) $str = $this->_getForegroundString($style)
. $this->_getBackgroundString($style->getBackground()) . $this->_getBackgroundString($style);
. $this->_getOptionsString($style->getOptions());
$styleSet = ($str != ''); $styleSet = ($str != '');
return $str . $message . ($styleSet ? chr(27) . '[0m' : ''); return $str . $message . ($styleSet ? chr(27) . '[0m' : '');
...@@ -75,18 +74,20 @@ class AnsiColorPrinter extends AbstractPrinter ...@@ -75,18 +74,20 @@ class AnsiColorPrinter extends AbstractPrinter
/** /**
* Retrieves the ANSI string representation of requested color name * Retrieves the ANSI string representation of requested color name
* *
* @param string $background Background color name * @param Style $style Style
* @return string * @return string
*/ */
protected function _getBackgroundString($background) protected function _getBackgroundString(Style $style)
{ {
$background = $style->getBackground();
if (empty($background)) { if (empty($background)) {
return ''; return '';
} }
$esc = chr(27); $esc = chr(27);
switch ($background) { switch (strtoupper($background)) {
case 'BLACK': case 'BLACK':
return $esc . '[40m'; return $esc . '[40m';
case 'RED': case 'RED':
...@@ -112,55 +113,58 @@ class AnsiColorPrinter extends AbstractPrinter ...@@ -112,55 +113,58 @@ class AnsiColorPrinter extends AbstractPrinter
/** /**
* Retrieves the ANSI string representation of requested color name * Retrieves the ANSI string representation of requested color name
* *
* @param string $foreground Foreground color name * @param Style $style Style
* @return string * @return string
*/ */
protected function _getForegroundString($foreground) protected function _getForegroundString(Style $style)
{ {
$foreground = $style->getForeground();
if (empty($foreground)) { if (empty($foreground)) {
return ''; return '';
} }
$esc = chr(27); $str = chr(27) . '[' . $this->_getOptionsString($style);
switch ($foreground) { switch (strtoupper($foreground)) {
case 'BLACK': case 'BLACK':
return $esc . '[30m'; return $str . '30m';
case 'RED': case 'RED':
return $esc . '[31m'; return $str . '31m';
case 'GREEN': case 'GREEN':
return $esc . '[32m'; return $str . '32m';
case 'YELLOW': case 'YELLOW':
return $esc . '[33m'; return $str . '33m';
case 'BLUE': case 'BLUE':
return $esc . '[34m'; return $str . '34m';
case 'MAGENTA': case 'MAGENTA':
return $esc . '[35m'; return $str . '35m';
case 'CYAN': case 'CYAN':
return $esc . '[36m'; return $str . '36m';
case 'WHITE': case 'WHITE':
return $esc . '[37m'; return $str . '37m';
case 'DEFAULT_FGU': case 'DEFAULT_FGU':
return $esc . '[38m'; return $str . '38m';
case 'DEFAULT': case 'DEFAULT':
default: default:
return $esc . '[39m'; return $str . '39m';
} }
} }
/** /**
* Retrieves the ANSI string representation of requested options * Retrieves the ANSI string representation of requested options
* *
* @param array $options Options * @param Style $style Style
* @return string * @return string
*/ */
protected function _getOptionsString($options) protected function _getOptionsString(Style $style)
{ {
$options = $style->getOptions();
if (empty($options)) { if (empty($options)) {
return ''; return '';
} }
$esc = chr(27);
$str = ''; $str = '';
foreach ($options as $name => $value) { foreach ($options as $name => $value) {
...@@ -169,22 +173,22 @@ class AnsiColorPrinter extends AbstractPrinter ...@@ -169,22 +173,22 @@ class AnsiColorPrinter extends AbstractPrinter
switch ($name) { switch ($name) {
case 'BOLD': case 'BOLD':
$str .= $esc . '[1m'; $str .= '1;';
break; break;
case 'HALF': case 'HALF':
$str .= $esc . '[2m'; $str .= '2;';
break; break;
case 'UNDERLINE': case 'UNDERLINE':
$str .= $esc . '[4m'; $str .= '4;';
break; break;
case 'BLINK': case 'BLINK':
$str .= $esc . '[5m'; $str .= '5;';
break; break;
case 'REVERSE': case 'REVERSE':
$str .= $esc . '[7m'; $str .= '7;';
break; break;
case 'CONCEAL': case 'CONCEAL':
$str .= $esc . '[8m'; $str .= '8;';
break; break;
default: default:
// Ignore unknown option // Ignore unknown option
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment