[2.0] Fixed issue with CLI ANSI Printer that was displaying incorrect background and font formatting in some situations.
This commit is contained in:
parent
1ddebef8a4
commit
49076b7bd4
@ -62,10 +62,10 @@ abstract class AbstractPrinter
|
|||||||
{
|
{
|
||||||
$this->_stream = $stream;
|
$this->_stream = $stream;
|
||||||
$this->_maxColumnSize = 80;
|
$this->_maxColumnSize = 80;
|
||||||
|
|
||||||
$this->_initStyles();
|
$this->_initStyles();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes Printer Styles
|
* Initializes Printer Styles
|
||||||
*
|
*
|
||||||
@ -100,7 +100,7 @@ abstract class AbstractPrinter
|
|||||||
$this->addStyle($name, $style);
|
$this->addStyle($name, $style);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a single Style to Printer.
|
* Add a single Style to Printer.
|
||||||
* Example of inclusion to support a new Style:
|
* Example of inclusion to support a new Style:
|
||||||
@ -115,7 +115,7 @@ abstract class AbstractPrinter
|
|||||||
{
|
{
|
||||||
$this->_styles[strtoupper($name)] = $style;
|
$this->_styles[strtoupper($name)] = $style;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a defined Style.
|
* Retrieves a defined Style.
|
||||||
*
|
*
|
||||||
@ -126,11 +126,11 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the maximum column size (defines the CLI margin).
|
* Sets the maximum column size (defines the CLI margin).
|
||||||
*
|
*
|
||||||
@ -140,7 +140,7 @@ abstract class AbstractPrinter
|
|||||||
{
|
{
|
||||||
$this->_maxColumnSize = $maxColumnSize;
|
$this->_maxColumnSize = $maxColumnSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes to the output stream.
|
* Writes to the output stream.
|
||||||
*
|
*
|
||||||
@ -149,7 +149,7 @@ abstract class AbstractPrinter
|
|||||||
public function output($message)
|
public function output($message)
|
||||||
{
|
{
|
||||||
fwrite($this->_stream, $message);
|
fwrite($this->_stream, $message);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,10 +162,10 @@ abstract class AbstractPrinter
|
|||||||
public function write($message, $style = 'NONE')
|
public function write($message, $style = 'NONE')
|
||||||
{
|
{
|
||||||
$this->output($this->format($message, $style));
|
$this->output($this->format($message, $style));
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a line to the output stream, formatting it by applying the defined style.
|
* Writes a line to the output stream, formatting it by applying the defined style.
|
||||||
*
|
*
|
||||||
@ -174,9 +174,11 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the given message with the defined style.
|
* Formats the given message with the defined style.
|
||||||
*
|
*
|
||||||
|
@ -53,7 +53,7 @@ class AnsiColorPrinter extends AbstractPrinter
|
|||||||
'NONE' => new Style(),
|
'NONE' => new Style(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
@ -62,31 +62,32 @@ class AnsiColorPrinter extends AbstractPrinter
|
|||||||
if ( ! $this->_supportsColor()) {
|
if ( ! $this->_supportsColor()) {
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
$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' : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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,79 +113,82 @@ 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) {
|
||||||
if ($value) {
|
if ($value) {
|
||||||
$name = strtoupper($name);
|
$name = strtoupper($name);
|
||||||
|
|
||||||
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
|
||||||
@ -192,10 +196,10 @@ class AnsiColorPrinter extends AbstractPrinter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the current Output Stream supports ANSI Colors
|
* Checks if the current Output Stream supports ANSI Colors
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user