kahlan\cli\Cli::color PHP Method

color() public static method

Return a VT100 colored string.
public static color ( mixed $string, string | array $options = null )
$string mixed The string to color.
$options string | array The possible values for an array are: - `'style`: a style code. - `'color'`: a color code. - `'background'`: a background color code. The string must respect one of the following format: - `'style;color;background'`. - `'style;color'`. - `'color'`.
    public static function color($string, $options = null)
    {
        if ($options === null) {
            return $string;
        }
        if (is_string($options)) {
            $options = explode(';', $options);
            if (strlen($options[0]) === 1) {
                $options = array_pad($options, 3, 'default');
                $options = array_combine(['style', 'color', 'background'], $options);
            } else {
                $options = ['color' => reset($options)];
            }
        }
        $options += ['style' => 'default', 'color' => 'default', 'background' => 'default'];
        $format = "[";
        $format .= static::_vtstyle($options['style']) . ';';
        $format .= static::_vtcolor($options['color']) . ';';
        $format .= static::_vtbackground($options['background']) . 'm';
        return $format . $string . "";
    }

Usage Example

Beispiel #1
0
                    $check = ord($actual[$i]) == ord($expected[$i]) ? true : false;
                    if ($check) {
                        break;
                    }
                }
                expect($check)->toBe(true);
            };
        });
        it("leaves string unchanged whith no options", function () {
            expect(Cli::color("String"))->toBe("String");
        });
        it("applies a color using a string as options", function () {
            $this->check(Cli::color("String", "yellow"), "Srting");
        });
        it("applies a complex color using a semicolon separated string as options", function () {
            $this->check(Cli::color("String", "n;yellow;100"), "Srting");
            $this->check(Cli::color("String", "4;red;100"), "Srting");
            $this->check(Cli::color("String", "n;100;100"), "Srting");
        });
        it("applies the 39 default color with unknown color name", function () {
            $this->check(Cli::color("String", "some_strange_color"), "Srting");
        });
    });
    describe('->bell()', function () {
        it("bells", function () {
            expect(function () {
                Cli::bell(2);
            })->toEcho(str_repeat("", 2));
        });
    });
});
All Usage Examples Of kahlan\cli\Cli::color