Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Screen/Cursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace seregazhuk\PhpWatcher\Screen;

final class Cursor
{
public function startOfLine(): void
{
echo "\033[1D";
}

public function erase(): void
{
echo "\033[1K";
}

public function write(int $foregroundColor, string $text): void
{
echo "\e[38;5;{$foregroundColor}m{$text}\e[0m";
}

public function hide(): void
{
echo "\033[?25l";
}
}
1 change: 0 additions & 1 deletion src/Screen/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function start(string $command): void

public function restarting(string $command): void
{
$this->output->writeln('');
$this->spinner->erase();
$this->output->writeln('');
$this->info('restarting due to changes...');
Expand Down
24 changes: 14 additions & 10 deletions src/Screen/Spinner.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,21 @@ final class Spinner
197
];

private $cursor;

private $currentFrameIndex = 0;
private $currentColorIndex = 0;

public function __construct()
{
$this->cursor = new Cursor();
}

public function spin(): void
{
$this->erase();
$this->output();
$this->increment();
$this->hideCursor();
}

public function interval(): float
Expand All @@ -85,12 +92,12 @@ public function interval(): float

public function erase(): void
{
echo "\033[1K";
$this->cursor->erase();
}

private function currentColor(): string
private function currentColor(): int
{
return '38;5;' . self::COLORS[$this->currentColorIndex]. 'm';
return self::COLORS[$this->currentColorIndex];
}

private function currentFrame(): string
Expand All @@ -100,12 +107,9 @@ private function currentFrame(): string

private function output(): void
{
echo "\e[{$this->currentColor()}{$this->currentFrame()}\e[0m\r";
}

private function hideCursor(): void
{
echo "\033[?25l";
$this->cursor->write($this->currentColor(), $this->currentFrame());
$this->cursor->hide();
$this->cursor->startOfLine();
}

private function increment(): void
Expand Down