Overview
Zero-dependency terminal ANSI color, text styling, 256-color & truecolor RGB, ANSI escape stripping, visible length calculation, and progress bar rendering for HolyPHP.
bundled — works offline
zero dependencies
terminal
color
Quick start
import "ansi";
println(ansi_bold(ansi_green("Build succeeded!")));
println(ansi_rgb("Custom truecolor text", 255, 127, 80));
println(ansi_fg256("256-color orange", 208));
$style = new AnsiStyle();
println($style->bold()->underline()->rgb(100, 200, 255)->apply("Styled header"));
$colored = ansi_red("Error: ") . ansi_bold("file not found");
println("Plain text: " . ansi_strip($colored));
println("Visible length: " . ansi_visible_len($colored));
println(ansi_progress_bar(7, 10, 20));
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install ansi
This adds the library to hphp.yaml, installs it into .hphp/packages/ansi/ and pins the exact version in hphp.lock:
require:
ansi: "^1.0"
Use it
import "ansi";
ansi is bundled with the compiler, so import "ansi"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install ansi # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install ansi
Documentation
API Reference
Text Styling
ansi_bold(string $text): string
ansi_dim(string $text): string
ansi_italic(string $text): string
ansi_underline(string $text): string
ansi_blink(string $text): string
ansi_inverse(string $text): string
ansi_hidden(string $text): string
ansi_strikethrough(string $text): string
ansi_reset(): string — returns the ANSI SGR reset sequence (\x1b[0m).
ansi_style(string $text, array $codes): string — wraps $text with multiple SGR integer codes (0..107).
Standard & Bright Colors
- Foreground:
ansi_black, ansi_red, ansi_green, ansi_yellow, ansi_blue, ansi_magenta, ansi_cyan, ansi_white
- Bright Foreground:
ansi_bright_black, ansi_bright_red, ansi_bright_green, ansi_bright_yellow, ansi_bright_blue, ansi_bright_magenta, ansi_bright_cyan, ansi_bright_white
- Background:
ansi_bg_black, ansi_bg_red, ansi_bg_green, ansi_bg_yellow, ansi_bg_blue, ansi_bg_magenta, ansi_bg_cyan, ansi_bg_white
- Bright Background:
ansi_bg_bright_black, ansi_bg_bright_red, ansi_bg_bright_green, ansi_bg_bright_yellow, ansi_bg_bright_blue, ansi_bg_bright_magenta, ansi_bg_bright_cyan, ansi_bg_bright_white
256-Color, RGB & Hex
ansi_fg256(string $text, int $code): string — 256-color foreground ($code in 0..255).
ansi_bg256(string $text, int $code): string — 256-color background ($code in 0..255).
ansi_rgb(string $text, int $r, int $g, int $b): string — 24-bit truecolor foreground (0..255 per channel).
ansi_bg_rgb(string $text, int $r, int $g, int $b): string — 24-bit truecolor background (0..255 per channel).
ansi_hex(string $text, string $hex): string — truecolor foreground from #RRGGBB or RRGGBB.
ansi_bg_hex(string $text, string $hex): string — truecolor background from #RRGGBB or RRGGBB.
Fluent Builder (AnsiStyle)
new AnsiStyle()
- Chainable methods:
->bold(), ->dim(), ->italic(), ->underline(), ->inverse(), ->strikethrough(), ->fg256(int $code), ->bg256(int $code), ->rgb(int $r, int $g, int $b), ->bgRgb(int $r, int $g, int $b)
->apply(string $text): string
Stripping & Visible Length
ansi_strip(string $text): string — removes ANSI CSI, OSC, and 2-byte escape sequences.
ansi_visible_len(string $text): int — returns the character length of $text with ANSI escape sequences stripped.
Progress Bar
ansi_progress_bar(int $current, int $total, int $width = 30, string $fill = "#", string $empty = "-", bool $colored = true): string
- Renders a progress bar such as
[#####-----] 50%.
$current values greater than $total are clamped to $total.
Error Handling
All functions validate their inputs and throw Exception with descriptive ansi: ... messages:
- Out-of-range 256-color code (
< 0 or > 255): ansi: 256-color foreground code must be between 0 and 255, got 300
- Out-of-range RGB channel (
< 0 or > 255): ansi: RGB red channel must be between 0 and 255, got 256
- Invalid hex color string:
ansi: hex color must be 6 hex digits (e.g. "#ff8800"), got "#fff"
- Invalid SGR code in
ansi_style: ansi: SGR code must be an integer between 0 and 107, got 999
- Invalid progress bar parameters (
$total <= 0, $current < 0, or $width < 1): ansi: progress bar total must be greater than 0, got 0
Examples
01Quick start
Bundled with the compiler — import "ansi"; works offline in any file, or add it to a project with hphp install ansi.
import "ansi";
println(ansi_bold(ansi_green("Build succeeded!")));
println(ansi_rgb("Custom truecolor text", 255, 127, 80));
println(ansi_fg256("256-color orange", 208));
$style = new AnsiStyle();
println($style->bold()->underline()->rgb(100, 200, 255)->apply("Styled header"));
$colored = ansi_red("Error: ") . ansi_bold("file not found");
println("Plain text: " . ansi_strip($colored));
println("Visible length: " . ansi_visible_len($colored));
println(ansi_progress_bar(7, 10, 20));
Try ithphp install ansi
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_ansi.hphp.