Coder Social home page Coder Social logo

Comments (15)

freekmurze avatar freekmurze commented on September 15, 2024 1

I'm not a big user of ReactPHP so I don't know what a good API for most users might be, this is just my two little cents with only my specific problem in mind.

For my package it would have been nice that a $stdio->onKeypress($callable) function would have been present.

from reactphp-stdio.

clue avatar clue commented on September 15, 2024 1

Thanks for all your input so far!

Note that the above code will have to be deprecated via #62 for the upcoming v1.2.0 release. This is required in preparation for the upcoming v2.0.0 release (#50).

A proper way to register custom functions to key events has been worked out already and will be added again with the v2.1.0 release that will be released shortly thereafter.

This means that you can keep relying on the above behavior with the old version for now and I will update this ticket again for the v2.1.0 release ๐Ÿ‘

from reactphp-stdio.

freekmurze avatar freekmurze commented on September 15, 2024

Tried a whole lot of variations to get this to work but no luck.

Latest attempt:

        $this->io->getInput()->on($eventName, function ($line) use ($callable) {
            $callable($line);

            $this->io->getReadline()->setInput('')->clear();
        });

Hope to find a solution soon, this is the last problem I need to solve before tagging a new release of my package.

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

I was peeking around (because of your tweet) and came up w/ pretty much the same solution as you. But that one works for me. Does it do something it shouldn't?

from reactphp-stdio.

freekmurze avatar freekmurze commented on September 15, 2024

The thing is with that code in my previous comment, actions get executed multiple times. So pressing "a" runs the tests, pressing "a" again runs the tests twice, and so on. Can't find a good way to reset what has been typed before.

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

Yeah, I noticed this but I can't figure out why. If I replace the callable with a simple echo I don't get repeated echoes or characters. Which is strange.

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

Got it: $this->io->getInput()->removeAllListeners();

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

That appears to work for at least the "all tests" case.

from reactphp-stdio.

freekmurze avatar freekmurze commented on September 15, 2024

Mmm, I don't understand 100% how you fixed this.

Tried:

        $this->io->getInput()->on($eventName, function ($line) use ($callable) {
            $callable(trim($line));

            $this->io->getInput()->removeAllListeners();
        });

Doesn't seem to be working...

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

Oh sorry, in your Terminal.php's removeAllListeners method:

public function removeAllListeners()
{
    $this->io->removeAllListeners();
    $this->io->getInput()->removeAllListeners(); // Added this line

    return $this;
}

This appears to break input on the filter screens though. Looking into that now.

from reactphp-stdio.

freekmurze avatar freekmurze commented on September 15, 2024

Yeah, just figured it out and bumped on the breaking filter screens. Thanks for figuring this out together with me :-)

from reactphp-stdio.

thecrypticace avatar thecrypticace commented on September 15, 2024

๐Ÿ‘

from reactphp-stdio.

freekmurze avatar freekmurze commented on September 15, 2024

Got it!

https://github.com/spatie/phpunit-watcher/blob/f954fd2/src/Terminal.php#L25-L38

I'm thinking removeAllListeners removed some internal listeners as well. Solved it by using once.

Seems hacky, but it works.

from reactphp-stdio.

clue avatar clue commented on September 15, 2024

I'm glad you've got this sorted out already! Let's recap some of the nifty details here.

The following code will invoke the callback whenever data arrives on STDIN:

$stdio->getInput()->on('data', function ($data) {
    var_dump($data);
});

If you only want to execute this callback once for when the next input arrives, you should indeed use once() instead:

$stdio->getInput()->once('data', function ($data) {
    var_dump($data);
});

Both should work for the use case you've mentioned above, but note they actually work on raw data received from STDIN, which may expose more "data" than you may be aware of. This event will be emitted whenever new data on the STDIN stream is ready, which may not only include "user key presses".

In particular, special keys such as cursors or Fn will actually emit binary terminal codes that consist of multiple bytes (which may in turn contain ASCII bytes). Similarly, pressing non-ASCII keys such as รค will emit a single event with multiple bytes. Additionally, copy-pasting things into the terminal will emit a single event with multiple bytes.

To make things worse, keep in mind that the loop will process data as soon as it (is aware that it) arrives. This means that if you either block the loop or copy-paste big inputs, you may actually receive data in chunks which may contain multiple bytes. Additionally, these chunks can potentially be split at byte ranges so that a single UTF-8 characters can be broken into multiple chunks with incomplete UTF-8 sequences.

All of these are probably not an issue for your particular use case, so you should be able to consume this stream just fine. But these combined are the reason why the Readline class does all the heavy lifting internally and only exposes the data when the user presses ENTER.

I'm not opposed to exposing things through an easier API, but I'd love to hear some more thoughts on this first! Thanks for sparking this discussion, which I think is a very good step towards this API! ๐Ÿ‘

from reactphp-stdio.

clue avatar clue commented on September 15, 2024

I've just filed #70 to add this for the upcoming release :shipit:

I would love some feedback, so it would be much appreciated if anybody happens to have a chance to check this out before merging.

from reactphp-stdio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.