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 Color Output Laravel Package

jfcherng/php-color-output

Lightweight PHP utility to add ANSI colors and styles to CLI output. Provides named foreground/background styles, special effects (bold, underline, blink, reverse), aliases, and a simple CliColor::color() helper to format strings for terminals.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer with composer require jfcherng/php-color-output. Begin by using the core CliColor class: inject or import \Jfcherng\Utility\CliColor, then call CliColor::color($string, $colors) to apply ANSI formatting. Start simple — wrap a single string like CliColor::color('Hello', 'f_red') to see colored output in your CLI. For multi-style strings, pass comma-separated strings ('f_white,b_blue') or arrays (['f_white', 'b_blue']). Always test in your target shell environment — especially Windows (ensure ANSI support via color in cmd.exe or use winpty, or consider enabling --ansi in phpunit runs).

Implementation Patterns

  • Per-command styling in CLI tools: Wrap output in CliColor::color() calls inside Symfony\Component\Console\Command\Command subclasses — e.g., for success/error messages: echo CliColor::color('✓ Success!', 'f_green, b'), while maintaining consistency with style tokens.
  • Log decoration: Interleave with PSR-3 loggers by decorating messages before writing: Logger::info(CliColor::color($msg, 'f_cyan')) or add a custom monolog handler that processes records.
  • Progress and status bars: Use color() in loops for dynamic status: echo CliColor::color("Processing {$i}/{$total}...", 'f_yellow'), "\r";.
  • Reusable color constants: Define your app’s color palette in a config class (e.g., self::SUCCESS = 'f_green,b';) to centralize styling.
  • Conditional stripping for logs: Use CliColor::noColor() before writing to files or remote logs: file_put_contents('debug.log', CliColor::noColor($coloredOutput)).

Gotchas and Tips

  • No global functions since v3: Do not use legacy global color() or noColor() — they were removed in v3.0.0. Always use \Jfcherng\Utility\CliColor::color(...).
  • Windows limitations: Default Windows consoles (pre-Insider builds) may not support ANSI codes. Use --ansi flag when invoking PHP or consider bentools/terminfo or squizlabs/php_codesniffer’s phpcbf’s color handling patterns as fallback.
  • Nested styles must be manually reset: If using $reset = false, remember to append 'reset' manually: CliColor::color('Start', 'f_blue', false) . CliColor::color('End', 'reset').
  • Alias shorthand: Use aliases like 'b', 'blk', 'u', 'h', 'rev', 'rst' instead of full names — they’re shorter and widely adopted (e.g., 'b' for bold).
  • No automatic line breaks: color() only formats the string — add PHP_EOL or \n yourself where needed; avoid "foo\n" without \r on Windows if cross-platform compatibility is required.
  • Extensibility via traits: Since it’s a utility class, avoid subclassing — instead compose via traits or helper methods in your project’s Terminal service class for consistent, app-wide formatting.
  • Test color stripping: Write unit tests for both colored and noColor() outputs — ensure downstream processing (e.g., file I/O, APIs) doesn’t break on unescaped ANSI codes.
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
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
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