Skip to content

Text is being cutted when pasting. #165

Closed

Description

@ptykamikaze

Hello! I'm trying to use your cli which is very nice tool but I'm having a problem. When I'm pasting a text, it is being reduced. For example if I paste CSLAD00000334, the cli return CSLAD0000033. The weird thing is that if I press space, the last character appears. Can anyone help me with this issue?

Activity

  1. Lynesth commented on Aug 3, 2018

    @Lynesth
    Collaborator

    Hey there,

    Where exactly are you pasting the text ? Is it in an input ? If so, which one ?
    Are you experiencing the same issue if you try pasting something shorter ?

  2. ptykamikaze commented on Aug 3, 2018

    @ptykamikaze
    Author

    Hello. I'm using this method to generate an input

            $result = $menu->askText()
                        ->setPromptText('Enter the number)
                        ->setValidationFailedText('Invalid number')
                        ->setPlaceholderText('#')
                        ->ask();
    

    Temporally, I have modified this file "NonCanonicalReader.php", line 64, to allow up to 30 characters and for the moment it is working...

    $char = $this->terminal->read(30);

    Thanks for your quick response...

  3. AydinHassan commented on Sep 4, 2018

    @AydinHassan
    Member

    hey @ptykamikaze sorry for the very late reply. This is an interesting problem and I'm not sure what the solution should be. I never considered pasting in to the field when building this. I think the best way to solve this might be to PR a failing test case highlighting the issue and then we can discuss and figure out a solution.

    While your solution does work for your use case, any pasted string over 30 chars would still result in trimming.

  4. tbannister commented on Jan 6, 2023

    @tbannister

    I've run into the same problem with the current 4.3.0 release. It looks like the issue is that NonCanonicalReader consumes input in 4 character chunks. If you paste a line that is not divisible by 4, it waits for further input. For some reason that doesn't happen while typing, though. In fact, typing a regular non-control character (like space or a letter) will cause the remainder letters to appear followed by the regular non-control character. If you type a control character, like enter, after pasting a string that is not divisible by four the control character is ignored and the remainder characters are lost. Pressing enter a second time will then close the input and process the truncated value.

    Examples:

    1. Paste "func" - "func" appears.
    2. Paste "funct" - Only "func" appears.
    3. Paste "functi" - Only "func" appears.
    4. Paste "functio" - Only "func" appears.
    5. Paste "function" - "function" appears.
    6. Paste "funct" - "func" appears - press space - "funct " appears.
    7. Paste "funct" - "func" appears - press enter - "func" remains showing.
    8. Paste "funct" - "func" appears - press enter - "func" remains showing - press enter again - input closes.
  5. tbannister commented on Jan 6, 2023

    @tbannister

    I've experimented a little further with this, and this is interesting, the processing bug only appears on the second or later block of pasted text, according to the length specified in the NonCanonicalReader.php file in the terminal library. If you paste a three character string, it always works, but if you paste 5 or longer and it's not divisible by 4, you get one of the buggy interactions. If you increase the size of the read call in NonCanonicalReader.php the string now has to be divisible by the new value to work.

  6. tbannister commented on Jan 6, 2023

    @tbannister

    I think the issue is with the fread call in ResourceInputStream.php. It looks like fread is blocking on the final call because not enough data is ready to continue. If there are between 1 and 3 leftover characters, it blocks until new input triggers it to run again.

  7. AydinHassan commented on Jan 7, 2023

    @AydinHassan
    Member

    Yeah I am not sure how to solve this, we need to read more than 1 char because from what I remember some control sequences come through as more than one char.

  8. tbannister commented on Jan 9, 2023

    @tbannister

    I've been poking around the stream reading documentation for PHP, and it seems like there are three possible solutions:

    1. Boost the read size to a very large number (8192, for example) so that any legitimately sized input should be read in the first fread.
    2. Use fgets to get input one line at a time for inputs.
    3. Turn off blocking on stdin when data is read, and turn it back on when no more data is left.

    The third option seems simplest, most scalable, and in testing fixes the problem. It requires a small change to ResourceInputStream.php in the terminal library.

  9. AydinHassan commented on Jan 9, 2023

    @AydinHassan
    Member

    @tbannister nice investigation, go for it, hopefully the test suite should highlight any issues :)

  10. tbannister commented on Jan 9, 2023

    @tbannister

    A pull request has been submitted to terminal.

  11. tbannister commented on Jan 11, 2023

    @tbannister

    After using the patched code for a bit, I discovered that I had unintentionally created a side effect where the blocking status of the input stream could be changed. I've submitted an new pull request with a new commit that saves the initial blocking status and restores that status when the stream object is destroyed.

  12. AydinHassan commented on Jan 11, 2023

    @AydinHassan
    Member

    I've merged it 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions