Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spin spacing between elements after erasing #77

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class ConsoleOutput extends SymfonyConsoleOutput
*/
protected int $newLinesWritten = 1;

/**
* Ignore incoming new lines, keeping the previous $newLinesWritten value.
*/
protected bool $ignoreNewLines = false;

/**
* How many new lines were written by the last output.
*/
Expand All @@ -19,6 +24,14 @@ public function newLinesWritten(): int
return $this->newLinesWritten;
}

/**
* Ignore incoming new lines, keeping the previous $newLinesWritten value.
*/
public function ignoreNewLines(bool $shouldIgnore = true): void
{
$this->ignoreNewLines = $shouldIgnore;
}

/**
* Write the output and capture the number of trailing new lines.
*/
Expand All @@ -30,6 +43,10 @@ protected function doWrite(string $message, bool $newline): void
$message .= \PHP_EOL;
}

if ($this->ignoreNewLines) {
return;
}

$trailingNewLines = strlen($message) - strlen(rtrim($message, \PHP_EOL));

if (trim($message) === '') {
Expand Down
13 changes: 13 additions & 0 deletions src/Spinner.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct(public string $message = '')
public function spin(Closure $callback): mixed
{
$this->capturePreviousNewLines();
$this->ignoreNewLines(true);

register_shutdown_function(fn () => $this->restoreCursor());

Expand Down Expand Up @@ -94,6 +95,18 @@ protected function resetTerminal(bool $originalAsync): void

$this->eraseRenderedLines();
$this->showCursor();
$this->ignoreNewLines(false);
}

/**
* If the method exists, instruct the output to ignore new lines
* as the spinner will erase itself at the end of the process.
*/
protected function ignoreNewLines(bool $ignore = true): void
{
if (method_exists(static::output(), 'ignoreNewLines')) {
static::output()->ignoreNewLines($ignore);
}
}

/**
Expand Down