PHPackages                             south-pointe/ansi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. south-pointe/ansi

ActiveLibrary[CLI &amp; Console](/categories/cli)

south-pointe/ansi
=================

Library for creating ANSI escape codes in PHP

21.7k↑37.5%1PHP

Since Nov 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/south-pointe/ansi)[ Packagist](https://packagist.org/packages/south-pointe/ansi)[ RSS](/packages/south-pointe-ansi/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

ANSI sequence generator library for PHP
=======================================

[](#ansi-sequence-generator-library-for-php)

[![Test](https://github.com/south-pointe/ansi/actions/workflows/test.yml/badge.svg)](https://github.com/south-pointe/ansi/actions/workflows/test.yml/badge.svg)[![codecov](https://camo.githubusercontent.com/cc7355f63a38bc15dffe2bd6cde895560680ee1d79b385a612a61bef9096f9bf/68747470733a2f2f636f6465636f762e696f2f67682f736f7574682d706f696e74652f616e73692f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d315056384642344f344f)](https://codecov.io/gh/south-pointe/ansi)[![GitHub](https://camo.githubusercontent.com/8ce2a30a6b23ce7487ab17a90b4c67f1c7f2adaf3401f5757336c82165561d1e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736f7574682d706f696e74652f616e7369)](https://camo.githubusercontent.com/8ce2a30a6b23ce7487ab17a90b4c67f1c7f2adaf3401f5757336c82165561d1e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736f7574682d706f696e74652f616e7369)

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
-------------

[](#prerequisites)

- PHP 8.1+

Installation
------------

[](#installation)

```
composer require south-pointe/ansi

```

Example
-------

[](#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
-------

[](#methods)

### Ansi

[](#ansi)

> This is the main class you should be using to generate the sequence codes.
> All methods in this class are static.

- `Ansi::buffer(): Buffer` Starts a buffered instance. Used for chaining sequences.
- `Ansi::line($text): string` Output text, reset style, and move to the new line.
- `Ansi::bell(): string` Rings the bell.
- `Ansi::backspace(): string` Moves the cursor back.
- `Ansi::tab(): string` Moves the cursor by 8 spaces.
- `Ansi::lineFeed(): string` Moves to the next line and scroll screen up.
- `Ansi::carriageReturn(): string` Moves cursor to column 0.
- `Ansi::cursorUp(int $n = 1): string` Moves the cursor up `$n` rows.
- `Ansi::cursorDown(int $n = 1): string` Moves the cursor down `$n` rows.
- `Ansi::cursorForward(int $n = 1): string` Moves the cursor forward `$n` cells.
- `Ansi::cursorBack(int $n = 1): string` Moves the cursor back `$n` cells.
- `Ansi::cursorNextLine(int $n = 1): string` Moves cursor to start of line and move down by `$n`.
- `Ansi::cursorPreviousLine(int $n = 1): string` Moves cursor to the start of line and move up by `$n`.
- `Ansi::cursorPosition(int $row, int $column): string` Moves cursor to the specified position.
- `Ansi::eraseScreen(): string` Erases the entire screen.
- `Ansi::eraseToEndOfScreen(): string` Erases from the cursor position to the end of screen.
- `Ansi::eraseFromStartOfScreen(): string` Erases from the start of screen to the cursor position.
- `Ansi::eraseSavedLines(): string` Clears the screen and deletes all lines in the scrollback buffer.
- `Ansi::eraseLine(): string` Erases the entire line. Cursor Position will not change.
- `Ansi::eraseToEndOfLine(): string` Erases from cursor position to the end of the line.
- `Ansi::eraseFromStartOfLine(): string` Erases from the start of the line to the cursor position.
- `Ansi::scrollUp(int $lines = 1): string` Scrolls the screen up by `$lines`.
- `Ansi::scrollDown(int $lines = 1): string` Scrolls the screen down by `$lines`.
- `Ansi::resetStyle(): string` Resets the style of the output.
- `Ansi::bold(bool $toggle = true): string` Applies bold styling to succeeding text.
- `Ansi::italic(bool $toggle = true): string` Applies italic styling to succeeding text.
- `Ansi::underline(bool $toggle = true): string` Underlines to succeeding text.
- `Ansi::blink(bool $toggle = true): string` Makes succeeding text blink.
- `Ansi::foreground(Color $color): string` Applies the given color to the foreground font.
- `Ansi::foreground(Color $color): string` Applies the given color to the background font.
- `Ansi::deviceStatusReport(): string` Gives the device status report.
- `Ansi::getTerminalSize(): array{ row: int, column: int }` Get the terminal size of the current terminal.

### Buffer

[](#buffer)

> This class should be instantiated by calling `Ansi::buffer()`.
> Buffered class contains all methods in `Ansi` class except `getTerminalSize`.

- `text(string $text): self` Adds text to buffer.
- `flush(resource $to): self` Flushes all sequences buffered to the given resource.
- `clear(): self` Clears all sequences stored in the buffer.
- `toString(): string` Coverts all buffered sequences to string.

### Color

[](#color)

> Enum that contains shortcut names for 8-bit colors.
> Check out the [actual class](src/Codes/Color.php) for all the names.

- `Color::code(int $code): self` Gets the color by number.

License
-------

[](#license)

This is an open-sourced software licensed under the [MIT License](LICENSE).

References
----------

[](#references)

- [ANSI escape code - Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code)
- [ANSI Escape Codes - Gist](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797)
- [Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement - Bash Prompt HOWTO](https://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html)

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity20

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/02fd797054dcc2ad8fd9e01dba848d8bb50f00a4748e681a6c57860c22217814?d=identicon)[taka-oyama](/maintainers/taka-oyama)

---

Top Contributors

[![taka-oyama](https://avatars.githubusercontent.com/u/748854?v=4)](https://github.com/taka-oyama "taka-oyama (46 commits)")

---

Tags

ansiansi-codesansi-escape-sequencecliphp

### Embed Badge

![Health badge](/badges/south-pointe-ansi/health.svg)

```
[![Health](https://phpackages.com/badges/south-pointe-ansi/health.svg)](https://phpackages.com/packages/south-pointe-ansi)
```

###  Alternatives

[wp-cli/wp-cli

WP-CLI framework

5.1k17.2M320](/packages/wp-cli-wp-cli)[consolidation/annotated-command

Initialize Symfony Console commands from annotated command class methods.

22569.8M19](/packages/consolidation-annotated-command)[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24725.8M17](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

12944.1M5.1k](/packages/illuminate-console)[php-tui/php-tui

Comprehensive TUI library heavily influenced by Ratatui

589747.0k6](/packages/php-tui-php-tui)[codedungeon/php-cli-colors

Liven up you PHP Console Apps with standard colors

10210.1M26](/packages/codedungeon-php-cli-colors)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
