ansi

Terminal ANSI colors, 256-color & RGB styling, stripping and progress bars.

v1.0.0Tooling & CLIbundledimport "ansi";
Overview Installation Documentation Examples

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
Packageansi
Version1.0.0 (follows the compiler)
Entry fileansi.hphp
Sourcepackages/lib/ansi ↗

Documentation

API Reference

Text Styling

Standard & Bright Colors

256-Color, RGB & Hex

Fluent Builder (AnsiStyle)

Stripping & Visible Length

Progress Bar

Error Handling

All functions validate their inputs and throw Exception with descriptive ansi: ... messages:

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 it
hphp install ansi
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_ansi.hphp.