nafisc/ansi-util

An ANSI library for PHP

v1.2 2019-05-18 22:58 UTC

This package is auto-updated.

Last update: 2024-12-19 11:13:41 UTC


README

Allows you to create ANSI strings with ease in PHP.

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

Java Java Version: See here

Usage

Note: It is not guaranteed that all of these ANSI codes will work on all terminals. See here for a list of compatible terminals.

$stringBuilder = new \Ansi\StringBuilder;

echo $stringBuilder
         ->raw('Hello, ')
         ->underline('John Doe')
         ->resetUnderline()
         ->raw('. ')
         ->raw('This is ')
         ->color16(\Ansi\Color16::FG_RED, 'red')
         ->raw('.')
         .PHP_EOL;

Result

\Ansi\StringBuilder

Note: Each of these functions takes an optional string parameter which will be added immediately after the ANSI code is applied. They also all follow the Fluent Design Pattern. The string builder itself implements the __toString magic method, and as such can be treated as a string.

Utility Methods

\Ansi\Color16

The Color16 class is a utility class for 16-bit color codes.

Note: Each of these constants needs to be prefixed with either FG_ when using it as a foreground color or BG_ when using it as a background color.

RESET
BLACK
RED
GREEN
YELLOW
BLUE
MAGENTA
CYAN
LIGHT_GRAY
DARK_GRAY
LIGHT_RED
LIGHT_GREEN
LIGHT_YELLOW
LIGHT_BLUE
LIGHT_MAGENTA
LIGHT_CYAN
WHITE