1

I am writing a PHP script which will be run with the shell command in myfile.php.
I want to show a progress bar which changes in time like this:

[------]

[*-----]

[****--]

i don't know if this is the best practice to do such a thing, but I want do it using a simple echo, and the position of the progress bar in the shell unchanged.

After it's done, I want to delete the whole loading thing.

Generally, how can I change something I have already echo'ed?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
shampoo
  • 1,173
  • 12
  • 22

2 Answers2

1

What you need to use are ANSI Escape Sequences.

This question and answer on StackOverflow cover it and you should be able to adapt it to your needs, take a look at: Update Command-line Output, i.e. for Progress

Community
  • 1
  • 1
shannonman
  • 841
  • 4
  • 8
0

chr(8) is the backspace character. With a little math, you could print enough of them to move the cursor back to the start of your bar, and print the new data over top of where you printed before.

Just dont print a newline at the end of your bar.

Problem with this: if you do any sort of text capture on your output, it will pick these up into the captured text, but it will look fine in the console window

Uberfuzzy
  • 8,253
  • 11
  • 42
  • 50