CLI spinner processing
| From: | Daevid Vincent | Date: | Wed, 14 Nov 2007 08:36:18 +0000 |
| Subject: | CLI spinner processing | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-264478@lists.php.net to get a copy of this message | ||
Check out this cool little snippet for use in CLI scripts. It shows processing with a spinner. It
took me a little while to figure out the magic combination of the cursor array and ternary operator.
My main goal was to make it as self contained and efficient as possible. I am importing millions of
records, and didn't want to waste CPU time on computing modulus on some huge $i value. :)
--------------------- 8< SNIP >8 ----------------------------------------------
#!/usr/bin/php -q
<?php
define('ESC', 27);
printf( "%c[2J", ESC ); //clear screen
//note how the '-' is at [1], not [0]
$cursorArray =
array('/','-','\\','|','/','-','\\','|');
echo "Processing: ";
printf( "%c7", ESC ); //save cursor position
for ($count = 1; $count <= 30; $count++)
{
//note that $i is all self contained.
printf("%c8(".$cursorArray[ (($i++ > 7) ? ($i = 1) : ($i % 8)) ].") %02d",
ESC, $count); //restore cursor position and print
sleep(1);
}
echo "\nDone.\n";
?>