We neede more colors ! On Artisan commands !!
Tags : Laravel4
Do you wana to be more colorful your Artisan command? Did you hate funny <info>...</info> syntanx? If so, make stuff simple. Add your color.
make base command
Maybe many Laravel developer use base command class, then extend it. (How about you? ) If you didn't, just make it. (If you have it, so just add run() method.)
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class BaseCommand extends Command {
public function run( InputInterface $input, OutputInterface $output )
{
// Set extra colors.
// The most problem is $output->getFormatter() don't work...
// So create new formatter to add extra color.
$formatter = new OutputFormatter( $output->isDecorated() );
$formatter->setStyle( 'red', new OutputFormatterStyle( 'red', 'black' ) );
$formatter->setStyle( 'green', new OutputFormatterStyle( 'green', 'black' ) );
$formatter->setStyle( 'yellow', new OutputFormatterStyle( 'yellow', 'black' ) );
$formatter->setStyle( 'blue', new OutputFormatterStyle( 'blue', 'black' ) );
$formatter->setStyle( 'magenta', new OutputFormatterStyle( 'magenta', 'black' ) );
$formatter->setStyle( 'yellow-blue', new OutputFormatterStyle( 'yellow', 'blue' ) );
$output->setFormatter( $formatter );
$result = parent::run( $input, $output );
}
}
Maybe, you don't need to read Symfony 2 docs. The codes tell what did. Very simple, don't worst your time to know everything. Just copy them. :D
You can add more your colors. ;)
Then use in read command like this :
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ColorOutputCommand extends BaseCommand {
/**
* The console command name.
*
* @var string
*/
protected $name = 'color:color';
/**
* The console command description.
*
* @var string
*/
protected $description = 'A sample color output.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->line('<green>Green output.</green>');
$this->line('<yellow-blue>Yellow output on Blue back.</yellow-blue>');
return 0;
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array( );
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array( );
}
}
Pretty simple. Have fun. (Who complained that using escape sequences is simpler?? :D )