south-pointe / ansi
Library for creating ANSI escape codes in PHP
Installs: 1 268
Dependents: 3
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/south-pointe/ansi
Requires
- php: >= 8.1
Requires (Dev)
- ext-posix: *
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ~10.0
- symfony/var-dumper: ^6
This package is auto-updated.
Last update: 2025-10-09 19:21:18 UTC
README
This library will enable you to easily generate ANSI sequence strings in CLI.
The list below are some things you can do with this library.
- Set foreground/background color
- Set font styles (bold/italic)
- Move cursor positions
- Clear the screen
- Get the terminal size
Prerequisites
- PHP 8.1+
Installation
composer require south-pointe/ansi
Example
use SouthPointe\Ansi\Ansi; use SouthPointe\Ansi\Codes\Color; // Change foreground text to blue. echo Ansi::fgColor(Color::Blue); // Set color by 256-color mode. echo Ansi::fgColor(Color::code(12)); // Move the cursor back by 2. echo Ansi::cursorBack(2); // Add "test" + "\e[0m" (reset style) + "\r" (carriage return) + "\n" (new line) echo Ansi::line('test'); // Ansi::buffer() will allow you to chain multiple sequences. echo Ansi::buffer() ->bold() ->fgColor(Color::Gray) ->text('foo bar') ->resetStyle() ->toString(); // Will send the string to STDOUT (default stream uri) echo Ansi::stream() ->text('to stdout') ->flush(); // Will send the string to STDERR echo Ansi::stream(STDERR) ->bgColor(Color::Red) ->flush(); // Returns the size of the terminal as ['row' => int, 'column' => int]. echo Ansi::getTerminalSize();
Methods
Ansi
This is the main class you should be using to generate the sequence codes.
All methods in this class are static.
Ansi::buffer(): BufferStarts a buffered instance. Used for chaining sequences.Ansi::line($text): stringOutput text, reset style, and move to the new line.Ansi::bell(): stringRings the bell.Ansi::backspace(): stringMoves the cursor back.Ansi::tab(): stringMoves the cursor by 8 spaces.Ansi::lineFeed(): stringMoves to the next line and scroll screen up.Ansi::carriageReturn(): stringMoves cursor to column 0.Ansi::cursorUp(int $n = 1): stringMoves the cursor up$nrows.Ansi::cursorDown(int $n = 1): stringMoves the cursor down$nrows.Ansi::cursorForward(int $n = 1): stringMoves the cursor forward$ncells.Ansi::cursorBack(int $n = 1): stringMoves the cursor back$ncells.Ansi::cursorNextLine(int $n = 1): stringMoves cursor to start of line and move down by$n.Ansi::cursorPreviousLine(int $n = 1): stringMoves cursor to the start of line and move up by$n.Ansi::cursorPosition(int $row, int $column): stringMoves cursor to the specified position.Ansi::eraseScreen(): stringErases the entire screen.Ansi::eraseToEndOfScreen(): stringErases from the cursor position to the end of screen.Ansi::eraseFromStartOfScreen(): stringErases from the start of screen to the cursor position.Ansi::eraseSavedLines(): stringClears the screen and deletes all lines in the scrollback buffer.Ansi::eraseLine(): stringErases the entire line. Cursor Position will not change.Ansi::eraseToEndOfLine(): stringErases from cursor position to the end of the line.Ansi::eraseFromStartOfLine(): stringErases from the start of the line to the cursor position.Ansi::scrollUp(int $lines = 1): stringScrolls the screen up by$lines.Ansi::scrollDown(int $lines = 1): stringScrolls the screen down by$lines.Ansi::resetStyle(): stringResets the style of the output.Ansi::bold(bool $toggle = true): stringApplies bold styling to succeeding text.Ansi::italic(bool $toggle = true): stringApplies italic styling to succeeding text.Ansi::underline(bool $toggle = true): stringUnderlines to succeeding text.Ansi::blink(bool $toggle = true): stringMakes succeeding text blink.Ansi::foreground(Color $color): stringApplies the given color to the foreground font.Ansi::foreground(Color $color): stringApplies the given color to the background font.Ansi::deviceStatusReport(): stringGives the device status report.Ansi::getTerminalSize(): array{ row: int, column: int }Get the terminal size of the current terminal.
Buffer
This class should be instantiated by calling
Ansi::buffer().
Buffered class contains all methods inAnsiclass exceptgetTerminalSize.
text(string $text): selfAdds text to buffer.flush(resource $to): selfFlushes all sequences buffered to the given resource.clear(): selfClears all sequences stored in the buffer.toString(): stringCoverts all buffered sequences to string.
Color
Enum that contains shortcut names for 8-bit colors.
Check out the actual class for all the names.
Color::code(int $code): selfGets the color by number.
License
This is an open-sourced software licensed under the MIT License.