bramus/ansi-php
bramus/ansi-php is a lightweight PHP library for working with ANSI escape codes in the terminal. Easily colorize and style CLI output, move the cursor, clear sections of the screen, and build richer command-line interfaces with minimal setup.
Installation Add the package via Composer:
composer require bramus/ansi-php
No additional configuration is required—it’s a drop-in library.
First Use Case Output colored text in a CLI script:
use Bramus\Ansi\Ansi;
echo Ansi::colorize('Hello, Laravel!', 'green');
Or use ANSI escape sequences directly:
echo Ansi::ansi('Hello, Laravel!')->fg('green')->render();
Where to Look First
colorize(), ansi(), and modifiers (fg(), bg(), bold(), etc.).Basic Text Styling
Use Ansi::colorize() for one-off colored text:
Ansi::colorize('Warning!', 'yellow', 'bold');
Fluent Chaining Build complex styles with method chaining:
echo Ansi::ansi('Error: File not found')
->fg('red')
->bg('white')
->bold()
->render();
CLI Artifacts
Ansi::table() for formatted output:
Ansi::table(['User', 'Email'], [
['John', 'john@example.com'],
['Jane', 'jane@example.com'],
]);
Ansi::ansi() for dynamic updates:
$progress = Ansi::ansi('[' . str_repeat(' ', 50) . ']')->fg('gray');
// Update later: $progress->fg('green')->render();
Integration with Laravel Artisan Enhance command output:
$this->info(Ansi::colorize('Task completed!', 'green'));
Conditional Styling Dynamically apply styles based on logic:
$status = $job->status;
$color = $status === 'failed' ? 'red' : 'green';
$this->line(Ansi::colorize($status, $color));
Monolog handlers:
$handler = new \Monolog\Handler\StreamHandler(storage_path('logs/app.log'));
$handler->pushProcessor(function ($record) {
$record['formatted'] = Ansi::colorize($record['message'], 'cyan');
return $record;
});
PHPUnit output for better readability:
$this->assertTrue(true, Ansi::colorize('Test passed!', 'green'));
Ansi::isSupported()) before rendering.mix->then(function () {
echo Ansi::colorize('Build complete!', 'green') . PHP_EOL;
});
Windows Compatibility
symfony/console for cross-platform compatibility.Overuse of Colors
Performance
Nested Styles
fg() inside another ansi()) may break rendering.Ansi::ansi()->render() for complex cases to isolate styles.IDE/Editor Support
symfony/console for IDE integration.\r or \n in sequences.Ansi::ansi()->render() to isolate the issue.if (!Ansi::isSupported()) {
echo "Fallback: " . $plainText;
}
.env or config files to tweak.Ansi::addColor('laravel', '#FF2D20'); // Laravel red
Ansi::colorize('Laravel', 'laravel');
Custom ANSI Sequences
Extend the library by adding methods to Ansi class:
// In a service provider or trait:
Ansi::macro('underline', function ($text) {
return $this->ansi($text)->underline()->render();
});
Usage:
Ansi::underline('Click here');
Integration with Laravel
Ansi to the container for dependency injection:
$this->app->singleton(Ansi::class, function () {
return new Ansi();
});
// app/Facades/Ansi.php
namespace App\Facades;
use Bramus\Ansi\Ansi;
use Illuminate\Support\Facades\Facade;
class Ansi extends Facade {
protected static function getFacadeAccessor() { return Ansi::class; }
}
Usage:
\App\Facades\Ansi::colorize('Hello', 'red');
Testing
Ansi in unit tests to avoid polluting output:
$this->partialMock(Ansi::class, ['colorize'])
->shouldReceive('colorize')
->once()
->andReturn('Mocked colored text');
How can I help you explore Laravel packages today?