├── LICENSE ├── README.md ├── composer.json ├── config └── console-spinner.php └── src ├── LaravelConsoleSpinnerServiceProvider.php ├── Spinner.php └── SpinnerMixin.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Rahul Dey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Console Spinner 2 | Laravel Console Spinner was created by [Rahul Dey](https://github.com/RahulDey12). It is just a custom Progress Bar inspired by [icanhazstring/symfony-console-spinner](https://github.com/icanhazstring/symfony-console-spinner). 3 | 4 | [![StyleCI Status](https://github.styleci.io/repos/399605115/shield)](https://styleci.io/repos/399605115) 5 | [![Total Downloads](http://poser.pugx.org/rahul900day/laravel-console-spinner/downloads)](https://packagist.org/packages/rahul900day/laravel-console-spinner) 6 | [![Version](http://poser.pugx.org/rahul900day/laravel-console-spinner/version)](https://packagist.org/packages/rahul900day/laravel-console-spinner) 7 | [![PHP Version Require](http://poser.pugx.org/rahul900day/laravel-console-spinner/require/php)](https://packagist.org/packages/rahul900day/laravel-console-spinner) 8 | 9 | ## Installation 10 | > **Requires [PHP 7.3+](https://php.net/releases/)** 11 | 12 | Via [Composer](https://getcomposer.org): 13 | 14 | ```bash 15 | composer require rahul900day/laravel-console-spinner 16 | ``` 17 | 18 | You can publish the config file with: 19 | ```bash 20 | php artisan vendor:publish --tag=console-spinner-config 21 | ``` 22 | 23 | This is the contents of the published config file: 24 | 25 | ```php 26 | return [ 27 | 'chars' => ['⠏', '⠛', '⠹', '⢸', '⣰', '⣤', '⣆', '⡇'], 28 | ]; 29 | ``` 30 | 31 | ## Usage 32 | ```php 33 | class SimpleLaravelCommand extends Command 34 | { 35 | /** 36 | * Execute the console command. 37 | * 38 | * @return void 39 | */ 40 | public function handle() 41 | { 42 | $spinner = $this->spinner($users->count()); 43 | $spinner->setMessage('Loading...'); 44 | $spinner->start(); 45 | 46 | foreach ($users as $user) { 47 | // Do your stuff... 48 | 49 | $spinner->advance(); 50 | } 51 | $spinner->finish(); 52 | } 53 | } 54 | ``` 55 | The `$spinner` is compatible with Symfony `ProgressBar`, so you can run any method of this class. 56 | 57 | Or you can also use with `withSpinner` method by giving an iterable. 58 | ```php 59 | $this->withSpinner(User::all(), function($user) { 60 | // Do your stuff with $user 61 | }, 'Loading...'); 62 | ``` 63 | 64 | ## Licence 65 | This package is released under the [MIT license](https://github.com/RahulDey12/laravel-console-spinner/blob/master/LICENSE). 66 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rahul900day/laravel-console-spinner", 3 | "description": "Laravel Console Spinner is a spinner output for Laravel command line.", 4 | "keywords": [ 5 | "console", 6 | "command-line", 7 | "php", 8 | "cli", 9 | "laravel", 10 | "artisan", 11 | "symfony" 12 | ], 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Rahul Dey", 17 | "email": "rahul900day@gmail.com" 18 | } 19 | ], 20 | "require": { 21 | "php": "^7.3|^8.0", 22 | "illuminate/console": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 23 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "Rahul900Day\\LaravelConsoleSpinner\\": "src/" 28 | } 29 | }, 30 | "extra": { 31 | "laravel": { 32 | "providers": [ 33 | "Rahul900Day\\LaravelConsoleSpinner\\LaravelConsoleSpinnerServiceProvider" 34 | ] 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /config/console-spinner.php: -------------------------------------------------------------------------------- 1 | ['⠏', '⠛', '⠹', '⢸', '⣰', '⣤', '⣆', '⡇'], 11 | ]; 12 | -------------------------------------------------------------------------------- /src/LaravelConsoleSpinnerServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 13 | __DIR__.'/../config/console-spinner.php' => config_path('console-spinner.php'), 14 | ], 'console-spinner-config'); 15 | 16 | Command::mixin(new SpinnerMixin()); 17 | } 18 | 19 | public function register() 20 | { 21 | $this->mergeConfigFrom( 22 | __DIR__.'/../config/console-spinner.php', 23 | 'console-spinner' 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Spinner.php: -------------------------------------------------------------------------------- 1 | step = 0; 27 | $this->chars = config('console-spinner.chars'); 28 | 29 | $this->progressBar = $output->createProgressBar($max); 30 | $this->progressBar->setBarCharacter('✔'); 31 | $this->progressBar->setProgressCharacter($this->chars[0]); 32 | $this->progressBar->setMessage(''); 33 | $this->progressBar->setFormat('%bar% %message%'); 34 | $this->progressBar->setBarWidth(1); 35 | $this->progressBar->setRedrawFrequency(31); 36 | } 37 | 38 | /** 39 | * @return \Symfony\Component\Console\Helper\ProgressBar 40 | */ 41 | public function getOriginalProgressBar() 42 | { 43 | return $this->progressBar; 44 | } 45 | 46 | public function __call($name, $arguments) 47 | { 48 | return call_user_func_array([$this->progressBar, $name], $arguments); 49 | } 50 | 51 | public function advance(int $step = 1) 52 | { 53 | $this->step += $step; 54 | $this->progressBar->setProgressCharacter($this->chars[$this->step % count($this->chars)]); 55 | $this->progressBar->advance($step); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/SpinnerMixin.php: -------------------------------------------------------------------------------- 1 | output, $max); 14 | }; 15 | } 16 | 17 | protected function withSpinner() 18 | { 19 | return function ($totalSteps, \Closure $callback, string $message = '', array $options = []) { 20 | $spinner = $this->spinner( 21 | is_iterable($totalSteps) ? count($totalSteps) : $totalSteps 22 | ); 23 | $spinner->setMessage($message); 24 | 25 | // Set more options 26 | foreach ($options as $option => $args) { 27 | $method = Str::startsWith($option, 'set') ? Str::camel($option) : 'set'.Str::studly($option); 28 | call_user_func_array([$spinner, $method], Arr::wrap($args)); 29 | } 30 | 31 | $spinner->start(); 32 | 33 | if (is_iterable($totalSteps)) { 34 | foreach ($totalSteps as $item) { 35 | $callback($item, $spinner); 36 | 37 | $spinner->advance(); 38 | } 39 | } else { 40 | $callback($spinner); 41 | } 42 | 43 | $spinner->finish(); 44 | 45 | if (is_iterable($spinner)) { 46 | return $totalSteps; 47 | } 48 | }; 49 | } 50 | } 51 | --------------------------------------------------------------------------------