8000 Support running on non-TTY and closing STDIN and STDOUT streams by clue · Pull Request #57 · clue/reactphp-stdio · GitHub
[go: up one dir, main page]

Skip to content

Support running on non-TTY and closing STDIN and STDOUT streams #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Do not try to set TTY mode if not a TTY
  • Loading branch information
clue committed Nov 1, 2017
commit c4e17d7a4273e19dd247924cbffbeb2ea25b9ca4
34 changes: 16 additions & 18 deletions src/Stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,37 @@ class Stdin extends Stream
public function __construct(LoopInterface $loop)
{
parent::__construct(STDIN, $loop);
}

public function resume()
{
if ($this->oldMode === null) {
if ($this->isTty()) {
$this->oldMode = shell_exec('stty -g');

// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');

parent::resume();
}
}

public function pause()
public function close()
{
if ($this->oldMode !== null) {
// Reset stty so it behaves normally again
shell_exec(sprintf('stty %s', $this->oldMode));
$this->restore();
parent::close();
}

$this->oldMode = null;
parent::pause();
}
public function __destruct()
{
$this->restore();
}

public function close()
private function restore()
{
$this->pause();
parent::close();
if ($this->oldMode !== null && $this->isTty()) {
// Reset stty so it behaves normally again
shell_exec(sprintf('stty %s', $this->oldMode));
$this->oldMode = null;
}
}

public function __destruct()
private function isTty()
{
$this->pause();
return (is_resource(STDIN) && function_exists('posix_isatty') && posix_isatty(STDIN));
}
}
39 changes: 39 additions & 0 deletions tests/FunctionalExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

class FunctionalExampleTest extends TestCase
{
public function testPeriodicExampleWithPipedInputEndsBecauseInputEnds()
{
$output = $this->execExample('echo hello | php 01-periodic.php');

$this->assertContains('you just said: hello\n', $output);
}

public function testPeriodicExampleWithNullInputQuitsImmediately()
{
$output = $this->execExample('php 01-periodic.php < /dev/null');

$this->assertNotContains('you just said:', $output);
}

public function testPeriodicExampleWithNoInputQuitsImmediately()
{
$output = $this->execExample('true | php 01-periodic.php');

$this->assertNotContains('you just said:', $output);
}

public function testPeriodicExampleWithSleepNoInputQuitsOnEnd()
{
$output = $this->execExample('sleep 0.1 | php 01-periodic.php');

$this->assertNotContains('you just said:', $output);
}

private function execExample($command)
{
chdir(__DIR__ . '/../examples/');

return shell_exec($command);
}
}
0