How to display the output of the Laravel artisan command on the same line?

I would like to display the processing using a simple series of points. It's simple in the browser, just do echo '.' , and it goes on one line, but how to do it on the same line when sending data to the artisan command line?

Each subsequent call to $this->info('.') Places a point on a new line.

+6
source share
3 answers

The method information uses writeln, it adds a new line at the end, you need to use a record instead.

 //in your command $this->output->write('my inline message', false); $this->output->write('my inline message continues', false); 
+9
source

There is probably a bit of a theme since you only want a series of dots. But you can easily imagine the progress bar in the wizard commands using the built-in functions in Laravel.

Declare a class variable as follows:

 protected $progressbar; 

And initialize the progress bar like this, say in the 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 complete the progress by doing this:

 $this->progressbar->finish(); 

Update: for Laravel 5.1 + A simpler syntax is even more convenient:

  • Initialize $bar = $this->output->createProgressBar(count($foo));
  • Advance $bar->advance();
  • Finish $bar->finish();
+12
source

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

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

If you use this often, you can create your own helper method, for example:

 public function inlineInfo($string) { $this->output->write("<info>$string</info>"); } 
+4
source

Source: https://habr.com/ru/post/974037/


All Articles