eComEvo
eComEvo

Reputation: 12559

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.

Upvotes: 22

Views: 19965

Answers (4)

savgoran
savgoran

Reputation: 53

\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|

Upvotes: -1

Jonas Carlbaum
Jonas Carlbaum

Reputation: 577

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

Upvotes: 22

marcanuy
marcanuy

Reputation: 23972

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

Upvotes: 30

Igor Pantović
Igor Pantović

Reputation: 9246

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

Upvotes: 12

Related Questions