From d00b5b5e089d301f5cd3ad264611f07c8b805e00 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 11:14:14 +0200 Subject: [PATCH 1/4] Remove default value --- src/Symfony/Component/Console/Cursor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Cursor.php b/src/Symfony/Component/Console/Cursor.php index 03fd5e0672b7..3be356ab1960 100644 --- a/src/Symfony/Component/Console/Cursor.php +++ b/src/Symfony/Component/Console/Cursor.php @@ -95,7 +95,7 @@ public function clearLine(bool $fromCurrentPosition = false) */ public function clearOutput() { - $this->output->write("\x1b[0J", false); + $this->output->write("\x1b[0J"); } /** @@ -103,7 +103,7 @@ public function clearOutput() */ public function clearScreen() { - $this->output->write("\x1b[2J", false); + $this->output->write("\x1b[2J"); } /** From 55fef914cc6ef40a50a1481e4932845ff988e447 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 11:15:20 +0200 Subject: [PATCH 2/4] Split a method --- src/Symfony/Component/Console/Cursor.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Cursor.php b/src/Symfony/Component/Console/Cursor.php index 3be356ab1960..5cbfebe148b9 100644 --- a/src/Symfony/Component/Console/Cursor.php +++ b/src/Symfony/Component/Console/Cursor.php @@ -81,13 +81,17 @@ public function show() /** * Clears all the output from the current line. */ - public function clearLine(bool $fromCurrentPosition = false) + public function clearLine() { - if (true === $fromCurrentPosition) { - $this->output->write("\x1b[K"); - } else { - $this->output->write("\x1b[2K"); - } + $this->output->write("\x1b[2K"); + } + + /** + * Clears all the output from the current line after the current position. + */ + public function clearLineAfter() + { + $this->output->write("\x1b[K"); } /** From b0e7eb66e00cb613a8ae74de54a04bdcf3d56265 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 11:16:50 +0200 Subject: [PATCH 3/4] Make Cursor fluent --- src/Symfony/Component/Console/Cursor.php | 57 ++++++++++++++----- .../Console/Helper/QuestionHelper.php | 2 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Console/Cursor.php b/src/Symfony/Component/Console/Cursor.php index 5cbfebe148b9..8d3d868a0618 100644 --- a/src/Symfony/Component/Console/Cursor.php +++ b/src/Symfony/Component/Console/Cursor.php @@ -19,7 +19,6 @@ class Cursor { private $output; - private $input; public function __construct(OutputInterface $output, $input = STDIN) @@ -28,86 +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() + public function clearLine(): self { $this->output->write("\x1b[2K"); + + return $this; } /** * Clears all the output from the current line after the current position. */ - public function clearLineAfter() + 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"); + + return $this; } /** * Clears the entire screen. */ - public function clearScreen() + public function clearScreen(): self { $this->output->write("\x1b[2J"); + + return $this; } /** diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index 797076a8bfaf..450bfab2587e 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -353,7 +353,7 @@ function ($match) use ($ret) { } } - $cursor->clearLine(true); + $cursor->clearLineAfter(); if ($numMatches > 0 && -1 !== $ofs) { $cursor->savePosition(); From 5a7b3146f547b755aa09ae93fc1e5958929677fd Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 14:20:52 +0200 Subject: [PATCH 4/4] Make the Cursor class final --- src/Symfony/Component/Console/Cursor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Cursor.php b/src/Symfony/Component/Console/Cursor.php index 8d3d868a0618..9f8be9649c52 100644 --- a/src/Symfony/Component/Console/Cursor.php +++ b/src/Symfony/Component/Console/Cursor.php @@ -16,7 +16,7 @@ /** * @author Pierre du Plessis */ -class Cursor +final class Cursor { private $output; private $input;