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

[Console] cursor tweaks #36433

Merged
merged 4 commits into from
Apr 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 53 additions & 22 deletions src/Symfony/Component/Console/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
/**
* @author Pierre du Plessis <pdples@gmail.com>
*/
class Cursor
final class Cursor
{
private $output;

private $input;

public function __construct(OutputInterface $output, $input = STDIN)
Expand All @@ -28,82 +27,114 @@ public function __construct(OutputInterface $output, $input = STDIN)
$this->input = $input;
}

public function moveUp(int $lines = 1)
public function moveUp(int $lines = 1): self
fabpot marked this conversation as resolved.
Show resolved Hide resolved
{
$this->output->write(sprintf("\x1b[%dA", $lines));

return $this;
}

public function moveDown(int $lines = 1)
public function moveDown(int $lines = 1): self
{
$this->output->write(sprintf("\x1b[%dB", $lines));

return $this;
}

public function moveRight(int $columns = 1)
public function moveRight(int $columns = 1): self
{
$this->output->write(sprintf("\x1b[%dC", $columns));

return $this;
}

public function moveLeft(int $columns = 1)
public function moveLeft(int $columns = 1): self
{
$this->output->write(sprintf("\x1b[%dD", $columns));

return $this;
}

public function moveToColumn(int $column)
public function moveToColumn(int $column): self
{
$this->output->write(sprintf("\x1b[%dG", $column));

return $this;
}

public function moveToPosition(int $column, int $row)
public function moveToPosition(int $column, int $row): self
{
$this->output->write(sprintf("\x1b[%d;%dH", $row + 1, $column));

return $this;
}

public function savePosition()
public function savePosition(): self
{
$this->output->write("\x1b7");

return $this;
}

public function restorePosition()
public function restorePosition(): self
{
$this->output->write("\x1b8");

return $this;
}

public function hide()
public function hide(): self
{
$this->output->write("\x1b[?25l");

return $this;
}

public function show()
public function show(): self
{
$this->output->write("\x1b[?25h\x1b[?0c");

return $this;
}

/**
* Clears all the output from the current line.
*/
public function clearLine(bool $fromCurrentPosition = false)
public function clearLine(): self
{
if (true === $fromCurrentPosition) {
$this->output->write("\x1b[K");
} else {
$this->output->write("\x1b[2K");
}
$this->output->write("\x1b[2K");

return $this;
}

/**
* Clears all the output from the current line after the current position.
*/
public function clearLineAfter(): self
fabpot marked this conversation as resolved.
Show resolved Hide resolved
{
$this->output->write("\x1b[K");

return $this;
}

/**
* Clears all the output from the cursors' current position to the end of the screen.
*/
public function clearOutput()
public function clearOutput(): self
{
$this->output->write("\x1b[0J", false);
$this->output->write("\x1b[0J");

return $this;
}

/**
* Clears the entire screen.
*/
public function clearScreen()
public function clearScreen(): self
{
$this->output->write("\x1b[2J", false);
$this->output->write("\x1b[2J");

return $this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function ($match) use ($ret) {
}
}

$cursor->clearLine(true);
$cursor->clearLineAfter();

if ($numMatches > 0 && -1 !== $ofs) {
$cursor->savePosition();
Expand Down