diff --git a/composer.json b/composer.json index d459053..e34c0ed 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "symfony/yaml": "^4.3", "react/child-process": "^0.6.1", "ext-json": "*", - "ext-pcntl": "*" + "ext-pcntl": "*", + "alecrabbit/php-cli-snake": "^0.3.0" }, "autoload": { "psr-4": { diff --git a/php-watcher b/php-watcher index 5f092b5..6831ccf 100755 --- a/php-watcher +++ b/php-watcher @@ -2,11 +2,6 @@ add($command); $application->setDefaultCommand($command->getName(), true); $application->run(); - -function restore_cursor() { - echo "\033[?25h"; - exit; -} diff --git a/src/Screen/Screen.php b/src/Screen.php similarity index 91% rename from src/Screen/Screen.php rename to src/Screen.php index 5e26b29..39187c9 100644 --- a/src/Screen/Screen.php +++ b/src/Screen.php @@ -1,11 +1,11 @@ output = $output; - $this->spinner = new Spinner(); + $this->spinner = $spinner; } public function showOptions(WatchList $watchList): void @@ -79,6 +79,7 @@ public function subscribeToProcessOutput(Process $process): void public function showSpinner(LoopInterface $loop): void { + $this->spinner->begin(); $loop->addPeriodicTimer($this->spinner->interval(), function () { $this->spinner->spin(); }); diff --git a/src/Screen/Cursor.php b/src/Screen/Cursor.php deleted file mode 100644 index c3d91e2..0000000 --- a/src/Screen/Cursor.php +++ /dev/null @@ -1,26 +0,0 @@ -cursor = new Cursor(); - } - - public function spin(): void - { - $this->erase(); - $this->output(); - $this->increment(); - } - - public function interval(): float - { - return 1/10; - } - - public function erase(): void - { - $this->cursor->erase(); - } - - private function currentColor(): int - { - return self::COLORS[$this->currentColorIndex]; - } - - private function currentFrame(): string - { - return self::FRAMES[$this->currentFrameIndex]; - } - - private function output(): void - { - $this->cursor->write($this->currentColor(), $this->currentFrame()); - $this->cursor->hide(); - $this->cursor->startOfLine(); - } - - private function increment(): void - { - $this->currentFrameIndex ++; - if ($this->currentFrameIndex === count(self::FRAMES)) { - $this->currentFrameIndex = 0; - } - - $this->currentColorIndex ++; - if ($this->currentColorIndex === count(self::COLORS)) { - $this->currentColorIndex = 0; - } - } -} diff --git a/src/Watcher/Watcher.php b/src/Watcher/Watcher.php index 69e4f67..ab15891 100644 --- a/src/Watcher/Watcher.php +++ b/src/Watcher/Watcher.php @@ -5,7 +5,7 @@ use React\ChildProcess\Process; use React\EventLoop\LoopInterface; use seregazhuk\PhpWatcher\Filesystem\ChangesListener; -use seregazhuk\PhpWatcher\Screen\Screen; +use seregazhuk\PhpWatcher\Screen; final class Watcher { diff --git a/src/WatcherCommand.php b/src/WatcherCommand.php index 7536bdb..08ed889 100644 --- a/src/WatcherCommand.php +++ b/src/WatcherCommand.php @@ -2,11 +2,11 @@ namespace seregazhuk\PhpWatcher; +use AlecRabbit\Snake\Spinner; use React\ChildProcess\Process; use React\EventLoop\Factory; use seregazhuk\PhpWatcher\Config\Builder; use seregazhuk\PhpWatcher\Filesystem\ChangesListener; -use seregazhuk\PhpWatcher\Screen\Screen; use seregazhuk\PhpWatcher\Watcher\Watcher; use Symfony\Component\Console\Command\Command as BaseCommand; use Symfony\Component\Console\Input\InputArgument; @@ -34,10 +34,12 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output) { - $config = (new Builder())->build($input); $loop = Factory::create(); + $loop->addSignal(SIGINT, [$this, 'stop']); + $loop->addSignal(SIGTERM, [$this, 'stop']); - $screen = new Screen(new SymfonyStyle($input, $output)); + $config = (new Builder())->build($input); + $screen = new Screen(new SymfonyStyle($input, $output), new Spinner()); $filesystem = new ChangesListener($loop, $config->watchList()); $watcher = new Watcher($loop, $screen, $filesystem); @@ -45,4 +47,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $process = new Process($config->command()); $watcher->startWatching($process, $config->signal(), $config->delay()); } + + public function stop(): void + { + (new Spinner())->end(); + exit(); + } }