jakub-onderka/php-console-color
Abandoned PHP library for styling console output with ANSI colors. Formerly used to print colored text in terminals (see example.php). Consider using the maintained alternative: https://github.com/php-parallel-lint/PHP-Console-Color
Installation:
composer require jakub-onderka/php-console-color
(Note: While the package is abandoned, it remains lightweight and functional for basic needs.)
Basic Usage:
use JakubOnderka\PhpConsoleColor\ConsoleColor;
$color = new ConsoleColor();
echo $color->getColoredString('Hello, Laravel!', 'green');
(Output: Green text in terminal.)
First Use Case:
App\Services\Logger for better readability in CLI tools like laravel-log or monolog.Artisan::command() outputs (e.g., success/failure messages).Dynamic Styling in Views/Blade:
// In a Blade template or service
$color = new ConsoleColor();
return $color->getColoredString('Warning: ', 'yellow') . 'Database connection failed.';
(Useful for CLI-based admin panels or debug views.)
Conditional Coloring:
$status = $job->status;
$color = new ConsoleColor();
$statusText = $status === 'completed'
? $color->getColoredString($status, 'green')
: $color->getColoredString($status, 'red');
(Apply in App\Jobs\JobStatusChecker or App\Console\Commands\JobMonitor.)
Integration with Laravel’s Logging:
// In AppServiceProvider::boot()
$logger = app(\Illuminate\Log\Logger::class);
$logger->listen(function ($level, $message, array $context) {
$color = new ConsoleColor();
echo $color->getColoredString("[{$level}] ", 'cyan') . $message . PHP_EOL;
});
(Override default log formatting in config/logging.php if needed.)
Artisan Command Feedback:
// In a custom Artisan command
$this->info($color->getColoredString('Task completed!', 'green'));
$this->error($color->getColoredString('Failed to migrate.', 'red'));
ConsoleTheme facade to wrap ConsoleColor for consistent styling across the app.
// app/Facades/ConsoleTheme.php
public static function warning($text) {
return (new ConsoleColor())->getColoredString($text, 'yellow');
}
echo "\033[1m" . $color->getColoredString('Important!', 'red') . "\033[0m";
Windows Compatibility:
php -r "echo (new JakubOnderka\PhpConsoleColor\ConsoleColor())->getColoredString('Test', 'green');" to test environments. Fallback to plain text if unsupported:
if (!ConsoleColor::isSupported()) {
return $text; // Fallback
}
Performance:
ConsoleColor in loops (e.g., logging thousands of rows) can slow output.Deprecation:
symfony/console (built into Laravel) or php-parallel-lint/PHP-Console-Color.if (!ConsoleColor::isSupported()) {
throw new \RuntimeException('Console colors not supported in this environment.');
}
ConsoleColor::RESET to avoid bleeding into subsequent text:
echo $color->getColoredString('Error', 'red') . ConsoleColor::RESET;
Custom Color Palette:
Extend the class to add domain-specific colors (e.g., #FF5733 for "urgent"):
class ExtendedConsoleColor extends ConsoleColor {
public function getOrangeString($text) {
return $this->getColoredString($text, '#FF5733');
}
}
Integration with Laravel’s Termwind:
For modern Laravel apps, combine with termwind for richer CLI styling:
use Termwind\Components\DIV;
DIV::new()->bgGray(800)->textGreen('Success')->render();
Testing:
Mock ConsoleColor in unit tests to avoid environment-dependent failures:
$mockColor = $this->createMock(ConsoleColor::class);
$mockColor->method('getColoredString')->willReturn('Mocked: ' . $text);
How can I help you explore Laravel packages today?