2

我想在使用命令行的 PHP 应用程序中为整行背景颜色着色。问题是我没有找到任何“更聪明”的方法来做到这一点:

public function outFullLine($string, $center = false)
{
    $terminalColumns = exec('tput cols');
    $emptyRow = implode(array_fill(0, $terminalColumns, " "));

    if ($center) {
        $position = strlen($emptyRow) / 2 - strlen($string) / 2;
    } else {
        $position = 0;
    }

    $outputString = substr_replace(
        $emptyRow,
        $string,
        $position,
        strlen($string)
    );

    fwrite(STDOUT, $string);
}

那么有什么方法可以做到这一点而不用实际填充空格?我想这更像是一个 bash 而不是 PHP 问题,所以我标记了两者。

4

2 回答 2

1

以时间为例:http ://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-着色-使用-bash-shell-colors/

或者放在课堂上然后调用颜色:

class ColorCLI {
            static $foreground_colors = array(
                    'black'        => '0;30', 'dark_gray'    => '1;30',
                    'blue'         => '0;34', 'light_blue'   => '1;34',
                    'green'        => '0;32', 'light_green'  => '1;32',
                    'cyan'         => '0;36', 'light_cyan'   => '1;36',
                    'red'          => '0;31', 'light_red'    => '1;31',
                    'purple'       => '0;35', 'light_purple' => '1;35',
                    'brown'        => '0;33', 'yellow'   => '1;33',
                    'light_gray'   => '0;37', 'white'        => '1;37',
            );

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

            // Returns colored string
            public static function getColoredString($string, $foreground_color = null, $background_color = null) {
                    $colored_string = "";

                    // Check if given foreground color found
                    if ( isset(self::$foreground_colors[$foreground_color]) ) {
                            $colored_string .= "\033[" . self::$foreground_colors[$foreground_color] . "m";
                    }
                    // Check if given background color found
                    if ( isset(self::$background_colors[$background_color]) ) {
                            $colored_string .= "\033[" . self::$background_colors[$background_color] . "m";
                    }

                    // Add string and end coloring
                    $colored_string .=  $string . "\033[0m";

                    return $colored_string;
            }

            // Returns all foreground color names
            public static function getForegroundColors() {
                    return array_keys(self::$foreground_colors);
            }

            // Returns all background color names
            public static function getBackgroundColors() {
                    return array_keys(self::$background_colors);
            }
    }

您需要首先计算字符总数,然后将颜色填充到一行:

$screenwidth = exec('tput cols');
$screenheight = exec('tput lines');

检查这个颜色和行:http ://blog.apokalyptik.com/2007/11/20/colorizing-php-cli-scripts/这个完整的类代码作为例子:https ://github.com/runekaagaard /php-termcolor/blob/master/termcolor.php

于 2013-02-22T18:06:37.500 回答
0

当您为字符及其背景着色时,要为整行着色,您必须用空白字符填充它,例如空格或制表符。顺便说一句,有一个很好的控制台颜色梨类

于 2013-02-22T18:57:00.057 回答