crossterm-rs / crossterm

Cross platform terminal library rust
MIT License
3.26k stars 279 forks source link

Crossterm works partially for MSYS, CGWIN, Mintty, CI/CD on Windows. #580

Open TimonPost opened 3 years ago

TimonPost commented 3 years ago

Describe the bug MSYS, CGWIN, Mintty (e.g. gitbash, windows ssh-terminals) have in common that they are tools that do not have access to the internal WinAPI of windows. This is a problem when they try to do operations that are implemented with system calls (not via ANSI codes). This is the case for retrieving operations like input, terminal size, cursor position. Operations that use ANSI codes work perfectly fine.

According to the creator of winapi rust crate, he said:

For MinTTY the handles are pipes, not consoles
if you have a pipe, you use ReadFile and WriteFile
regardless of whether it is just a pipe, or actually a TTY like git bash
if you have a console then you use the console functions like WriteConsoleW and ReadConsoleW
you never ever use libc stuff on windows

So apparently crossterm will have not access to the console handles in the mentioned emulators. And perhaps a workaround is to use read/write file instead for read/write console. But this solution is different for each type of retrieving operation. I am not sure how to go about this, if one has any ideas on the best way to go around this, feel free to leave a comment.

Related issues:

natbraun commented 3 years ago

For terminal size, on Git Bash (which uses MSYS2 behind the scenes), tput cols and tput lines seem to work.

rashil2000 commented 3 years ago

Hi @TimonPost

Just wanted to add something to the discussion here - someone might find it useful. (I tried many TUI programs that use the crossterm-rs backend - listed below.)

For mintty terminal

Most Windows-native, non-MSYS2 apps don't work inside mintty, You need to set the environment variable MSYS to enable_pcon, and then launch mintty - to make Windows-native apps work fully. More info can be found in this discussion mintty/wsltty#271.

For conhost, or (newer) Windows terminal

When using Git Bash, you need to launch the TUI apps with the TERM variable unset (MSYS2 sets it to xterm-256color), like so:

alias ttyper='TERM= ttyper'
alias lapin='TERM= lapin'
alias btm='TERM= btm'
alias rusty-rain='TERM= rusty-rain'
alias gping='TERM= gping'
alias pipes-rs='TERM= pipes-rs'
glenmurphy commented 2 years ago

FWIW, in MINGW64, if I build+run my crossterm app with "cargo run", it works great, but if I try to directly run the same generated exe, it creates the console spew outlined above.

Running "./target/debug/myexe.exe | cat" works (but slowly)

sigoden commented 2 years ago

In MSYS2 (git bash), directly run exe not works, but run with npm run or cargo run works.

Kreijstal commented 8 months ago

trick: when on mintty, try "shell command" instead of command, shell in msys2, uses dark magic to make it work better, sometimes, at least on windows 8 where there is no conpty