1
0
mirror of synced 2024-12-13 22:56:04 +03:00

[2.0] Fixed issue with CLI ANSI Printer that was displaying incorrect background and font formatting in some situations.

This commit is contained in:
guilhermeblanco 2010-03-19 03:10:11 +00:00
parent 1ddebef8a4
commit 49076b7bd4
2 changed files with 62 additions and 56 deletions

View File

@ -62,10 +62,10 @@ abstract class AbstractPrinter
{
$this->_stream = $stream;
$this->_maxColumnSize = 80;
$this->_initStyles();
}
/**
* Initializes Printer Styles
*
@ -100,7 +100,7 @@ abstract class AbstractPrinter
$this->addStyle($name, $style);
}
}
/**
* Add a single Style to Printer.
* Example of inclusion to support a new Style:
@ -115,7 +115,7 @@ abstract class AbstractPrinter
{
$this->_styles[strtoupper($name)] = $style;
}
/**
* Retrieves a defined Style.
*
@ -126,11 +126,11 @@ abstract class AbstractPrinter
if (is_string($name)) {
$name = strtoupper($name);
return isset($this->_styles[$name]) ? $this->_styles[$name] : null;
} else {
return $name;
}
return $name;
}
/**
* Sets the maximum column size (defines the CLI margin).
*
@ -140,7 +140,7 @@ abstract class AbstractPrinter
{
$this->_maxColumnSize = $maxColumnSize;
}
/**
* Writes to the output stream.
*
@ -149,7 +149,7 @@ abstract class AbstractPrinter
public function output($message)
{
fwrite($this->_stream, $message);
return $this;
}
@ -162,10 +162,10 @@ abstract class AbstractPrinter
public function write($message, $style = 'NONE')
{
$this->output($this->format($message, $style));
return $this;
}
/**
* 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')
{
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.
*

View File

@ -53,7 +53,7 @@ class AnsiColorPrinter extends AbstractPrinter
'NONE' => new Style(),
));
}
/**
* @inheritdoc
*/
@ -62,31 +62,32 @@ class AnsiColorPrinter extends AbstractPrinter
if ( ! $this->_supportsColor()) {
return $message;
}
$style = $this->getStyle($style);
$str = $this->_getForegroundString($style->getForeground())
. $this->_getBackgroundString($style->getBackground())
. $this->_getOptionsString($style->getOptions());
$str = $this->_getForegroundString($style)
. $this->_getBackgroundString($style);
$styleSet = ($str != '');
return $str . $message . ($styleSet ? chr(27) . '[0m' : '');
}
/**
* Retrieves the ANSI string representation of requested color name
*
* @param string $background Background color name
* @param Style $style Style
* @return string
*/
protected function _getBackgroundString($background)
protected function _getBackgroundString(Style $style)
{
$background = $style->getBackground();
if (empty($background)) {
return '';
}
$esc = chr(27);
switch ($background) {
switch (strtoupper($background)) {
case 'BLACK':
return $esc . '[40m';
case 'RED':
@ -112,79 +113,82 @@ class AnsiColorPrinter extends AbstractPrinter
/**
* Retrieves the ANSI string representation of requested color name
*
* @param string $foreground Foreground color name
* @param Style $style Style
* @return string
*/
protected function _getForegroundString($foreground)
protected function _getForegroundString(Style $style)
{
$foreground = $style->getForeground();
if (empty($foreground)) {
return '';
}
$esc = chr(27);
switch ($foreground) {
$str = chr(27) . '[' . $this->_getOptionsString($style);
switch (strtoupper($foreground)) {
case 'BLACK':
return $esc . '[30m';
return $str . '30m';
case 'RED':
return $esc . '[31m';
return $str . '31m';
case 'GREEN':
return $esc . '[32m';
return $str . '32m';
case 'YELLOW':
return $esc . '[33m';
return $str . '33m';
case 'BLUE':
return $esc . '[34m';
return $str . '34m';
case 'MAGENTA':
return $esc . '[35m';
return $str . '35m';
case 'CYAN':
return $esc . '[36m';
return $str . '36m';
case 'WHITE':
return $esc . '[37m';
return $str . '37m';
case 'DEFAULT_FGU':
return $esc . '[38m';
return $str . '38m';
case 'DEFAULT':
default:
return $esc . '[39m';
return $str . '39m';
}
}
/**
* Retrieves the ANSI string representation of requested options
*
* @param array $options Options
* @param Style $style Style
* @return string
*/
protected function _getOptionsString($options)
protected function _getOptionsString(Style $style)
{
$options = $style->getOptions();
if (empty($options)) {
return '';
}
$esc = chr(27);
$str = '';
foreach ($options as $name => $value) {
if ($value) {
$name = strtoupper($name);
switch ($name) {
case 'BOLD':
$str .= $esc . '[1m';
$str .= '1;';
break;
case 'HALF':
$str .= $esc . '[2m';
$str .= '2;';
break;
case 'UNDERLINE':
$str .= $esc . '[4m';
$str .= '4;';
break;
case 'BLINK':
$str .= $esc . '[5m';
$str .= '5;';
break;
case 'REVERSE':
$str .= $esc . '[7m';
$str .= '7;';
break;
case 'CONCEAL':
$str .= $esc . '[8m';
$str .= '8;';
break;
default:
// Ignore unknown option
@ -192,10 +196,10 @@ class AnsiColorPrinter extends AbstractPrinter
}
}
}
return $str;
}
/**
* Checks if the current Output Stream supports ANSI Colors
*