Skip to content

Commit 6ff2395

Browse files
committed
Use cursor class in QuestionHelper
1 parent 30af066 commit 6ff2395

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/Symfony/Component/Console/Cursor.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public function moveToPosition(int $column, int $row)
5757

5858
public function savePosition()
5959
{
60-
$this->output->write("\x1b[s");
60+
$this->output->write("\x1b7");
6161
}
6262

6363
public function restorePosition()
6464
{
65-
$this->output->write("\x1b[u");
65+
$this->output->write("\x1b8");
6666
}
6767

6868
public function hide()
@@ -76,11 +76,15 @@ public function show()
7676
}
7777

7878
/**
79-
* Clears all the output from the of the current line.
79+
* Clears all the output from the current line.
8080
*/
81-
public function clearLine()
81+
public function clearLine(bool $fromCurrentPosition = false)
8282
{
83-
$this->output->write("\x1b[2K");
83+
if (true === $fromCurrentPosition) {
84+
$this->output->write("\x1b[K");
85+
} else {
86+
$this->output->write("\x1b[2K");
87+
}
8488
}
8589

8690
/**

src/Symfony/Component/Console/Helper/QuestionHelper.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14+
use Symfony\Component\Console\Cursor;
1415
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\Console\Formatter\OutputFormatter;
1617
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
@@ -203,6 +204,8 @@ protected function writeError(OutputInterface $output, \Exception $error)
203204
*/
204205
private function autocomplete(OutputInterface $output, Question $question, $inputStream, callable $autocomplete): string
205206
{
207+
$cursor = new Cursor($output);
208+
206209
$fullChoice = '';
207210
$ret = '';
208211

@@ -232,7 +235,7 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
232235
--$i;
233236
$fullChoice = self::substr($fullChoice, 0, $i);
234237
// Move cursor backwards
235-
$output->write("\033[1D");
238+
$cursor->moveLeft();
236239
}
237240

238241
if (0 === $i) {
@@ -319,16 +322,16 @@ function ($match) use ($ret) {
319322
}
320323

321324
// Erase characters from cursor to end of line
322-
$output->write("\033[K");
325+
$cursor->clearLine(true);
323326

324327
if ($numMatches > 0 && -1 !== $ofs) {
325328
// Save cursor position
326-
$output->write("\0337");
329+
$cursor->savePosition();
327330
// Write highlighted text, complete the partially entered response
328331
$charactersEntered = \strlen(trim($this->mostRecentlyEnteredValue($fullChoice)));
329332
$output->write('<hl>'.OutputFormatter::escapeTrailingBackslash(substr($matches[$ofs], $charactersEntered)).'</hl>');
330333
// Restore cursor position
331-
$output->write("\0338");
334+
$cursor->restorePosition();
332335
}
333336
}
334337

src/Symfony/Component/Console/Tests/CursorTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class CursorTest extends TestCase
1919
{
2020
protected $stream;
2121

22-
protected function setUp()
22+
protected function setUp(): void
2323
{
2424
$this->stream = fopen('php://memory', 'r+');
2525
}
2626

27-
protected function tearDown()
27+
protected function tearDown(): void
2828
{
2929
fclose($this->stream);
3030
$this->stream = null;
@@ -135,7 +135,7 @@ public function testSavePosition()
135135

136136
$cursor->savePosition();
137137

138-
$this->assertEquals("\x1b[s", $this->getOutputContent($output));
138+
$this->assertEquals("\x1b7", $this->getOutputContent($output));
139139
}
140140

141141
public function testHide()
@@ -162,7 +162,7 @@ public function testRestorePosition()
162162

163163
$cursor->restorePosition();
164164

165-
$this->assertEquals("\x1b[u", $this->getOutputContent($output));
165+
$this->assertEquals("\x1b8", $this->getOutputContent($output));
166166
}
167167

168168
public function testClearOutput()

0 commit comments

Comments
 (0)