Windows の conpty を使うことで、クロスプラットフォームな tui 環境は整ってきた様子。 要するに curses への依存を除去して、 POSIX API への依存を Win32 API と付け変えできるようにすれば、 PosixWindows 共用のコードにできる。 gorust 界隈の tui アプリ neovim、wezterm なんかで既に達成されている。

c++ でも今風の TUI を作りたいと思っていたのだが、libvtermFTXUI を併用することでできそうだ。 libvterm, FTXUI 共に Windows Unix 両方で動くコンパクトなライブラリだ。 libvterm で vt100 escape sequence を ScreenGrid に翻訳して、 FTXUI の Screen に描画する。 FTXUIScreenGrid の描画に加えて、PosixWindows の 入力、resize, mouse をさばくこともできる。

resize
rawinput stdin
----+ +---+--->+---+--->+-------+
TERM|-->|APP| |pty| |process|
| | | +---+<---+-------+
| | | ^|stdout
| | | ||VT100 escape sequence
| | | |v
| | | +--------+
|<--| |<---|libvterm|
----+ +---+--->+--------+
vt100 resize
draw

上の図では、TERM が curses/terminfo で、process が posix であることが多い。 resize 周りは、ioctrl.h 。 vt100 parser と TERM への出力が分離されていない難解な塊だったりする。

process まわりは conpty を使って手作りできる。

struct Pty
{
HPC Console; // conpty
HANDLE ReadPipe; // child process stdout reader
HANDLE WritePipe; // child process stdin writer
};
terminal/samples/ConPTY/EchoCon at main · microsoft/terminal
The new Windows Terminal and the original Windows console host, all in the same place! - microsoft/terminal
terminal/samples/ConPTY/EchoCon at main · microsoft/terminal favicon https://github.com/microsoft/terminal/tree/main/samples/ConPTY/EchoCon
terminal/samples/ConPTY/EchoCon at main · microsoft/terminal

ReadPipe を thread で ReadFile し、 別の thread で、hThread を WaitForSingleObject して終了を監視するのがよさそうだ。

あとは、 sixel とか試してみたのだが conpty は sixel を通してくれないみたい。 chafaWindows でも動いたので、こっちで遊んでみる予定。

uim-fep から posix をはがして、 Windows でも動くようにできないか画策中…