-
Notifications
You must be signed in to change notification settings - Fork 6
Rewrite #1
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
Rewrite #1
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3fe1292
Rewrite
AydinHassan ab40182
Docs for InputCharacter
AydinHassan 6796414
Remove debug code
AydinHassan d72f6ad
Add travis
AydinHassan d78d6c6
Don't exec if we don't need to
AydinHassan 7e47fbd
Remove readCharacter because it doesn't do what we want. Reading 1 by…
AydinHassan 141afd2
Rename TerminalReader to NonCanonicalReader as we now disable canonic…
AydinHassan 0db57e0
Parse original canonical mode
AydinHassan 8db49d2
Remove unused property
AydinHassan 0c4de0b
Add restoreOriginalConfiguration to interface
AydinHassan 90e8de2
Remove debug
AydinHassan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,22 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
|
||
install: | ||
- composer self-update | ||
- composer install | ||
|
||
before_script: | ||
- mkdir -p build/logs | ||
|
||
script: | ||
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml | ||
- composer cs | ||
- composer static | ||
|
||
after_script: | ||
- bash <(curl -s https://codecov.io/bash) | ||
- wget https://scrutinizer-ci.com/ocular.phar | ||
- php ocular.phar code-coverage:upload --format=php-clover ./build/logs/clover.xml |
This file contains hidden or 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 |
---|---|---|
|
@@ -13,14 +13,14 @@ | |
"email": "[email protected]" | ||
} | ||
], | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.0", | ||
"squizlabs/php_codesniffer": "^3.0" | ||
}, | ||
"require": { | ||
"php" : ">=7.0", | ||
"ext-posix": "*", | ||
"myclabs/php-enum": "^1.5" | ||
"php" : ">=7.1", | ||
"ext-posix": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.1", | ||
"squizlabs/php_codesniffer": "^3.2", | ||
"phpstan/phpstan": "^0.9.2" | ||
}, | ||
"autoload" : { | ||
"psr-4" : { | ||
|
@@ -34,6 +34,9 @@ | |
"cs" : [ | ||
"phpcs src --standard=PSR2", | ||
"phpcs test --standard=PSR2" | ||
], | ||
"static" : [ | ||
"phpstan analyse src --level=7" | ||
] | ||
} | ||
} |
This file contains hidden or 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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit colors="true"> | ||
<testsuite name="Terminal Test Suite"> | ||
<directory>./test</directory> | ||
</testsuite> | ||
<filter> | ||
<whitelist addUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains hidden or 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,19 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\Exception; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class NotInteractiveTerminal extends \RuntimeException | ||
{ | ||
public static function inputNotInteractive() : self | ||
{ | ||
return new self('Input stream is not interactive (non TTY)'); | ||
} | ||
|
||
public static function outputNotInteractive() : self | ||
{ | ||
return new self('Output stream is not interactive (non TTY)'); | ||
} | ||
} |
This file contains hidden or 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,40 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\IO; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class BufferedOutput implements OutputStream | ||
{ | ||
private $buffer = ''; | ||
|
||
public function write(string $buffer): void | ||
{ | ||
$this->buffer .= $buffer; | ||
} | ||
|
||
public function fetch(bool $clean = true) : string | ||
{ | ||
$buffer = $this->buffer; | ||
|
||
if ($clean) { | ||
$this->buffer = ''; | ||
} | ||
|
||
return $buffer; | ||
} | ||
|
||
public function __toString() : string | ||
{ | ||
return $this->fetch(); | ||
} | ||
|
||
/** | ||
* Whether the stream is connected to an interactive terminal | ||
*/ | ||
public function isInteractive() : bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains hidden or 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,20 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\IO; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
interface InputStream | ||
{ | ||
/** | ||
* Callback should be called with the number of bytes requested | ||
* when ready. | ||
*/ | ||
public function read(int $numBytes, callable $callback) : void; | ||
|
||
/** | ||
* Whether the stream is connected to an interactive terminal | ||
*/ | ||
public function isInteractive() : bool; | ||
} |
This file contains hidden or 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,19 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\IO; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
interface OutputStream | ||
{ | ||
/** | ||
* Write the buffer to the stream | ||
*/ | ||
public function write(string $buffer) : void; | ||
|
||
/** | ||
* Whether the stream is connected to an interactive terminal | ||
*/ | ||
public function isInteractive() : bool; | ||
} |
This file contains hidden or 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,47 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\IO; | ||
|
||
use function is_resource; | ||
use function get_resource_type; | ||
use function stream_get_meta_data; | ||
use function strpos; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class ResourceInputStream implements InputStream | ||
{ | ||
/** | ||
* @var resource | ||
*/ | ||
private $stream; | ||
|
||
public function __construct($stream = STDIN) | ||
{ | ||
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') { | ||
throw new \InvalidArgumentException('Expected a valid stream'); | ||
} | ||
|
||
$meta = stream_get_meta_data($stream); | ||
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) { | ||
throw new \InvalidArgumentException('Expected a readable stream'); | ||
} | ||
|
||
$this->stream = $stream; | ||
} | ||
|
||
public function read(int $numBytes, callable $callback) : void | ||
{ | ||
$buffer = fread($this->stream, $numBytes); | ||
$callback($buffer); | ||
} | ||
|
||
/** | ||
* Whether the stream is connected to an interactive terminal | ||
*/ | ||
public function isInteractive() : bool | ||
{ | ||
return posix_isatty($this->stream); | ||
} | ||
} |
This file contains hidden or 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,46 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal\IO; | ||
|
||
use function is_resource; | ||
use function get_resource_type; | ||
use function stream_get_meta_data; | ||
use function strpos; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class ResourceOutputStream implements OutputStream | ||
{ | ||
/** | ||
* @var resource | ||
*/ | ||
private $stream; | ||
|
||
public function __construct($stream = STDOUT) | ||
{ | ||
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') { | ||
throw new \InvalidArgumentException('Expected a valid stream'); | ||
} | ||
|
||
$meta = stream_get_meta_data($stream); | ||
if (strpos($meta['mode'], 'r') !== false && strpos($meta['mode'], '+') === false) { | ||
throw new \InvalidArgumentException('Expected a writable stream'); | ||
} | ||
|
||
$this->stream = $stream; | ||
} | ||
|
||
public function write(string $buffer): void | ||
{ | ||
fwrite($this->stream, $buffer); | ||
} | ||
|
||
/** | ||
* Whether the stream is connected to an interactive terminal | ||
*/ | ||
public function isInteractive() : bool | ||
{ | ||
return posix_isatty($this->stream); | ||
} | ||
} |
This file contains hidden or 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,126 @@ | ||
<?php | ||
|
||
namespace PhpSchool\Terminal; | ||
|
||
use function in_array; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class InputCharacter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this represents a single character or control character read from the input source. It contains a bunch of helper methods for dealing with control sequences and validation of them. |
||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $data; | ||
|
||
public const UP = 'UP'; | ||
public const DOWN = 'DOWN'; | ||
public const RIGHT = 'RIGHT'; | ||
public const LEFT = 'LEFT'; | ||
public const CTRLA = 'CTRLA'; | ||
public const CTRLB = 'CTRLB'; | ||
public const CTRLE = 'CTRLE'; | ||
public const CTRLF = 'CTRLF'; | ||
public const BACKSPACE = 'BACKSPACE'; | ||
public const CTRLW = 'CTRLW'; | ||
public const ENTER = 'ENTER'; | ||
public const TAB = 'TAB'; | ||
|
||
private static $controls = [ | ||
"\033[A" => self::UP, | ||
"\033[B" => self::DOWN, | ||
"\033[C" => self::RIGHT, | ||
"\033[D" => self::LEFT, | ||
"\001" => self::CTRLA, | ||
"\002" => self::CTRLB, | ||
"\005" => self::CTRLE, | ||
"\006" => self::CTRLF, | ||
"\010" => self::BACKSPACE, | ||
"\177" => self::BACKSPACE, | ||
"\027" => self::CTRLW, | ||
"\n" => self::ENTER, | ||
"\t" => self::TAB, | ||
]; | ||
|
||
public function __construct(string $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Is this character a control sequence? | ||
*/ | ||
public function isControl() : bool | ||
{ | ||
return isset(static::$controls[$this->data]); | ||
} | ||
|
||
/** | ||
* Is this character a normal character? | ||
*/ | ||
public function isNotControl() : bool | ||
{ | ||
return ! $this->isControl(); | ||
} | ||
|
||
/** | ||
* Get the raw character or control sequence | ||
*/ | ||
public function get() : string | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Get the actual control name that this sequence represents. | ||
* One of the class constants. Eg. self::UP. | ||
* | ||
* Throws an exception if the character is not actually a control sequence | ||
*/ | ||
public function getControl() : string | ||
{ | ||
if (!isset(static::$controls[$this->data])) { | ||
throw new \RuntimeException(sprintf('Character "%s" is not a control', $this->data)); | ||
} | ||
|
||
return static::$controls[$this->data]; | ||
} | ||
|
||
/** | ||
* Get the raw character or control sequence | ||
*/ | ||
public function __toString() : string | ||
{ | ||
return $this->get(); | ||
} | ||
|
||
/** | ||
* Does the given control name exist? eg self::UP. | ||
*/ | ||
public static function controlExists(string $controlName) : bool | ||
{ | ||
return in_array($controlName, static::$controls, true); | ||
} | ||
|
||
/** | ||
* Get all of the available control names | ||
*/ | ||
public static function getControls() : array | ||
{ | ||
return array_values(array_unique(static::$controls)); | ||
} | ||
|
||
/** | ||
* Create a instance from a given control name. Throws an exception if the | ||
* control name does not exist. | ||
*/ | ||
public static function fromControlName(string $controlName) : self | ||
{ | ||
if (!static::controlExists($controlName)) { | ||
throw new \InvalidArgumentException(sprintf('Control "%s" does not exist', $controlName)); | ||
} | ||
|
||
return new static(array_search($controlName, static::$controls, true)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this feels wierd, is it needed? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really - the EA inspections plugin for PHP storm recommends it for optimisation - I can remove if you want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind it being there, just didn't understand why it was