Skip to content

[Console] Handle signals on text input #61852

@valx76

Description

@valx76

Description

Currently, when we are waiting for user input, signals are not triggered.
For example, if we press Ctrl+C, we also have to hit ENTER to process the input and send the signal.
Imo, it would give a more native CLI feeling if signals were handled directly.

This is happening because QuestionHelper is using fgets() and fgetc() in a blocking stream.
Instead, we could use fread() as it is done in the autocomplete feature.

For example, it could look like something like this for a one-line input (inspired by the autocomplete code):

$ret = '';
$isStdin = 'php://stdin' === (stream_get_meta_data($inputStream)['uri'] ?? null);
$r = [$inputStream];
$w = [];

while (!feof($inputStream)) {
    while ($isStdin && 0 === @stream_select($r, $w, $w, 0, 100)) {
        // Give signal handlers a chance to run
        $r = [$inputStream];
    }
    $c = fread($inputStream, 1);

    if ("\n" === $c) {
        break;
    }

    $ret .= $c;
}

What do you think?
Can give a shot for a PR if that's something you'd be willing to integrate :).

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions