Advertisement
Guest User

CLI Colors

a guest
Feb 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2.  
  3.     class Colors {
  4.         private $foreground = array();
  5.         private $background = array();
  6.  
  7.         public function __construct() {
  8.             // Set up shell colors
  9.             $this->foreground_colors['black'] = '0;30';
  10.             $this->foreground_colors['dark_gray'] = '1;30';
  11.             $this->foreground_colors['blue'] = '0;34';
  12.             $this->foreground_colors['light_blue'] = '1;34';
  13.             $this->foreground_colors['green'] = '0;32';
  14.             $this->foreground_colors['light_green'] = '1;32';
  15.             $this->foreground_colors['cyan'] = '0;36';
  16.             $this->foreground_colors['light_cyan'] = '1;36';
  17.             $this->foreground_colors['red'] = '0;31';
  18.             $this->foreground_colors['light_red'] = '1;31';
  19.             $this->foreground_colors['purple'] = '0;35';
  20.             $this->foreground_colors['light_purple'] = '1;35';
  21.             $this->foreground_colors['brown'] = '0;33';
  22.             $this->foreground_colors['yellow'] = '1;33';
  23.             $this->foreground_colors['light_gray'] = '0;37';
  24.             $this->foreground_colors['white'] = '1;37';
  25.  
  26.             $this->background_colors['black'] = '40';
  27.             $this->background_colors['red'] = '41';
  28.             $this->background_colors['green'] = '42';
  29.             $this->background_colors['yellow'] = '43';
  30.             $this->background_colors['blue'] = '44';
  31.             $this->background_colors['magenta'] = '45';
  32.             $this->background_colors['cyan'] = '46';
  33.             $this->background_colors['light_gray'] = '47';
  34.         }
  35.  
  36.         // Returns colored string
  37.         public function getColoredString($string, $foreground = null, $background = null) {
  38.  
  39.             $colored_string = "";
  40.  
  41.             // Check if given foreground color found
  42.             if (isset($this->foreground_colors[$foreground])) {
  43.                 $colored_string .= "\033[" . $this->foreground_colors[$foreground] . "m";
  44.             }
  45.  
  46.             // Check if given background color found
  47.             if (isset($this->background_colors[$background])) {
  48.                     $colored_string .= "\033[" . $this->background_colors[$background] . "m";
  49.             }
  50.  
  51.             // Add string and end coloring
  52.             $colored_string .=  $string . "\033[0m";
  53.             return $colored_string;
  54.         }
  55.  
  56.         // Returns all foreground color names
  57.         public function getForegroundColors() {
  58.             return array_keys($this->foreground_colors);
  59.         }
  60.  
  61.         // Returns all background color names
  62.         public function getBackgroundColors() {
  63.             return array_keys($this->background_colors);
  64.         }
  65.     }
  66.  
  67.     // Usage
  68.     $colors = new Colors();
  69.  
  70.     echo $colors->getColoredString("SUCCESS!", "white", "green") . "\n";
  71.     echo $colors->getColoredString("SUCCESS!", "white", "green") . "\n";
  72.     echo $colors->getColoredString("SUCCESS!", "white", "green") . "\n";
  73.  
  74.     echo "\033[01;34mOr we can use colors directly inside echo. \033[0m", "\n";
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement