Skip to content

Commit e6cf8c6

Browse files
authored
Merge pull request #60 from clue-labs/write
Consistently return boolean success from write()
2 parents a969286 + d73bf70 commit e6cf8c6

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/Readline.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,21 @@ public function setAutocomplete($autocomplete)
427427
*/
428428
public function redraw()
429429
{
430-
// Erase characters from cursor to end of line
431-
$output = "\r\033[K" . $this->prompt;
430+
// Erase characters from cursor to end of line and then redraw actual input
431+
$this->output->write("\r\033[K" . $this->getDrawString());
432+
433+
return $this;
434+
}
435+
436+
/**
437+
* Returns the string that is used to draw the input prompt
438+
*
439+
* @return string
440+
* @internal
441+
*/
442+
public function getDrawString()
443+
{
444+
$output = $this->prompt;
432445
if ($this->echo !== false) {
433446
if ($this->echo === true) {
434447
$buffer = $this->linebuffer;
@@ -439,9 +452,8 @@ public function redraw()
439452
// write output, then move back $reverse chars (by sending backspace)
440453
$output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell());
441454
}
442-
$this->output->write($output);
443455

444-
return $this;
456+
return $output;
445457
}
446458

447459
/**

src/Stdio.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
9191

9292
public function write($data)
9393
{
94-
if ($this->ending || (string)$data === '') {
95-
return;
94+
// return false if already ended, return true if writing empty string
95+
if ($this->ending || $data === '') {
96+
return !$this->ending;
9697
}
9798

9899
$out = $data;
@@ -147,8 +148,7 @@ public function write($data)
147148

148149
if ($restoreReadline) {
149150
// write output and restore original readline prompt and line buffer
150-
$this->output->write($out);
151-
$this->readline->redraw();
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();
@@ -158,7 +158,7 @@ public function write($data)
158158
}
159159

160160
// write to actual output stream
161-
$this->output->write($out);
161+
return $this->output->write($out);
162162
}
163163
}
164164

tests/StdioTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testWriteEmptyStringWillNotWriteToOutput()
5050

5151
$output->expects($this->never())->method('write');
5252

53-
$stdio->write('');
53+
$this->assertTrue($stdio->write(''));
5454
}
5555

5656
public function testWriteWillClearReadlineWriteOutputAndRestoreReadline()
@@ -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()
@@ -423,7 +423,7 @@ public function testWriteAfterEndWillNotWriteToOutput()
423423

424424
$output->expects($this->never())->method('write');
425425

426-
$stdio->write('test');
426+
$this->assertFalse($stdio->write('test'));
427427
}
428428

429429
public function testEndTwiceWillCloseInputAndEndOutputOnce()

0 commit comments

Comments
 (0)