22

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.

4 Answers 4

32

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);
Sign up to request clarification or add additional context in comments.

1 Comment

use $this->command->getOutput()->write(//... inside of a seeder
22

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();

4 Comments

No problems, I was googling the same questions a while ago, and stumbled upon the progress bar solution, allthough poorly documented, if documented at all. Thought this answer might help someone find it faster, since it's a convenient way of presenting progress in a nice way.
Is there a way to display commandline output, such as with $this->error(), that will not disrupt the progressbar?
You can get a progressbar instance if >5.2 with new \Symfony\Component\Console\Helper\ProgressBar($this->output)
@eComEvo did you figure out a way to solve your problem? I am facing same issue
12

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>");
}

Comments

-1

\r is Carriage return
\n is Newline

So you can use:

echo "something\r";

to write in same line to display progress, for example. After that you can call "\n|

2 Comments

please pay attention to the question.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.