예제 #1
0
 /**
  * @dataProvider textStyleExceptionProvider
  *
  * @expectedException Jm_Console_TextStyleException
  */
 public function testTextStyleException($style)
 {
     $style = Jm_Console_TextStyle::fromString($style);
 }
예제 #2
0
 /**
  * Wraps $message into ANSI escape sequences.
  *
  * @param string                      $message The string to wrap
  * @param Jm_Console_Output_TextStyle $style   The text style
  *
  * @return string
  * @todo move to Jm_Console_TextStyle
  */
 public function colorize($message, $style)
 {
     if (is_string($style)) {
         $style = Jm_Console_TextStyle::fromString($style);
     } else {
         if (!is_a($style, 'Jm_Console_TextStyle')) {
             throw new InvalidArgumentException(sprintf('$style expected to be a Jm_Console_TextStyle or a string. ' . '%s found', gettype($style)));
         }
     }
     // on non ansi terminals or when ansi has been explicitely disabled
     // we no styling is required
     if ($this->ansiEnabled !== TRUE || !$this->assumeIsatty()) {
         return $message;
     }
     // if all style attriubtes set to reset disable styling
     if ($style->getForegroundColor() === Jm_Console_TextStyle::DEFAULT_COLOR && $style->getBackgroundColor() === Jm_Console_TextStyle::DEFAULT_COLOR && $style->getTextDecoration() === Jm_Console_TextStyle::NO_DECORATIONS) {
         return $message;
     }
     // wrap the message into an ANSI escape sequence
     // in order to colorize the string
     $codes = array();
     if ($style->getForegroundColor() !== Jm_Console_TextStyle::DEFAULT_COLOR) {
         $codes[] = '3' . $style->getForegroundColor();
     }
     if ($style->getBackgroundColor() !== Jm_Console_TextStyle::DEFAULT_COLOR) {
         $codes[] = '4' . $style->getBackgroundColor();
     }
     if ($style->getTextDecoration() !== Jm_Console_TextStyle::NO_DECORATIONS) {
         $codes[] = $style->getTextDecoration();
     }
     $ansi = "[" . implode(';', $codes) . 'm';
     $ansi .= $message;
     $ansi .= "";
     return $ansi;
 }