Rumah php教程 php手册 PHP Class Command Line Colors (bash,cmd)

PHP Class Command Line Colors (bash,cmd)

Jun 13, 2016 am 10:45 AM
bash c class cmd colors command line php

 


 

color.class.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 .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
            }
            // Check if given background color found
            if (isset($this->background_colors[$background_color])) {
                $colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
            }
 
            // Add string and end coloring
            $colored_string .=  $string . "\033[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);
        }
    }
 
?>

 

 

banner.php

include("color.class.php");

$colors = new Colors();

$a=$colors->getColoredString("   ##        ###       ", "light_red");

$x=$b=$colors->getColoredString("     c0d3b3:", "light_red","red");


$b=$colors->getColoredString("lost", "light_red","red");

$c=$colors->getColoredString("wolf      ", "light_red","red");
echo "\r\n";

echo  "                                                $x"."$b"."$c";

echo "\n\r\n\r\n\r\n\r\n";

echo  "                        ########                    #"."\n";
echo  "                    #################              #"."\n";
echo  "                  ######################          #"."\n";
echo  "                  #########################      #"."\n";
echo  "                ############################"."\n";
echo  "               ##############################"."\n";
echo  "               ###############################"."\n";
echo  "              ###############################"."\n";
echo  "              ##############################"."\n";
echo  "                              #    ########   #";
echo  "     \n               $a";echo"####   ##"."\n";
echo  "                                      ###   ###"."\n";
echo  "                                    ####   ###"."\n";
echo  "               ####          ##########   ####"."\n";
echo  "               #######################   ####"."\n";
echo  "                 ####################   ####"."\n";
echo  "                  ##################  ####"."\n";
echo  "                    ############      ##"."\n";
echo  "                       ########        ###"."\n";
echo  "                      #########        #####"."\n";
echo  "                    ############      ######"."\n";
echo  "                   ########      #########"."\n";
echo  "                     #####       ########"."\n";
echo  "                       ###       #########"."\n";
echo  "                      ######    ############"."\n";
echo  "                     #######################"."\n";
echo  "                     #   #   ###  #   #   ##"."\n";
echo  "                     ########################"."\n";
echo  "                      ##     ##   ##     ##"."\r\n\r\n";

?>

Colors class basic usage examples

 
    // 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") . "\n";
    echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n";
    echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n";
    echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n";
    echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n";
    echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";
 
?>

All Foreground and background colors printed

 

 

   
        // 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             echo $colors->getColoredString("Test Foreground colors", $fgs[$i]) . "\t";
            if (isset($bgs[$i])) {
                echo $colors->getColoredString("Test Background colors", null, $bgs[$i]);
            }
            echo "\n";
        }
        echo "\n";
 
        // Loop through all foreground and background colors
        foreach ($fgs as $fg) {
            foreach ($bgs as $bg) {
                echo $colors->getColoredString("Test Colors", $fg, $bg) . "\t";
            }
            echo "\n";
        }
 
?>

//////////////////////////////////////////////////////////

ANSI Command Line Colors under Windows

 

Having colored text in the command line is a great help for spotting error or success messages. Unfortunately, those of us developing under Windows do not have this feature by default. Here’s how to enable it:

  1. Download ANSICON from http://adoxa.110mb.com/ansicon/index.html
  2. Extract the proper files (Depending on if you have a 32 or 64 bit machine) to c:\ansicon\ (For example). I have a 32 bit machine and hence I extracted the files from inside the x86 folder.
  3. Open a command line prompt and go to c:\ansicon, and then type “ansicon -i” without the quotes
  4. Done

You can now enjoy the colored output of PHPUnit for example.

 

 

摘自 lostwolf's blog

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Tetapan grafik terbaik
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Cara Memperbaiki Audio Jika anda tidak dapat mendengar sesiapa
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Pengesah Mencipta CakePHP Pengesah Mencipta CakePHP Sep 10, 2024 pm 05:26 PM

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

Panduan Ringkas CakePHP Panduan Ringkas CakePHP Sep 10, 2024 pm 05:27 PM

CakePHP ialah rangka kerja MVC sumber terbuka. Ia menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP mempunyai beberapa perpustakaan untuk mengurangkan beban tugas yang paling biasa.

Bagaimana anda menghuraikan dan memproses HTML/XML dalam PHP? Bagaimana anda menghuraikan dan memproses HTML/XML dalam PHP? Feb 07, 2025 am 11:57 AM

Tutorial ini menunjukkan cara memproses dokumen XML dengan cekap menggunakan PHP. XML (bahasa markup extensible) adalah bahasa markup berasaskan teks yang serba boleh yang direka untuk pembacaan manusia dan parsing mesin. Ia biasanya digunakan untuk penyimpanan data

See all articles