codedungeon/php-cli-colors
Add ANSI color codes to PHP CLI output with simple constants. Use Color::GREEN, Color::RESET, etc. to style messages in console apps. Lightweight, zero-config, and easy to drop into any Composer-based project.
Start by installing the package via Composer:
composer require codedungeon/php-cli-colors '~1.0'
The package is minimal and self-contained — no configuration or bootstrapping required. Immediately use it in your CLI scripts by including the autoloader and referencing the Color class constants:
require __DIR__ . '/vendor/autoload.php';
use Codedungeon\PHPCliColors\Color;
echo Color::GREEN . 'Success!' . Color::RESET . PHP_EOL;
Your first real-world use case: outputting colorized status messages in a console application (e.g., php my-tool.php). For quick validation, run the included rainbow.php example from the repo to see all available colors rendered in the terminal.
Color::GREEN, Color::RED, and Color::RESET inline for simple highlight needs.Color::prefix(), Color::suffix(), and Color::apply($text, $color) for reusable color wrappers:
echo Color::apply('Error occurred!', Color::RED) . PHP_EOL;
Color::BG_RED or Color::BG_WHITE.trait LoggerTrait {
public function info(string $msg): void {
echo Color::CYAN . '[INFO] ' . Color::RESET . $msg . PHP_EOL;
}
}
echoColor() that handles php_sapi_name() === 'cli' checks if needed for non-cli contexts).echo "\x1b[90m...\x1b[0m" works on modern Windows 10+ with VT100 enabled, but older systems need workarounds like php-console-color fallbacks).Color::RESET is critical: Always reset colors after use to avoid bleeding into subsequent output (especially in shells with persistent prompts). Missing this is the #1 source of "my terminal is stuck red!" reports.~1.12.0 or higher for vibrant colors.// ✅ Good
echo Color::RED . $msg . Color::RESET;
// ⚠️ Risky
echo "Error: $msg"; // Cannot safely inject color into variable interpolation
symfony/console instead, or layer on top (e.g., use this for colors, symfony/console for formatting/options).vendor/composer/autoload_classmap.php includes Color.php.putenv('NO_COLOR=1') (the package respects the NO_COLOR convention by default), ensuring predictable test output.How can I help you explore Laravel packages today?