Skip to content

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
merged 11 commits into from
Apr 22, 2018
Merged

Rewrite #1

merged 11 commits into from
Apr 22, 2018

Conversation

AydinHassan
Copy link
Member

@AydinHassan AydinHassan commented Apr 18, 2018

@mikeymike I will annotate this PR in a few hours so maybe we can go through it together at some point after that.

@AydinHassan AydinHassan requested a review from mikeymike April 18, 2018 16:41
/**
* @author Aydin Hassan <[email protected]>
*/
class InputCharacter
Copy link
Member Author

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
Copy link
Member Author

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?

Copy link
Member

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;
Copy link
Member Author

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
Copy link
Member Author

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.

Copy link
Member

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)
Copy link
Member Author

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 TerminalReader
Copy link
Member Author

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
Copy link
Member Author

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

@php-school php-school deleted a comment from codecov-io Apr 18, 2018
…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-io
Copy link

codecov-io commented Apr 18, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@12a0a3d). Click here to learn what that means.
The diff coverage is 80.92%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master       #1   +/-   ##
=========================================
  Coverage          ?   80.92%           
  Complexity        ?       80           
=========================================
  Files             ?        7           
  Lines             ?      152           
  Branches          ?        0           
=========================================
  Hits              ?      123           
  Misses            ?       29           
  Partials          ?        0
Impacted Files Coverage Δ Complexity Δ
src/Exception/NotInteractiveTerminal.php 100% <100%> (ø) 2 <2> (?)
src/InputCharacter.php 100% <100%> (ø) 11 <11> (?)
src/UnixTerminal.php 69.86% <69.86%> (ø) 39 <39> (?)
src/IO/BufferedOutput.php 81.81% <81.81%> (ø) 5 <5> (?)
src/IO/ResourceOutputStream.php 81.81% <81.81%> (ø) 7 <7> (?)
src/IO/ResourceInputStream.php 83.33% <83.33%> (ø) 7 <7> (?)
src/NonCanonicalReader.php 94.73% <94.73%> (ø) 9 <9> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 12a0a3d...90e8de2. Read the comment docs.

src/Terminal.php Outdated

public function setCanonicalMode(bool $useCanonicalMode = true)
Copy link
Member Author

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
Copy link
Member Author

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?

Copy link
Member

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;
Copy link
Member

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? 🤔

Copy link
Member Author

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?

Copy link
Member

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense

@mikeymike mikeymike merged commit c3a87b9 into master Apr 22, 2018
@mikeymike mikeymike deleted the rewrite branch April 22, 2018 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants