Friday, February 4, 2011

Add Color to PHP Echo in CLI

I adapted this PHP script from If Not True Then False for CentOS escape codes, which use a semi-colon and not a comma between bold and color codes for foreground color escaping.

[Update Sep 3, 2012] This code is now available on github as part of the Box Bart project.

The newer code has __callStatic magic letting you type EscapeColors::red('Some warning message'); in place of the wordier (and less obvious) EscapeColors::fg_color('red', 'Some warning message');


/**
 * Color escapes for bash output
 */
class Escape_Colors
{
 private static $foreground = array(
  'black' => '0;30',
  'dark_gray' => '1;30',
  'red' => '0;31',
  'bold_red' => '1;31',
  'green' => '0;32',
  'bold_green' => '1;32',
  'brown' => '0;33',
  'yellow' => '1;33',
  'blue' => '0;34',
  'bold_blue' => '1;34',
  'purple' => '0;35',
  'bold_purple' => '1;35',
  'cyan' => '0;36',
  'bold_cyan' => '1;36',
  'white' => '1;37',
  'bold_gray' => '0;37',
 );

 private static $background = array(
  'black' => '40',
  'red' => '41',
  'magenta' => '45',
  'yellow' => '43',
  'green' => '42',
  'blue' => '44',
  'cyan' => '46',
  'light_gray' => '47',
 );

 /**
  * Make string appear in color
  */
 public static function fg_color($color, $string)
 {
  if (!isset(self::$foreground[$color]))
  {
   throw new Exception('Foreground color is not defined');
  }

  return "\033[" . self::$foreground[$color] . "m" . $string . "\033[0m";
 }

 /**
  * Make string appear with background color
  */
 public static function bg_color($color, $string)
 {
  if (!isset(self::$background[$color]))
  {
   throw new Exception('Background color is not defined');
  }

  return "\033[" . self::$background[$color] . 'm' . $string . "\033[0m";
 }

 /**
  * See what they all look like
  */
 public static function all_fg()
 {
  foreach (self::$foreground as $color => $code)
  {
   echo "$color - " . self::fg_color($color, 'Hello, world!') . PHP_EOL;
  }
 }

 /**
  * See what they all look like
  */
 public static function all_bg()
 {
  foreach (self::$background as $color => $code)
  {
   echo "$color - " . self::bg_color($color, 'Hello, world!') . PHP_EOL;
  }
 }
}


13 comments:

  1. Hence, the developers must compare similar PHP functions, and choose the one that does not affect the performance of the web application negatively and consume additional resources. https://medijo.lt/plakatai/

    ReplyDelete
  2. Often programmers find it daunting to understand and modify the code written by others. Hence, they need extra time to maintain and update the PHP applications efficiently.WordPress design Toronto

    ReplyDelete
  3. Hi Very Nice Blog I Have Read Your Post It Is Very Informative And Useful Thanks For Posting And Sharing With Us.
    Carpet Cleaning Brisbane
    Carpet Cleaners Brisbane

    ReplyDelete
  4. Thanks for sharing this information. Keep on updating us regularly.
    Pittosporum
    Magnolia

    ReplyDelete
  5. I’m amazed, I have to admit. Seldom do I come across a blog that’s both educative and interesting, and without a doubt, you have hit the nail on the head. The problem is something that not enough folks are speaking intelligently about. Now i'm very happy that I came across this during my search for something relating to this.
    Click here to get More information.

    ReplyDelete
  6. A good blog always comes-up with new and exciting information and while reading .I have feel that this blog is really have all those quality that qualify a blog to be a one. If you are looking for dental clinic Bundoora.Please visit us: Dentist Bundoora

    ReplyDelete
  7. Hi Very Nice Blog I Have Read Your Post It Is Very Informative And Useful Thanks For Posting And Sharing With Us.
    Bond Cleaning Melbourne
    Window Cleaning Melbourne

    ReplyDelete
  8. Great Article .Thank you so much for this useful article. Wishing you the best of luck for all your blogging efforts.
    Camberwell Toyota | Car Mechanic

    ReplyDelete