Fun at the Laravel Console
When I spun up a new Livewire component with artisan
(Laravel's CLI tool) recently, I was intrigued enough by the cute ASCII logo to take a look at the source code to see how it was made.
What interested me the most was not the logo itself, but rather the custom colors. I admit I haven't dug deep before into what makes these console commands tick. To my knowledge, Laravel doesn't offer custom colors out of the box.
Enter Symfony's Console Formatter
I already knew that Laravel's console uses Symfony's Console Output Formatter package(s) under the hood, which in turn offer a variety of colors and styles that you can apply to your output.
It's all downhill from here
Armed with this knowledge, the <fg=cyan>...</>
tags in Livewire's code made perfect sense now.
For my future convenience I created the following two commands:
1. List all the default Symfony styles
2. Generate a Ghostbusters logo
The Ghostbusters logo was copied from this lovely repository of ASCII art. The Laravel console command in the gist generates the colored logo in the main article image.
Reference
Let's take a closer look at what colors and styles are available and how they can be applied.
Foreground colors
Usage:
$this->line('<fg=magenta>Magenta text</>');
Background colors
Usage:
$this->line('<bg=blue>Blue background</>');
Options
Usage:
$this->line('<options=bold>Bold text</>');
Styles
Usage (each style is its own individual tag):
$this->line('<question>question</question>');
Custom combos
You may certainly combine the above to produce custom effects.
Usage:
$this->line('<fg=blue;options=blink;bg=yellow>blue text on yellow background</>');
$this->line('Clickable URL: <href=https://github.com;fg=blue;options=underscore>github.com</>');
I don't know about you but I'll be sure to make my future Laravel console command output more colorful!