Cml\Console\Format\Colour::colour PHP Method

colour() public static method

返回格式化后的字符串
public static colour ( string $text, string | array | integer $foregroundColors = null, string | integer $backgroundColors = null ) : string
$text string 要着色的文本
$foregroundColors string | array | integer 前景色 eg: red、red+highlight、Colors::BLACK、[Colors::BLACK, Colors::HIGHLIGHT]
$backgroundColors string | integer 背景色
return string
    public static function colour($text, $foregroundColors = null, $backgroundColors = null)
    {
        if (self::$noAnsi) {
            return $text;
        }
        $colour = function ($text) use($foregroundColors, $backgroundColors) {
            $colors = [];
            if ($backgroundColors) {
                $bColor = self::charToCode($backgroundColors);
                $colors[] = "4{$bColor[0]}";
            }
            if ($foregroundColors) {
                list($fColor, $option) = self::charToCode($foregroundColors);
                $option && ($colors[] = $option);
                $colors[] = "3{$fColor}";
            }
            $colors && ($text = sprintf("[%sm", implode(';', $colors)) . $text . "");
            //关闭所有属性;
            return $text;
        };
        $lines = explode("\n", $text);
        foreach ($lines as &$line) {
            $line = $colour($line);
        }
        return implode("\n", $lines);
    }

Usage Example

コード例 #1
0
ファイル: StaticResource.php プロジェクト: linhecheng/cmlphp
 /**
  * 生成软链接
  *
  * @param null $rootDir 站点静态文件根目录默认为项目目录下的public目录
  */
 public static function createSymbolicLink($rootDir = null)
 {
     $isCli = Request::isCli();
     if ($isCli) {
         Output::writeln(Colour::colour('create link start!', [Colour::GREEN, Colour::HIGHLIGHT]));
     } else {
         echo "<br />**************************create link start!*********************<br />";
     }
     is_null($rootDir) && ($rootDir = CML_PROJECT_PATH . DIRECTORY_SEPARATOR . 'public');
     is_dir($rootDir) || mkdir($rootDir, true, 0700);
     //modules_static_path_name
     // 递归遍历目录
     $dirIterator = new \DirectoryIterator(Cml::getApplicationDir('apps_path'));
     foreach ($dirIterator as $file) {
         if (!$file->isDot() && $file->isDir()) {
             $resourceDir = $file->getPathname() . DIRECTORY_SEPARATOR . Cml::getApplicationDir('app_static_path_name');
             if (is_dir($resourceDir)) {
                 $distDir = $rootDir . DIRECTORY_SEPARATOR . $file->getFilename();
                 $cmd = Request::operatingSystem() ? "mklink /d {$distDir} {$resourceDir}" : "ln -s {$resourceDir} {$distDir}";
                 is_dir($distDir) || exec($cmd, $result);
                 $tip = "  create link Application [{$file->getFilename()}] result : [" . (is_dir($distDir) ? 'true' : 'false') . "]";
                 if ($isCli) {
                     Output::writeln(Colour::colour($tip, [Colour::WHITE, Colour::HIGHLIGHT]));
                 } else {
                     print_r('|<span style="color:blue">' . str_pad($tip, 64, ' ', STR_PAD_BOTH) . '</span>|');
                 }
             }
         }
     }
     if ($isCli) {
         Output::writeln(Colour::colour('create link end!', [Colour::GREEN, Colour::HIGHLIGHT]));
     } else {
         echo "<br />****************************create link end!**********************<br />";
     }
 }
All Usage Examples Of Cml\Console\Format\Colour::colour