chhackett / vty-windows

BSD 3-Clause "New" or "Revised" License
5 stars 1 forks source link

Discussion: How to support different terminal environments #15

Closed chhackett closed 5 months ago

chhackett commented 11 months ago

Supporting terminals such as mintty or ConEmu correctly may be difficult.

Related discussion is happening on the Windows Terminal github: https://github.com/microsoft/terminal/issues/7434

ShrykeWindgrace commented 11 months ago

Tested alacritty, it almost works.

Otherwise everything seems to be Ok.

chhackett commented 10 months ago

Another alacritty problem: mouse mode doesn't work. Apparently this is a known issue on Windows: https://github.com/alacritty/alacritty/issues/1663 So we probably can't do anything about that. But hopefully we can detect that the environment doesn't support mouse mode and report it as not available.

I also tried running a git-bash shell. That doesn't work at all. App crashes with: $ vty-mode-demo.exe vty-mode-demo.exe: GetConsoleMode: invalid argument (The handle is invalid.)

I spent some time earlier trying to figure this out and have not made any progress yet.

chhackett commented 10 months ago

Perhaps we can use ALACRITTY_LOG env var to detect app is running in alacritty.

ShrykeWindgrace commented 10 months ago

I propose to track these issues as a table with the following info:

ShrykeWindgrace commented 10 months ago

Template is here: https://github.com/chhackett/vty-windows/wiki/TerminalSupport

I'll update it as the time goes

chhackett commented 10 months ago

Beautiful. I'll add as well.

ShrykeWindgrace commented 10 months ago

The problem with environment variables is that they are inherited. Say, I run WindowsTerminal (so its WT_SESSION is set), spawn a cmd.exe (separate legacy window), and from within spawn an alacritty (separate almost-new-school window). Now we have both WT_SESSION and ALACRITTY_LOG available.

Best I could think of is an early attempt to grab Windows Console Handle - if there is an exception, then the app will not work at all (with our current code), we catch that exception and gracefully stop - warn the user, show links to the list of supported terminals, etc.

Most importantly, we can do that prior to modifying anything in the terminal state, so we do not leave it in an incoherent state because of a crash.

If, on the other hand, we could grab that handle, we can proceed and hope that at least basic features will work. We still won't have a 100%-working way (only an approximation) to distinguish between e.g. WT and Alacritty, but that bug is slightly less important.

ShrykeWindgrace commented 10 months ago

Speaking of cygwin and its relatives (mintty emulator, the one spawned by git bash, msys2, and so on, if the windows setting is "let the CLI app pick its terminal emulator")

That being said, I do not have an ETA for the proof-of-concept.

chhackett commented 8 months ago

@ShrykeWindgrace So I've been working on support for mintty/msys2 terminals the last few weeks. I have it mostly working except for two problems. Getting the current window size is turning out to be very difficult. I initially tried to use the ioctl system call to read the TIOCGWINSZ struct. This doesn't work because it seems that mintty/mingw does not provide an implementation of ioctl. The next logical approach is what I have currently, which is to send the 'report screen size' escape sequence ("\ESC[18t"), then read the response escape sequence on stdin.

But this approach has failed. Even though the terminal is emitting the screen size sequence, the application is not reading it. Not sure why. But when I exit the application, I see the screen size sequence on the command line, like this: $ ;43;131t;43;131t;43;131t

The second problem is that the terminal stops responding to vty output at some point. This may or may not have anything to do with the screen size issue. In fact, I think it's unrelated. I ran a test where no screen size escape sequence is emitted, and still see output 'freezing'. Maybe it's related to some sequence that vty is emitting.

Anyway, if we can figure these two problems out, I believe everything else works, and we can release support for mintty. If you have some time, would you be able to take a look?

My work is checked in on the mintty-support branch.

ShrykeWindgrace commented 8 months ago

@chhackett that's quite a feat!

I do not think I will have much time for vty in the coming weeks, but I'll certainly take a look.

Uneducated guess with "\ESC[18t" and its output in the terminal - maybe it gets written to stderr or some other handle? This would explain both the inability to read and the presence on screen. That being said, I don't know whether stderr is readable at all =)

ShrykeWindgrace commented 6 months ago

@chhackett I finally got to work with your mintty support implementation.

First of all, it works almost out of the box=) It requires stty, and in my mobaXterm that was a stty.exe, had to make a symlink.

My tests were with vty-event-echo and vty-mode-demo from vty-crossplatform:

I tried launching my copies of mintty/git-bash from git-for-windows - it seems that it became intelligent enough to pick up the same backend as WindowsTerminal; apps built with released vty-crossplatform work out of the box (less work for us =) )

I'll try to run the interactive terminal test later.

chhackett commented 6 months ago

Ok, thanks for trying it out! I'll see if I can reproduce that event behavior this weekend sometime. I never noticed that pattern of only handling every 4th event.

Btw, the other thing we will need to figure out is how to detect whether the environment is cmd/powershell vs mintty. Right now, I just try calling the getConsoleMode - if it fails, then we must be in 'mintty' environment, otherwise regular cmd/powershell.

But that feels very unhaskelly. I tried using the mintty library which provides a function isMintty. But that always returned false for me even when it was running in mintty... If we could figure out the eventing issue, and that problem, we might be done.

chhackett commented 5 months ago

@ShrykeWindgrace I found a different solution. It turns out, ghcup worked in MSYS2 terminals already without any changes. After investigating, I discovered that by using CONIN$ and CONOUT$ strings, we are able to configure the MSYS terminal for VT processing with no other changes required. So I did that, and it seems to work great. Thanks for @hasufell for the solution.

chhackett commented 5 months ago

@ShrykeWindgrace @jdaugherty Just a heads up that I've released version 2.0.3 which adds support for mintty terminals, and seems to function correctly with alacritty, and ConEmu as well. At this point, I think we have support for all the major terminals.