Skip to content

Commit

Permalink
[Console] Added getters to output formatter style (and its interface).
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Mar 16, 2012
1 parent 48e6b49 commit 93ffe54
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php
Expand Up @@ -102,6 +102,22 @@ public function setForeground($color = null)
$this->foreground = static::$availableForegroundColors[$color];
}

/**
* Gets style foreground color.
*
* @return string|null
*/
public function getForeground()
{
if (null === $this->foreground) {
return null;
}

$names = array_flip(static::$availableForegroundColors);

return $names[$this->foreground];
}

/**
* Sets style background color.
*
Expand Down Expand Up @@ -130,6 +146,22 @@ public function setBackground($color = null)
$this->background = static::$availableBackgroundColors[$color];
}

/**
* Gets style background color.
*
* @return string|null
*/
public function getBackground()
{
if (null === $this->background) {
return null;
}

$names = array_flip(static::$availableBackgroundColors);

return $names[$this->background];
}

/**
* Sets some specific style option.
*
Expand Down Expand Up @@ -192,6 +224,23 @@ public function setOptions(array $options)
}
}

/**
* Gets specific style options.
*
* @return array
*/
public function getOptions()
{
$names = array_flip(static::$availableOptions);
$options = array();

foreach ($this->options as $code) {
$options[] = $names[$code];
}

return $options;
}

/**
* Applies the style to a given text.
*
Expand All @@ -200,6 +249,16 @@ public function setOptions(array $options)
* @return string
*/
public function apply($text)
{
return sprintf("%s%s\033[0m", $this->getTerminalSequence(), $text);
}

/**
* Gets terminal colorization sequence.
*
* @return string
*/
public function getTerminalSequence()
{
$codes = array();

Expand All @@ -216,6 +275,6 @@ public function apply($text)
$codes = array(0);
}

return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text);
return sprintf("\033[%sm", implode(';', $codes));
}
}
Expand Up @@ -29,6 +29,13 @@ interface OutputFormatterStyleInterface
*/
function setForeground($color = null);

/**
* Gets style foreground color.
*
* @return string|null
*/
function getForeground();

/**
* Sets style background color.
*
Expand All @@ -38,6 +45,13 @@ function setForeground($color = null);
*/
function setBackground($color = null);

/**
* Gets style background color.
*
* @return string|null
*/
function getBackground();

/**
* Sets some specific style option.
*
Expand All @@ -61,6 +75,20 @@ function unsetOption($option);
*/
function setOptions(array $options);

/**
* Gets specific style options.
*
* @return array
*/
function getOptions();

/**
* Gets terminal colorization sequence.
*
* @return string
*/
function getTerminalSequence();

/**
* Applies the style to a given text.
*
Expand Down

0 comments on commit 93ffe54

Please sign in to comment.