gum icon indicating copy to clipboard operation
gum copied to clipboard

On Windows, ENABLE_VIRTUAL_TERMINAL_INPUT flag remains set in console

Open subatomicjosue opened this issue 2 years ago • 1 comments

Describe the bug After running any gum command, input (stdin) in Windows terminal is left in odd state, whether running in the CMD terminal or the new Windows Terminal (wt.exe). For example, the arrow keys no longer work. Instead they return their corresponding ANSI escape sequences (eg ^[[A). Running the clear command will reset the terminal back to normal.

The problem appears to be related to the way the SetConsoleMode (https://docs.microsoft.com/en-us/windows/console/setconsolemode) is being set for stdin. It appears that ENABLE_VIRTUAL_TERMINAL_INPUT flag is being set for stdin but not being reset/removed.

Running the following piece of code in python, to access the Windows API resets the terminal:

    def _reset_stdin(self):
        """
        Remove ENABLE_VIRTUAL_TERMINAL_INPUT flag in stdin set by gum
        (via Win32 SetConsoleMode() call)
        """
    
        if __import__("platform").system() == "Windows":
            kernel32 = __import__("ctypes").windll.kernel32
            consoleFlagsInput = 64 + 32 + 4 + 2 + 1
            kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), consoleFlagsInput)
            del kernel32

Here -10 is the STD_INPUT_HANDLE according to https://docs.microsoft.com/en-us/windows/console/getstdhandle

To Reproduce Steps to reproduce the behavior:

  1. run any gum command in the standard dos terminal. eg gum choose one two three
  2. after command has completed, press the arrow keys. ansi escape sequence appears in stdout instead
  3. pressing backspace removes the entire word instead of single character

Expected behavior The terminal, in particular the stdin, under windows should remain the same as prior to running gum command.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser n/a
  • Version gum version v0.3.0 (c049d1a)

Additional context Add any other context about the problem here.

subatomicjosue avatar Aug 17 '22 00:08 subatomicjosue

Similar issues with 0.4.0. image

JanRK avatar Aug 17 '22 20:08 JanRK

This should be fixed by https://github.com/charmbracelet/bubbletea/pull/565 as soon as we merge & bump the bubbletea dependency in gum.

muesli avatar Oct 20 '22 09:10 muesli

When do you think an update would be available?

Galaxy102 avatar Nov 29 '22 08:11 Galaxy102

Hey do you think this is the same issue as in this video? Once returned from gum, arrow keys print what you see and ctrl+c etc don't work anymore

https://user-images.githubusercontent.com/7041726/205449396-8cf7182a-b3af-40fa-a226-7770c7daaf47.mp4

Edit: I just tried on gum version v0.8.1-0.20221118011134-eb3c5c1037a5 (main) and it still occurs

melMass avatar Dec 03 '22 15:12 melMass

Yes, it appears to be the same issue. It appears to be a bug in one of the dependencies gum.exe relies on.

subatomicjosue avatar Dec 05 '22 18:12 subatomicjosue