PHP Classes

PHP CLI Spinner: Make the cursor spin in the command line shells

Recommend this page to a friend!
  Info   View files Example   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 68 This week: 1All time: 10,286 This week: 571Up
Version License PHP version Categories
spinner 1.0.0Custom (specified...5PHP 5, Console
Description 

Author

This package can make the cursor spin in the command line shells.

It can start a parallel process that will change the character that appears in the current cursor position in the command-line shell to make it appear as an animated cursor.

The package provides theme classes that implement different styles of animations to be used as characters that appear spinning near the cursor.

Innovation Award
PHP Programming Innovation award nominee
June 2021
Number 3
Sometimes applications need to execute tasks that take a long time to finish. It is always good to provide feedback to the users to let them know what is happening, avoiding that the users become too anxious.

For console-based applications, usually, a cursor may appear blinking, but that does not give feedback on whether the current application is running well.

This package implements a better solution by showing the cursor changing its character, so it appears to be animated. This can give the users a better feeling by letting them know that the application is running.

The package implements a solution that uses a parallel process to animate the cursor character, so the application does not have to stop its main job to update the cursor animation.

Manuel Lemos
Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Age: 21
All time rank: 276644 in Turkey Turkey
Week rank: 50 Up2 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 13x

Example

<?php

use IsaEken\Spinner\Helpers;
use
IsaEken\Spinner\LockFile;

require_once
__DIR__ . '/../vendor/autoload.php';

/**
 * Create the lock file instance.
 */
$lockFile = LockFile::getInstance()->unserialize();

/**
 * Calculate the frame speed.
 */
$frame_speed = $lockFile->get('frame_speed') * 1000000;

/**
 * Get the theme class.
 */
$theme_class = $lockFile->get('theme_class');

/**
 * Create the frames array.
 */
$frames = $theme_class::frames();

/**
 * Set current frame index.
 */
$frame = 0;

/**
 * Update the frame while the script is running.
 */
while (true) {

   
/**
     * Create the frame line.
     */
   
$line = sprintf(
       
'%s%s%s %s',
       
chr(27),
       
'[0G',
       
$frames[$frame],
       
$lockFile->unserialize()->get('title'),
    );

   
/**
     * Print the created frame line.
     */
   
print $line;

   
/**
     * Cleanup printed line.
     */
   
for ($i = Helpers::getTerminalWidth() - mb_strlen($line); $i > 0; $i--) {
        print
' ';
    }

   
/**
     * Update frame index.
     */
   
$frame++;
    if (
$frame >= count($frames)) {
       
$frame = 0;
    }

   
/**
     * Wait the next frame.
     */
   
usleep($frame_speed);
}


Details

PHP Spinner

> Elegant spinner for interactive CLI apps. > > Alternative for https://github.com/sindresorhus/elegant-spinner

Spinner

$result = \IsaEken\Spinner\Spinner::run(function () {
    \IsaEken\Spinner\Spinner::setTitle('Calculating...');
    $a = 1;
    $b = 2;
    $c = $a + $b;
    \IsaEken\Spinner\Spinner::setTitle('Waiting...');
    sleep($c);
    return $c;
});

echo "The result is: $result!";

Installation

You can install using composer.

composer require isaeken/spinner

License

The MIT License (MIT). Please see License File for more information.


  Files folder image Files  
File Role Description
Files folder imagebin (2 files)
Files folder imageexample (3 files)
Files folder imagesrc (4 files, 3 directories)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file README.md Doc. Documentation

  Files folder image Files  /  bin  
File Role Description
  Plain text file get_width.bat Data Auxiliary data
  Plain text file spin.php Example Example script

  Files folder image Files  /  example  
File Role Description
  Image file example.gif Data Auxiliary data
  Plain text file example.php Class Class source
  Plain text file spinner.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageEnums (2 files)
Files folder imageInterfaces (1 file)
Files folder imageThemes (2 files)
  Plain text file Helpers.php Class Class source
  Plain text file LockFile.php Class Class source
  Plain text file PhpProcess.php Class Class source
  Plain text file Spinner.php Class Class source

  Files folder image Files  /  src  /  Enums  
File Role Description
  Plain text file OperatingSystem.php Class Class source
  Plain text file Status.php Class Class source

  Files folder image Files  /  src  /  Interfaces  
File Role Description
  Plain text file ThemeInterface.php Class Class source

  Files folder image Files  /  src  /  Themes  
File Role Description
  Plain text file ClassicTheme.php Class Class source
  Plain text file DefaultTheme.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:68
This week:1
All time:10,286
This week:571Up