예제 #1
0
 /**
  * @dataProvider textStyleExceptionProvider
  *
  * @expectedException Jm_Console_TextStyleException
  */
 public function testTextStyleException($style)
 {
     $style = Jm_Console_TextStyle::fromString($style);
 }
예제 #2
0
<?php

require_once 'Jm/Autoloader.php';
$fcolors = array(Jm_Console_TextStyle::BLACK, Jm_Console_TextStyle::RED, Jm_Console_TextStyle::GREEN, Jm_Console_TextStyle::YELLOW, Jm_Console_TextStyle::BLUE, Jm_Console_TextStyle::PURPLE, Jm_Console_TextStyle::CYAN, Jm_Console_TextStyle::WHITE, Jm_Console_TextStyle::DEFAULT_COLOR);
$bcolors = $fcolors;
$decorations = array(Jm_Console_TextStyle::BOLD, Jm_Console_TextStyle::LIGHT, Jm_Console_TextStyle::ITALIC, Jm_Console_TextStyle::UNDERLINE, Jm_Console_TextStyle::BLINK, Jm_Console_TextStyle::REVERSE, Jm_Console_TextStyle::HIDDEN, Jm_Console_TextStyle::NO_DECORATIONS);
$console = Jm_Console::singleton();
foreach ($fcolors as $fcolor) {
    foreach ($bcolors as $bcolor) {
        foreach ($decorations as $decoration) {
            $style = new Jm_Console_TextStyle($fcolor, $bcolor, $decoration);
            $message = $style->__toString();
            $console->writeln($message, $style);
        }
    }
}
예제 #3
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;
 }
예제 #4
0
 /**
  * Default text style parser
  *
  * @param string               $string The string with the style properties
  * @param Jm_Console_TextStyle $style  The style which properites
  *                                     should be set
  *
  * @return void
  */
 protected function defaultTextStyleParser($string, $style)
 {
     foreach (explode(',', $string) as $statement) {
         $statement = trim($statement);
         $keyval = explode(':', $statement);
         if (count($keyval) < 2) {
             // it's a simple statement
             if (in_array($statement, array('black', 'red', 'green', 'yellow', 'blue', 'purple', 'cyan', 'white', 'default'))) {
                 $style->setForegroundColor($this->colornames[$statement]);
                 continue;
             } elseif (in_array($statement, array('bold', 'light', 'italic', 'underline', 'blink_slow', 'blink_rapid', 'blink', 'reverse', 'hidden'))) {
                 $style->setTextDecoration($this->decorations[$statement]);
                 continue;
             }
             // if its not a color or a text decoration it is an error
             throw new Jm_Console_TextStyleException('Failed to parse the style identifier \'' . $string . '\'' . '. Unknown statement \'' . $statement . '\'');
         }
         // fully qualified statemens have a key and a value
         // separated by a ':'
         list($key, $value) = $keyval;
         switch ($key) {
             case 'fg':
                 if (!isset($this->colornames[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown foreground color value \'%s\'', $string, $value));
                 }
                 $style->setForegroundColor($this->colornames[$value]);
                 break;
             case 'bg':
                 if (!isset($this->colornames[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text decoration value \'%s\'', $string, $value));
                 }
                 $style->setBackgroundColor($this->colornames[$value]);
                 break;
             case 'td':
                 if (!isset($this->decorations[$value])) {
                     throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text decoration value \'%s\'', $string, $value));
                 }
                 $style->setTextDecoration($this->decorations[$value]);
                 break;
             default:
                 // if we reached this point something failed
                 throw new Jm_Console_TextStyleException(sprintf('Failed to parse the style identifier \'%s\'' . '. Unknown text style property \'%s\'', $string, $key));
         }
     }
 }