11.PNG

PHP 命令行(CLI)脚本使用彩色着色类 PHP Class for Coloring PHP Command Line (CLI) Scripts Output

PHP 命令行界面 (CLI) 没有内置的脚本输出着色。 我们可以创建自己的类在 PHP CLI 输出上添加颜色。
此类仅适用于 Bash shell。 这个类很容易使用,只需创建类的新实例,调用 getColoredString 函数,
并使用字符串和前景色或背景色做参数。

  • colors_class.php

    <?php
    class Colors
    {
      private $foreground_colors = array();
      private $background_colors = array();
    
      public function __construct()
      {
          // Set up shell colors
          $this->foreground_colors['black'] = '0;30';
          $this->foreground_colors['dark_gray'] = '1;30';
          $this->foreground_colors['blue'] = '0;34';
          $this->foreground_colors['light_blue'] = '1;34';
          $this->foreground_colors['green'] = '0;32';
          $this->foreground_colors['light_green'] = '1;32';
          $this->foreground_colors['cyan'] = '0;36';
          $this->foreground_colors['light_cyan'] = '1;36';
          $this->foreground_colors['red'] = '0;31';
          $this->foreground_colors['light_red'] = '1;31';
          $this->foreground_colors['purple'] = '0;35';
          $this->foreground_colors['light_purple'] = '1;35';
          $this->foreground_colors['brown'] = '0;33';
          $this->foreground_colors['yellow'] = '1;33';
          $this->foreground_colors['light_gray'] = '0;37';
          $this->foreground_colors['white'] = '1;37';
    
          $this->background_colors['black'] = '40';
          $this->background_colors['red'] = '41';
          $this->background_colors['green'] = '42';
          $this->background_colors['yellow'] = '43';
          $this->background_colors['blue'] = '44';
          $this->background_colors['magenta'] = '45';
          $this->background_colors['cyan'] = '46';
          $this->background_colors['light_gray'] = '47';
      }
    
      // Returns colored string
      public function getColoredString($string, $foreground_color = null, $background_color = null)
      {
          $colored_string = "";
    
          // Check if given foreground color found
          if (isset($this->foreground_colors[$foreground_color])) {
              $colored_string .= "33[" . $this->foreground_colors[$foreground_color] . "m";
          }
          // Check if given background color found
          if (isset($this->background_colors[$background_color])) {
              $colored_string .= "33[" . $this->background_colors[$background_color] . "m";
          }
    
          // Add string and end coloring
          $colored_string .=  $string . "33[0m";
    
          return $colored_string;
      }
    
      // Returns all foreground color names
      public function getForegroundColors()
      {
          return array_keys($this->foreground_colors);
      }
    
      // Returns all background color names
      public function getBackgroundColors()
      {
          return array_keys($this->background_colors);
      }
    }
    ?>

    颜色类基本使用示例: Colors class basic usage examples

    1.png

  • colors.php

    <?php
      include("colors_class.php");
      // Create new Colors class
      $colors = new Colors();
    
      // Test some basic printing with Colors class
      echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "
    ";
      echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "
    ";
      echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "
    ";
      echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "
    ";
      echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "
    ";
      echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "
    ";
    ?>

打印所有前景色和背景色: All Foreground and background colors printed

  • all_colors.php

    <?php
      include("colors_class.php");
      // Create new Colors class
      $colors = new Colors();
    
      // Get Foreground Colors
      $fgs = $colors->getForegroundColors();
      // Get Background Colors
      $bgs = $colors->getBackgroundColors();
    
      // Loop through all foreground and background colors
      $count = count($fgs);
      for ($i = 0; $i < $count; $i++) {
          echo $colors->getColoredString("Test Foreground colors", $fgs[$i]) . "    ";
          if (isset($bgs[$i])) {
              echo $colors->getColoredString("Test Background colors", null, $bgs[$i]);
          }
          echo "
    ";
      }
      echo "
    ";
    
      // Loop through all foreground and background colors
      foreach ($fgs as $fg) {
          foreach ($bgs as $bg) {
              echo $colors->getColoredString("Test Colors", $fg, $bg) . "    ";
          }
          echo "
    ";
      }
    ?>

    当然还有一个简单的方法,代码很小,四种颜色也够用了

    3.png

  • colorize.php

    <?php
    function colorize($text, $status) {
     $out = "";
     switch($status) {
    case "SUCCESS":
     $out = "[42m"; //Green background
     break;
    case "FAILURE":
     $out = "[41m"; //Red background
     break;
    case "WARNING":
     $out = "[43m"; //Yellow background
     break;
    case "NOTE":
     $out = "[44m"; //Blue background
     break;
    default:
     throw new Exception("Invalid status: " . $status);
     }
     return chr(27) . "$out" . "$text" . chr(27) . "[0m";
    }
    
    echo colorize("Your command was successfully executed...
    ", "SUCCESS");
    echo colorize("Your command was FAILURE executed...
    ", "FAILURE");
    echo colorize("Your command was WARNING executed...
    ", "WARNING");
    echo colorize("Your command was NOTE    info...
    ", "NOTE");
    
    ?>
分类: 学习编程

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注