Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Laravel artisan command output on same line?

I would like to display processing progress using a simple series of dots. This is easy in the browser, just do echo '.' and it goes on the same line, but how do I do this on the same line when sending data to the artisan commandline?

Each subsequent call to $this->info('.') puts the dot on a new line.

like image 617
eComEvo Avatar asked Aug 19 '14 16:08

eComEvo


People also ask

Can we make view using command line in Laravel?

There is very easy way to create a view(blade) file with php artisan make:view {view-name} command using Laravel More Command Package.

What is Laravel command-line interface?

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

Which artisan command makes another artisan command?

To call another Artisan command and save its output you should use $this->call() from your command.


3 Answers

The method info uses writeln, it adds a newline at the end, you need to use write instead.

//in your command
$this->output->write('my inline message', false);
$this->output->write('my inline message continues', false);
like image 158
marcanuy Avatar answered Oct 24 '22 23:10

marcanuy


Probably a little bit of topic, since you want a series of dots only. But you can easily present a progress bar in artisan commands using built in functionality in Laravel.

Declare a class variable like this:

protected $progressbar;

And initialize the progress bar like this, lets say in fire() method:

$this->progressbar = $this->getHelperSet()->get('progress');
$this->progressbar->start($this->output, Model::count());

And then do something like this:

foreach (Model::all() as $instance)
{
    $this->progressbar->advance(); //do stuff before or after this
}

And finilize the progress when done by calling this:

$this->progressbar->finish();

Update: For Laravel 5.1+ The simpler syntax is even more convenient:

  1. Initialize $bar = $this->output->createProgressBar(count($foo));
  2. Advance $bar->advance();
  3. Finish $bar->finish();
like image 27
Jonas Carlbaum Avatar answered Oct 24 '22 22:10

Jonas Carlbaum


If you look at the source, you will see that $this->info is actually just a shortcut for $this->output->writeln: Source.

You could use $this->output->write('<info>.</info>') to make it inline.

If you find yourself using this often you can make your own helper method like:

public function inlineInfo($string)
{
    $this->output->write("<info>$string</info>");
}
like image 11
Igor Pantović Avatar answered Oct 24 '22 21:10

Igor Pantović