-
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
Conversation
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class InputCharacter |
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.
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.
*/ | ||
class Terminal implements TerminalInterface | ||
interface Terminal |
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.
So I've rename TerminalInterface
to Terminal
and Terminal
to UnixTerminal
. I know we only have 1 terminal but we might add a windows one in the future if the terminal gets a bit better. What do you think?
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.
Seems logical, Windows afaik are working more towards unix style / vt100 compatable terminal with the Linux subsystem stuff anyway but I'm not sure if it replaces the standard cmd shit.
* | ||
* @return bool | ||
*/ | ||
public function isInteractive() : bool; |
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 replaces isATTY
- I think it's a bit nicer and more explanatory
|
||
public function getDetails(): string |
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've dropped this completely. Basically this got the name of the terminal. But it was only used when the terminal was not a TTY to throw an exception. But if it's not a TTY - then it doesn't really have a name because it's either a pipe or a redirect. So I don't think we need this.
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.
yeah makes sense
src/Terminal.php
Outdated
|
||
public function setCanonicalMode(bool $useCanonicalMode = true) |
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've swapped this for disableCanonicalMode
& enableCanonicalMode
src/TerminalReader.php
Outdated
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class TerminalReader |
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.
So this new class takes a Terminal
object and allows to read input and maps to InputCharacters
- it also allows to map other characters to control code. So cli-menu
code will look like this:
$reader = new TerminalReader($this->terminal);
$reader->addControlMappings([
'^P' => InputCharacter::UP,
'k' => InputCharacter::UP,
'^K' => InputCharacter::DOWN,
'j' => InputCharacter::DOWN,
"\r" => InputCharacter::ENTER,
' ' => InputCharacter::ENTER,
]);
while ($this->isOpen() && $char = $reader->readCharacter()) {
if ($char->isNotControl()) {
continue;
}
switch ($char->getControl()) {
case InputCharacter::UP:
case InputCharacter::DOWN:
$this->moveSelection($char->getControl());
$this->draw();
break;
case InputCharacter::ENTER:
$this->executeCurrentItem();
break;
}
}
What do you think?
* @author Michael Woodward <[email protected]> | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class UnixTerminal implements Terminal |
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 should hopefully be self explanatory
…te in non-canonical mode will read only half a control sequence.
…al mode and empty the buffer altogether. It should either be a control sequence or a character.
Codecov Report
@@ Coverage Diff @@
## master #1 +/- ##
=========================================
Coverage ? 80.92%
Complexity ? 80
=========================================
Files ? 7
Lines ? 152
Branches ? 0
=========================================
Hits ? 123
Misses ? 29
Partials ? 0
Continue to review full report at Codecov.
|
src/Terminal.php
Outdated
|
||
public function setCanonicalMode(bool $useCanonicalMode = true) |
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've swapped this for disableCanonicalMode
& enableCanonicalMode
* | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class NonCanonicalReader |
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.
So this new class takes a Terminal
object and allows to read input and maps to InputCharacters
- it also allows to map other characters to control code. So cli-menu
code will look like this:
$reader = new NonCanonicalReader($this->terminal);
$reader->addControlMappings([
'^P' => InputCharacter::UP,
'k' => InputCharacter::UP,
'^K' => InputCharacter::DOWN,
'j' => InputCharacter::DOWN,
"\r" => InputCharacter::ENTER,
' ' => InputCharacter::ENTER,
]);
while ($this->isOpen() && $char = $reader->readCharacter()) {
if ($char->isNotControl()) {
continue;
}
switch ($char->getControl()) {
case InputCharacter::UP:
case InputCharacter::DOWN:
$this->moveSelection($char->getControl());
$this->draw();
break;
case InputCharacter::ENTER:
$this->executeCurrentItem();
break;
}
}
It enables and disables canonical mode on the fly and empties the buffer on each read (well reads 4 bytes but there should never be more in there)
What do you think?
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.
👌
|
||
namespace PhpSchool\Terminal; | ||
|
||
use function in_array; |
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
*/ | ||
class Terminal implements TerminalInterface | ||
interface Terminal |
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.
Seems logical, Windows afaik are working more towards unix style / vt100 compatable terminal with the Linux subsystem stuff anyway but I'm not sure if it replaces the standard cmd shit.
|
||
public function getDetails(): string |
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.
yeah makes sense
@mikeymike I will annotate this PR in a few hours so maybe we can go through it together at some point after that.