Skip to content

Commit 419f09c

Browse files
committed
Consistently return boolean success from write()
1 parent f574375 commit 419f09c

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/Readline.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ public function setAutocomplete($autocomplete)
426426
* @internal
427427
*/
428428
public function redraw()
429+
{
430+
$this->output->write($this->__toString());
431+
432+
return $this;
433+
}
434+
435+
/** @internal */
436+
public function __toString()
429437
{
430438
// Erase characters from cursor to end of line
431439
$output = "\r\033[K" . $this->prompt;
@@ -439,9 +447,8 @@ public function redraw()
439447
// write output, then move back $reverse chars (by sending backspace)
440448
$output .= $buffer . str_repeat("\x08", $this->strwidth($buffer) - $this->getCursorCell());
441449
}
442-
$this->output->write($output);
443450

444-
return $this;
451+
return $output;
445452
}
446453

447454
/**

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->__toString());
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: 2 additions & 2 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()
@@ -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)