Skip to content

Commit d73bf70

Browse files
committed
Avoid unneeded control codes to overwrite input prompt
1 parent 419f09c commit d73bf70

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/Readline.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,21 @@ public function setAutocomplete($autocomplete)
427427
*/
428428
public function redraw()
429429
{
430-
$this->output->write($this->__toString());
430+
// Erase characters from cursor to end of line and then redraw actual input
431+
$this->output->write("\r\033[K" . $this->getDrawString());
431432

432433
return $this;
433434
}
434435

435-
/** @internal */
436-
public function __toString()
436+
/**
437+
* Returns the string that is used to draw the input prompt
438+
*
439+
* @return string
440+
* @internal
441+
*/
442+
public function getDrawString()
437443
{
438-
// Erase characters from cursor to end of line
439-
$output = "\r\033[K" . $this->prompt;
444+
$output = $this->prompt;
440445
if ($this->echo !== false) {
441446
if ($this->echo === true) {
442447
$buffer = $this->linebuffer;

src/Stdio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function write($data)
148148

149149
if ($restoreReadline) {
150150
// write output and restore original readline prompt and line buffer
151-
return $this->output->write($out . $this->readline->__toString());
151+
return $this->output->write($out . $this->readline->getDrawString());
152152
} else {
153153
// restore original cursor position in readline prompt
154154
$pos = $this->width($this->readline->getPrompt()) + $this->readline->getCursorCell();

tests/StdioTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testWriteWillClearReadlineWriteOutputAndRestoreReadline()
7272

7373
$stdio->write('test');
7474

75-
$this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer);
75+
$this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer);
7676
}
7777

7878
public function testWriteAgainWillMoveToPreviousLineWriteOutputAndRestoreReadlinePosition()
@@ -144,7 +144,7 @@ public function testWriteAgainWithNewlinesWillClearReadlineMoveToPreviousLineWri
144144

145145
$stdio->write("ond" . "\n" . "third");
146146

147-
$this->assertEquals("\r\033[K" . "\033[A" . "\r\033[3C" . "ond\nthird\n" . "\r\033[K" . "> input", $buffer);
147+
$this->assertEquals("\r\033[K" . "\033[A" . "\r\033[3C" . "ond\nthird\n" . "> input", $buffer);
148148
}
149149

150150
public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestoreReadline()
@@ -170,7 +170,7 @@ public function testWriteAfterReadlineInputWillClearReadlineWriteOutputAndRestor
170170

171171
$stdio->writeln('test');
172172

173-
$this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer);
173+
$this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer);
174174
}
175175

176176
public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRestoreReadline()
@@ -194,7 +194,7 @@ public function testOverwriteWillClearReadlineMoveToPreviousLineWriteOutputAndRe
194194

195195
$stdio->overwrite('overwrite');
196196

197-
$this->assertEquals("\r\033[K" . "\033[A" . "\r\033[K" . "overwrite\n" . "\r\033[K" . "> input", $buffer);
197+
$this->assertEquals("\r\033[K" . "\033[A" . "\r\033[K" . "overwrite\n" . "> input", $buffer);
198198
}
199199

200200
public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndRestoreReadline()
@@ -218,7 +218,7 @@ public function testOverwriteAfterNewlineWillClearReadlineAndWriteOutputAndResto
218218

219219
$stdio->overwrite('overwrite');
220220

221-
$this->assertEquals("\r\033[K" . "overwrite\n" . "\r\033[K" . "> input", $buffer);
221+
$this->assertEquals("\r\033[K" . "overwrite\n" . "> input", $buffer);
222222
}
223223

224224
public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline()
@@ -240,7 +240,7 @@ public function testWriteLineWillClearReadlineWriteOutputAndRestoreReadline()
240240

241241
$stdio->writeln('test');
242242

243-
$this->assertEquals("\r\033[K" . "test\n" . "\r\033[K" . "> input", $buffer);
243+
$this->assertEquals("\r\033[K" . "test\n" . "> input", $buffer);
244244
}
245245

246246
public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline()
@@ -263,7 +263,7 @@ public function testWriteTwoLinesWillClearReadlineWriteOutputAndRestoreReadline(
263263
$stdio->writeln('hello');
264264
$stdio->writeln('world');
265265

266-
$this->assertEquals("\r\033[K" . "hello\n" . "\r\033[K" . "> input" . "\r\033[K" . "world\n" . "\r\033[K" . "> input", $buffer);
266+
$this->assertEquals("\r\033[K" . "hello\n" . "> input" . "\r\033[K" . "world\n" . "> input", $buffer);
267267
}
268268

269269
public function testPauseWillBeForwardedToInput()

0 commit comments

Comments
 (0)