Esempio n. 1
0
 /**
  * Converts colorcodes in the format %y (for yellow) into ansi-control
  * codes. The conversion table is: ('bold' meaning 'light' on some
  * terminals). It's almost the same conversion table irssi uses.
  * <pre> 
  *                  text      text            background
  *      ------------------------------------------------
  *      %k %K %0    black     dark grey       black
  *      %r %R %1    red       bold red        red
  *      %g %G %2    green     bold green      green
  *      %y %Y %3    yellow    bold yellow     yellow
  *      %b %B %4    blue      bold blue       blue
  *      %m %M %5    magenta   bold magenta    magenta
  *      %p %P       magenta (think: purple)
  *      %c %C %6    cyan      bold cyan       cyan
  *      %w %W %7    white     bold white      white
  *
  *      %F     Blinking, Flashing
  *      %U     Underline
  *      %8     Reverse
  *      %_,%9  Bold
  *
  *      %n     Resets the color
  *      %%     A single %
  * </pre>
  * First param is the string to convert, second is an optional flag if
  * colors should be used. It defaults to true, if set to false, the
  * colorcodes will just be removed (And %% will be transformed into %)
  *
  * @param string $string  String to convert
  * @param bool   $colored Should the string be colored?
  *
  * @access public
  * @return string
  */
 function convert($string, $colored = true)
 {
     static $conversions = array('%y' => array('color' => 'yellow'), '%g' => array('color' => 'green'), '%b' => array('color' => 'blue'), '%r' => array('color' => 'red'), '%p' => array('color' => 'purple'), '%m' => array('color' => 'purple'), '%c' => array('color' => 'cyan'), '%w' => array('color' => 'grey'), '%k' => array('color' => 'black'), '%n' => array('color' => 'reset'), '%Y' => array('color' => 'yellow', 'style' => 'light'), '%G' => array('color' => 'green', 'style' => 'light'), '%B' => array('color' => 'blue', 'style' => 'light'), '%R' => array('color' => 'red', 'style' => 'light'), '%P' => array('color' => 'purple', 'style' => 'light'), '%M' => array('color' => 'purple', 'style' => 'light'), '%C' => array('color' => 'cyan', 'style' => 'light'), '%W' => array('color' => 'grey', 'style' => 'light'), '%K' => array('color' => 'black', 'style' => 'light'), '%N' => array('color' => 'reset', 'style' => 'light'), '%3' => array('background' => 'yellow'), '%2' => array('background' => 'green'), '%4' => array('background' => 'blue'), '%1' => array('background' => 'red'), '%5' => array('background' => 'purple'), '%6' => array('background' => 'cyan'), '%7' => array('background' => 'grey'), '%0' => array('background' => 'black'), '%F' => array('style' => 'blink'), '%U' => array('style' => 'underline'), '%8' => array('style' => 'inverse'), '%9' => array('style' => 'bold'), '%_' => array('style' => 'bold'));
     if ($colored) {
         $string = str_replace('%%', '% ', $string);
         foreach ($conversions as $key => $value) {
             $string = str_replace($key, Console_Color::color($value), $string);
         }
         $string = str_replace('% ', '%', $string);
     } else {
         $string = preg_replace('/%((%)|.)/', '$2', $string);
     }
     return $string;
 }