Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Cli Colors Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Direct constant usage: Use built-in constants like Color::GREEN, Color::RED, and Color::RESET inline for simple highlight needs.
  • Helper methods: The package provides Color::prefix(), Color::suffix(), and Color::apply($text, $color) for reusable color wrappers:
    echo Color::apply('Error occurred!', Color::RED) . PHP_EOL;
    
  • Background colors (added in v1.11.0): Mix foreground and background styling using constants like Color::BG_RED or Color::BG_WHITE.
  • Reusability: Create a small helper trait or class in your project to centralize colorized logging:
    trait LoggerTrait {
        public function info(string $msg): void {
            echo Color::CYAN . '[INFO] ' . Color::RESET . $msg . PHP_EOL;
        }
    }
    
  • Output abstraction: Wrap color output in functions for testability (e.g., echoColor() that handles php_sapi_name() === 'cli' checks if needed for non-cli contexts).

Gotchas and Tips

  • Console compatibility: Colors rely on ANSI escape codes. They may not render correctly on Windows CMD/PowerShell without proper terminal support or enabling ANSI support (e.g., 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.
  • Base color brightness (v1.12.0 fix): Prior to v1.12.0, base colors were visually dull due to a flag issue. Ensure you’re on ~1.12.0 or higher for vibrant colors.
  • Whitespace and escaping: Avoid using color constants inside double-quoted strings where interpolation occurs — use concatenation to prevent accidental parsing issues:
    // ✅ Good
    echo Color::RED . $msg . Color::RESET;
    
    // ⚠️ Risky
    echo "Error: $msg"; // Cannot safely inject color into variable interpolation
    
  • No formatting options: This package only handles colors — no bold, underline, or cursor control. If you need richer formatting, consider symfony/console instead, or layer on top (e.g., use this for colors, symfony/console for formatting/options).
  • Autoloading: Only uses classmap — no PSR-4. Verify vendor/composer/autoload_classmap.php includes Color.php.
  • Testing tip: In PHPUnit tests, suppress color output with putenv('NO_COLOR=1') (the package respects the NO_COLOR convention by default), ensuring predictable test output.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests