label = $label; } if (count($characters) < 1) { $characters = []; $size = 5; $positions = array_merge(range(0, $size - 1), range($size - 2, 1, -1)); foreach ($positions as $pos) { $line = str_repeat("-", $size); $characters[] = "[" . substr($line, 0, $pos) . "=" . substr($line, $pos + 1) . "]"; } } $this->characters(...$characters); } /** * Set the length of time to wait between drawing each stage. * * @param float $timeLimit * * @return Spinner */ public function timeLimit($timeLimit) { $this->timeLimit = (float) $timeLimit; return $this; } /** * Set the character to loop around. * * @param string $characters * * @return Spinner */ public function characters(...$characters) { if (count($characters) < 1) { throw new UnexpectedValueException("You must specify the characters to use"); } $this->characters = $characters; return $this; } /** * Re-writes the spinner * * @param string $label * * @return void */ public function advance($label = null) { if ($label === null) { $label = $this->label; } if ($this->lastDrawn) { $time = microtime(true) - $this->lastDrawn; if ($time < $this->timeLimit) { return; } } ++$this->current; if ($this->current >= count($this->characters)) { $this->current = 0; } $characters = $this->characters[$this->current]; $this->drawSpinner($characters, $label); $this->lastDrawn = microtime(true); } /** * Draw the spinner * * @param string $characters * @param string $label */ private function drawSpinner($characters, $label) { $spinner = ""; if ($this->firstLine) { $this->firstLine = false; } else { $spinner .= $this->util->cursor->up(1); $spinner .= $this->util->cursor->startOfCurrentLine(); $spinner .= $this->util->cursor->deleteCurrentLine(); } $spinner .= trim("{$characters} {$label}"); $this->output->write($this->parser->apply($spinner)); } }