Proposal: Add support for interactive console application on Windows

php.internals

Patrik Pasterčík

2 years ago
Hi internals I would like to propose extending PHP with support functions to enable interactive console work on Windows system. I mean the ability to control a PHP script running from the console using arrow keys, or function keys like "F1", etc. For example, when a list is displayed to the user and the user can select the desired item using the arrow keys and confirm it with the Enter key. An analysis of other solutions, a detailed description of the problems and a proof of concept is in the proposal in the issue here: https://github.com/php/php-src/issues/12227 The short version is that on Linux you can use "fread" function to read these keys (arrows, etc.) if you use an external `stty` command to set the console input processing modes (for example the laravel/prompts package use "stty -icanon -isig -echo"). However, there is no equivalent of the `stty` command for Windows (without the need to use the FFI extension, or the need to call an external program to read pressed keys, see issue for more details), so I propose to extend PHP with support functions for setting the console input mode (ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_VIRTUAL_TERMINAL_INPUT) by "SetConsoleMode" function, see: https://learn.microsoft.com/en-us/windows/console/setconsolemode I would like to know your opinion on this proposal. Or if you have any questions. And then as a next step I would like to create an RFC proposing these functions for Windows support. And possibly I could help with implementation (see my proof of concept in the issue). Alternatively, there is the question of whether to generalize this and directly design a multiplatform API that would allow to read these keys without the need to use any external programs even on Linux (where "stty" is used). But I don't know which has a better chance of implementation, a simple extension for Windows or a more complex multiplatform solution. Best regards Patrik Pasterčík

Sara Golemon

1 year ago
On Thu, Oct 12, 2023 at 6:50 PM Patrik Pasterčík <Plaki@seznam.cz> wrote:
> I would like to propose extending PHP with support functions to enable > interactive console work on Windows system. I mean the ability to > control a PHP script running from the console using arrow keys, or > function keys like "F1", etc. For example, when a list is displayed to > the user and the user can select the desired item using the arrow keys > and confirm it with the Enter key. > > An analysis of other solutions, a detailed description of the problems > and a proof of concept is in the proposal in the issue here: > https://github.com/php/php-src/issues/12227 > > The short version is that on Linux you can use "fread" function to read > these keys (arrows, etc.) if you use an external `stty` command to set > the console input processing modes (for example the laravel/prompts > package use "stty -icanon -isig -echo"). However, there is no > equivalent of the `stty` command for Windows (without the need to use > the FFI extension, or the need to call an external program to read > pressed keys, see issue for more details), so I propose to extend PHP > with support functions for setting the console input mode > (ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, > ENABLE_VIRTUAL_TERMINAL_INPUT) by "SetConsoleMode" function, see: > https://learn.microsoft.com/en-us/windows/console/setconsolemode > > I would like to know your opinion on this proposal. Or if you have any > questions. > > And then as a next step I would like to create an RFC proposing these > functions for Windows support. And possibly I could help with > implementation (see my proof of concept in the issue). > > Alternatively, there is the question of whether to generalize this and > directly design a multiplatform API that would allow to read these keys > without the need to use any external programs even on Linux (where > "stty" is used). But I don't know which has a better chance of > implementation, a simple extension for Windows or a more complex > multiplatform solution. > >
Resurrecting this message from last October... I think this is a completely reasonable ask, the only bikeshedding I would have is about how we set ourselves up for future success. Specifically, I think there's a space (a small one) for OS-specific functionality. This sort of thing on Windows, exposing CoreFoundation framework utilities on macOS, and probably a whole host of Linux specific toys (oh hey look, we have a posix extension with exactly those things -- yes, I know macOS and windows are posix-ish, don't at me). My point is, I think we should consider `ext/win` (potentially starting in PECL and only later promoting to build-by-default) and use a deliberately chosen prefix namespace like `win_*()`. I plan to open an RFC for this, but I think a temperature check is in order and there were no replies to Patrik's initial email, so this is me shaking the tree. -Sara (re-sent, because php's spam filter, I guess?)

Christoph Becker

1 year ago
On 13.09.2024 at 16:07, Sara Golemon wrote:
> On Thu, Oct 12, 2023 at 6:50 PM Patrik Pasterčík <Plaki@seznam.cz> wrote: > >> An analysis of other solutions, a detailed description of the problems >> and a proof of concept is in the proposal in the issue here: >> https://github.com/php/php-src/issues/12227 > > Resurrecting this message from last October... I think this is a > completely reasonable ask, the only bikeshedding I would have is about how > we set ourselves up for future success. Specifically, I think there's a > space (a small one) for OS-specific functionality. This sort of thing on > Windows, exposing CoreFoundation framework utilities on macOS, and probably > a whole host of Linux specific toys (oh hey look, we have a posix extension > with exactly those things -- yes, I know macOS and windows are posix-ish, > don't at me). > > My point is, I think we should consider `ext/win` (potentially starting in > PECL and only later promoting to build-by-default) and use a deliberately > chosen prefix namespace like `win_*()`.
Note that there are already a couple of related extensions on PECL[1]; of these only win32service is still maintained, though. Still, having a look at these extensions might make sense. Also note that there are a couple of Windows specific functions in core, prefixed as sapi_win32_ (not sure why the sapi_ prefix had been chosen), and these already allow dealing with some console specifics (codepages, some events and vt100). Maybe these should be moved in an own extension, but unbundling these is likely an issue (even renaming would be) for BC reasons. [1] <https://pecl.php.net/package-search.php?pkg_name=win32> Christoph

Calvin Buckley

1 year ago
On Sep 13, 2024, at 3:24 PM, Christoph M. Becker <cmbecker69@gmx.de> wrote:
> > Note that there are already a couple of related extensions on PECL[1]; > of these only win32service is still maintained, though. Still, having a > look at these extensions might make sense.
I actually maintain one for IBM i [1], but it's not on PECL. I provide binary builds directly to users however, so I haven't rushed to put it on PECL just yet. [1]: https://github.com/SeidenGroup/php-ibmi