Running PHP from the command line? CLImate is your new best bud.
CLImate allows you to easily output colored text, special formats, and more.
- Installation
- Basic Usage
- Colors
- Backgrounds
- Style Combinations
- Tags
- Custom Colors
- Commands
- Custom Commands
- Tables
- Borders
- Progress Bar
- JSON
- Dump
- Flanking
- Breaks
- Laravel Users
Using composer:
{
"require": {
"joetannenbaum/climate": "0.1.*"
}
}
The out
method simply receives a string that will output on a new line:
require_once('vendor/autoload.php');
$climate = new JoeTannenbaum\CLImate\CLImate;
$climate->out('This prints to the terminal.');
$climate->out('This prints to the terminal.')->out('This will be on a new line');
And you can do that. But that's not very fun.
There are many pre-defined colors at your disposal:
- Black
- Red
- Green
- Yellow
- Blue
- Magenta
- Cyan
- Light Gray
- Dark Gray
- Light Red
- Light Green
- Light Yellow
- Light Blue
- Light Magenta
- Light Cyan
- White
$climate->red('Whoa now this text is red.');
$climate->blue('Blue? Wow!');
$climate->lightGreen('It\'s not easy being (light) green.');
If you prefer, you can also simply chain the color method and continue using out
:
$climate->red()->out('Whoa now this text is red.');
$climate->blue()->out('Blue? Wow!');
$climate->lightGreen()->out('It\'s not easy being (light) green.');
To to apply a color as a background, simply prepend the color method with background
:
$climate->backgroundRed('Whoa now this text is red.');
$climate->backgroundRed()->out('Whoa now this text is red.');
$climate->backgroundBlue()->out('Blue? Wow!');
$climate->backgroundLightGreen()->out('It\'s not easy being (light) green.');
You have several formatting options:
- Bold
- Dim
- Underline
- Blink
- Invert
- Hidden
To apply a format:
$climate->bold('Bold and beautiful.');
$climate->underline('I have a line beneath me.');
$climate->bold()->out('Bold and beautiful.');
$climate->underline()->out('I have a line beneath me.');
You can apply multiple formats by chaining them:
$climate->bold()->underline()->out('Bold (and underlined) and beautiful.');
$climate->blink()->dim('Dim. But noticeable.');
You can chain any of the above to get what you want:
$climate->backgroundBlue()->green()->blink()->out('This may be a little hard to read.');
Feeling wild? Just throw them all into one method:
$climate->backgroundBlueGreenBlink('This may be a little hard to read.');
$climate->backgroundBlueGreenBlinkJson([
'this' => 'is some colorful json output'
]);
You can apply more than one format to an output, but only one foreground and one background color. Unless you use...
You can also just apply a color/background color/format to part of an output:
$climate->blue('Please <light_red>remember</light_red> to restart the server.');
$climate->out('Remember to use your <blink><yellow>blinker</yellow></blink> when turning.');
You can use any of the color or formatting keywords (snake cased) as tags.
To use a background color tag, simply prepend the color with background_
:
$climate->blue('Please <bold><background_light_red>remember</background_light_red></bold> to restart the server.');
You can add your own custom colors:
$climate->style->addColor('lilac', 38);
Once you've added the color, you can use it like any of the other colors:
$climate->lilac('What a pretty color.');
$climate->backgroundLilac()->out('This background is a pretty color.');
$climate->out('Just this <lilac>word</lilac> is a pretty color.');
$climate->out('Just this <background_lilac>word</background_lilac> is a pretty color.');
Commands are simply pre-defined colors for specific output:
$climate->error('Ruh roh.');
$climate->comment('Just so you know.');
$climate->whisper('Not so important, just a heads up.');
$climate->shout('This. This is important.');
$climate->info('Nothing fancy here. Just some info.');
You can add your own command, just make sure that the color is defined already.
$climate->style->addCommandColor('rage', 'cyan');
$climate->rage('SOMETHING IS MESSED UP.');
You can also override any command;
$climate->style->addCommandColor('error', 'light_blue');
$climate->error('Whelp. That did not turn out so well.');
The table
method can receive any of the following:
- Array of arrays
- Array of associative arrays
- Array of objects
$climate->table([
[
'Walter White',
'Father',
'Teacher',
],
[
'Skyler White',
'Mother',
'Accountant',
],
[
'Walter White Jr.',
'Son',
'Student',
],
]);
------------------------------------------
| Walter White | Father | Teacher |
------------------------------------------
| Skyler White | Mother | Accountant |
------------------------------------------
| Walter White Jr. | Son | Student |
------------------------------------------
$climate->table([
[
'name' => 'Walter White',
'role' => 'Father',
'profession' => 'Teacher',
],
[
'name' => 'Skyler White',
'role' => 'Mother',
'profession' => 'Accountant',
],
[
'name' => 'Walter White Jr.',
'role' => 'Son',
'profession' => 'Student',
],
]);
------------------------------------------
| name | role | profession |
==========================================
| Walter White | Father | Teacher |
------------------------------------------
| Skyler White | Mother | Accountant |
------------------------------------------
| Walter White Jr. | Son | Student |
------------------------------------------
As with other methods, you can style a table as well. So all of the following works:
$climate->redTable([
[
'name' => 'Walter White',
'role' => 'Father',
'profession' => 'Teacher',
],
[
'name' => 'Skyler White',
'role' => 'Mother',
'profession' => 'Accountant',
],
]);
$climate->red()->table([
[
'name' => 'Walter White',
'role' => 'Father',
'profession' => 'Teacher',
],
[
'name' => 'Skyler White',
'role' => 'Mother',
'profession' => 'Accountant',
],
]);
$climate->table([
[
'name' => 'Walter White',
'role' => '<light_blue>Father</light_blue>',
'profession' => 'Teacher',
],
[
'name' => 'Skyler White',
'role' => 'Mother',
'profession' => '<red>Accountant</red>',
],
]);
If you want to insert a border to break up output, simply use the border
method. By default, border
outputs a dashed border with 100 characters in it
$climate->border();
// ----------------------------------------------------------------------------------------------------
The border
method takes two arguments:
- Character(s) to be repeated
- Length of the border
$climate->border('*');
// ****************************************************************************************************
$climate->border('-*-');
// -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--
$climate->border('-*-', 50);
// -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*
As with the other methods, feel free to style the border:
$climate->red()->border();
$climate->redBorder();
$climate->bold()->backgroundBlue()->border();
Easily add a progress bar to your output:
$progress = $climate->progress()->total(100);
for ( $i = 0; $i <= 100; $i++ )
{
$progress->current( $i );
usleep(80000);
}
Which will result in:
You can also shorthand it a bit if you'd like:
$climate->progress(100);
As with everything else, style it however you like:
$climate->redProgress(100);
$climate->redBoldProgress(100);
The json
method outputs some pretty-printed JSON to the terminal:
$climate->json([
'name' => 'Gary',
'age' => 52,
'job' => 'Engineer',
]);
{
"name": "Gary",
"age": 52,
"job": "Engineer"
}
As with the other method, you can style this output as well:
$climate->redJson([
'name' => 'Gary',
'age' => 52,
'job' => 'Engineer',
]);
$climate->red()->json([
'name' => 'Gary',
'age' => 52,
'job' => '<blink>Engineer</blink>',
]);
$climate->underline()->json([
'name' => 'Gary',
'age' => 52,
'job' => 'Engineer',
]);
The dump
method allows you to var_dump
variables out to the terminal:
$climate->dump([
'This',
'That',
'Other Thing',
]);
Which results in:
array(3) {
[0]=>
string(4) "This"
[1]=>
string(4) "That"
[2]=>
string(11) "Other Thing"
}
But why not make it look cool:
$climate->errorDump([
'This',
'That',
'Other Thing',
]);
$climate->blinkDump([
'This',
'That',
'Other Thing',
]);
The flank
method allows you to bring a little more attention to a line:
$climate->flank('Look at me. Now.');
/// ### Look at me. Now. ###
You can specify the flanking characters:
$climate->flank('Look at me. Now.', '!');
/// !!! Look at me. Now. !!!
And how many flanking characters there should be:
$climate->flank('Look at me. Now.', '!', 5);
/// !!!!! Look at me. Now. !!!!!
As with the other method, you can style this output as well:
$climate->blink()->flank('Look at me. Now.');
$climate->blinkFlank('Look at me. Now.');
The br
method does exactly that, inserts a line break:
$climate->br();
For ease of use, the br
method is chainable:
$climate->br()->out('I have moved down a line.');
Use Laravel? Treat time. Add these lines to your app/config/app.php
:
'providers' => [
'...',
'JoeTannenbaum\CLImate\CLImateServiceProvider'
];
'aliases' => [
'...',
'CLImate' => 'JoeTannenbaum\CLImate\Facade\CLImate'
];
You can now any of the above methods via Laravel's facades:
CLImate::error('Ruh roh.');
CLImate::comment('Just so you know.');
CLImate::whisper('Not so important, just a heads up.');
CLImate::shout('This. This is important.');
CLImate::info('Nothing fancy here. Just some info.');