-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Hugga\Input; | ||
|
||
class TTyObserver | ||
{ | ||
protected $stdin; | ||
|
||
protected $charHandler = []; | ||
|
||
protected $handler = []; | ||
|
||
protected $stop = true; | ||
|
||
public static function isCompatible($stdin) | ||
{ | ||
return is_executable('/bin/stty') && function_exists('posix_isatty') && posix_isatty($stdin); | ||
} | ||
|
||
public function __construct($stdin) | ||
{ | ||
$this->stdin = $stdin; | ||
} | ||
|
||
public function start() | ||
{ | ||
$this->stop = false; | ||
|
||
// change tty settings | ||
$sttySettings = preg_replace('#.*; ?#s', '', $this->ttySettings('--all')); | ||
$this->ttySettings('cbreak -echo'); | ||
|
||
while (!$this->stop) { | ||
$c = ''; | ||
do { | ||
$c .= fgetc($this->stdin); | ||
$input = [$this->stdin]; | ||
} while (stream_select($input, $output, $error, 0) && in_array($this->stdin, $input)); | ||
|
||
$this->handle($c); | ||
} | ||
|
||
// reset tty settings | ||
$this->ttySettings($sttySettings); | ||
|
||
return; | ||
} | ||
|
||
public function stop() | ||
{ | ||
$this->stop = true; | ||
} | ||
|
||
protected function handle(string $char) | ||
{ | ||
$event = (object)[ | ||
'type' => 'keyUp', | ||
'char' => $char, | ||
'stopPropagation' => false, | ||
]; | ||
|
||
$handlers = array_reverse($this->handler); | ||
if (isset($this->charHandler[$char])) { | ||
array_unshift($handlers, ...array_reverse($this->charHandler[$char])); | ||
} | ||
foreach ($handlers as $handler) { | ||
$handler($event); | ||
if ($event->stopPropagation) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
public function on(string $char, callable $callback) | ||
{ | ||
if (!isset($this->charHandler[$char])) { | ||
$this->charHandler[$char] = []; | ||
} | ||
|
||
$this->charHandler[$char][] = $callback; | ||
return $this; | ||
} | ||
|
||
public function addHandler(callable $callback) | ||
{ | ||
$this->handler[] = $callback; | ||
return $this; | ||
} | ||
|
||
public function off(string $char, callable $callback) | ||
{ | ||
if (!isset($this->charHandler[$char])) { | ||
return $this; | ||
} | ||
|
||
if ($pos = array_search($callback, $this->charHandler[$char])) { | ||
array_splice($this->charHandler[$char], $pos, 1); | ||
} | ||
return $this; | ||
} | ||
|
||
public function removeHandler(callable $callback) | ||
{ | ||
if ($pos = array_search($callback, $this->charHandler[$char])) { | ||
array_splice($this->charHandler[$char], $pos, 1); | ||
} | ||
return $this; | ||
} | ||
|
||
protected function ttySettings($options) | ||
{ | ||
exec($cmd = "/bin/stty $options", $output, $returnValue); | ||
if ($returnValue) { | ||
throw new \Exception('Failed to change tty settings'); | ||
} | ||
return implode(' ', $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,6 @@ public function deleteLine() | |
} else { | ||
$this->delete(strlen($tail)); | ||
} | ||
} while($i > 0); | ||
} while ($i > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters