Skip to content

Commit

Permalink
Merge pull request #58 from clue-labs/restore
Browse files Browse the repository at this point in the history
Restore blocking mode before closing and restore TTY mode on unclean shutdown
  • Loading branch information
clue authored Nov 1, 2017
2 parents 32a5b99 + 4c84374 commit bd60c1b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function __construct(LoopInterface $loop)
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
}

// register shutdown function to restore TTY mode in case of unclean shutdown (uncaught exception)
// this will not trigger on SIGKILL etc., but the terminal should take care of this
register_shutdown_function(array($this, 'close'));
}

public function close()
Expand All @@ -52,6 +56,11 @@ private function restore()
shell_exec(sprintf('stty %s', $this->oldMode));
$this->oldMode = null;
}

// restore blocking mode so following programs behave normally
if (defined('STDIN') && is_resource(STDIN)) {
stream_set_blocking(STDIN, true);
}
}

/**
Expand Down

0 comments on commit bd60c1b

Please sign in to comment.