Fun at the Laravel Console

by webmaster 2020-02-10 #laravel
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.

Livewire ASCII logo

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

Command gist

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.

Command gist

Reference

Let's take a closer look at what colors and styles are available and how they can be applied.

Foreground colors

Symfony available foreground colors

Usage:

$this->line('<fg=magenta>Magenta text</>');

Background colors

Symfony available background colors

Usage:

$this->line('<bg=blue>Blue background</>');

Options

Symfony available options

Usage:

$this->line('<options=bold>Bold text</>');

Styles

Symfony predefined 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.

Custom color/style combos

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!

Liked this article? Share it on your favorite platform.