crossterm-rs/crossterm

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

TimonPost opened this issue · 5 comments

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.

  • Cygwin a large collection of GNU and Open Source tools that provide functionality similar to a Linux distribution on Windows.The unixy tools in MSYS2 are directly based on Cygwin, so there is some overlap there.
  • While Cygwin focuses on building Unix software on Windows as is, MSYS2 focuses on building native software built against the Windows APIs.
  • Mintty is an open-source terminal emulator for Cygwin, a Unix-like environment for Windows.
  • Most CI systems will not have access to a native terminal device.

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:

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

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'

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)

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

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