Skip to content

Commit

Permalink
feature #36433 [Console] cursor tweaks (fabpot)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Console] cursor tweaks

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Refs #27444
| License       | MIT
| Doc PR        | n/a

While playing with the new cursor class, I realized that I would like to make some changes. Mainly having a fluent interface: `$cursor->moveUp(10)->clearOutput()` for instance.

I have also separater the `clearLine()` method in two method to avoid the bool arg.

/cc @pierredup

Commits
-------

5a7b314 Make the Cursor class final
b0e7eb6 Make Cursor fluent
55fef91 Split a method
d00b5b5 Remove default value
  • Loading branch information
fabpot committed Apr 12, 2020
2 parents 6eaafed + 5a7b314 commit cea5ebc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
75 changes: 53 additions & 22 deletions src/Symfony/Component/Console/Cursor.php
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
{
$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
{
$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
Expand Up @@ -353,7 +353,7 @@ function ($match) use ($ret) {
}
}

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

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

0 comments on commit cea5ebc

Please sign in to comment.