Is it possible to enable VT100/ANSI escape codes from PHP 5 in Windows

独自空忆成欢 提交于 2019-12-10 19:23:24

问题


I'm in the process of upgrading an old PHP 5 app to PHP 7. I'm using Codeception for unit testing because it has nice colour output, making it easy to see if all the tests have passed or not.

Things I have tried:

  • Upgrade to PHP 7: the app crashes
  • Run Codeception with no special flags: ANSI escape codes are printed to screen making the output hard to read
  • Run Codeception with the --no-colors flag: output is a single colour taking longer to recognise a 100% passing run, or which tests have failed
  • Use ANSICON instead of Windows Command Prompt: PHP 5 runs incredibly slowly, taking an hour to produce a report with coverage, when the same run under the Command Prompt only takes 2 minutes

Things I have not tried:

  • Setting a registry value so that ANSI escape codes are always on unless turned of by the running program: this solution is not portable to other developers

I'd like to do the same as the source code listed further below, except within PHP. Something like:

if (is_windows_console()) {
  if (has_vt100_extensions()) {
    if (are_vt100_extensions_disabled()) {
      vt100_extensions->on();
    }
  }
}

I have no idea if it's even possible to issue commands to the Command Prompt API from within PHP.

The following C code is from the PHP 7 source:

PHP_WINUTIL_API BOOL php_win32_console_fileno_set_vt100(zend_long fileno, BOOL enable)
{
    BOOL result = FALSE;
    HANDLE handle = (HANDLE) _get_osfhandle(fileno);

    if (handle != INVALID_HANDLE_VALUE) {
        DWORD events;

        if (fileno != 0 && !GetNumberOfConsoleInputEvents(handle, &events)) {
            // Not STDIN
            DWORD mode;

            if (GetConsoleMode(handle, &mode)) {
                DWORD newMode;

                if (enable) {
                    newMode = mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
                }
                else {
                    newMode = mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING;
                }
                if (newMode == mode) {
                    result = TRUE;
                }
                else {
                    if (SetConsoleMode(handle, newMode)) {
                        result = TRUE;
                    }
                }
            }
        }
    }
    return result;
}

Is there a way to emulate this functionality from within a PHP 5 script?

I've found the following related questions but I'm still no closer to even starting:

  • How to call winapi functions from PHP?
  • How do I make Win32 API calls from PHP?
  • How to create C# DLL to use in PHP

来源:https://stackoverflow.com/questions/58775351/is-it-possible-to-enable-vt100-ansi-escape-codes-from-php-5-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!