Getting IO server to cooperate with raw TTY

I’m playing with building a TUI entirely in Elixir (no curses or anything). I use stty to put the TTY in raw mode, and that works fine when I read user input from STDIN, but I’m trying to change it to read user input explicitly from /dev/tty so content can be piped in. But I’m running into issues where the server wrapping the file doesn’t seem to work with TTY raw mode.

When I open /dev/tty with the :raw option, it doesn’t spin up an IO server, and I have to use to read from it. This gets characters right as they’re typed, but it seems to grab bytes instead of unicode codepoints.

But if I don’t pass the :raw option and try to use it doesn’t actually get characters until there’s an EOF on a new line, and I end up needing the icanon and icrnl flags to even do that. But seems to actually return codepoints (I would prefer graphemes, but I don’t see a way to do that).

Are y’all aware of a way to get on a file to work with a raw TTY mode?

#!/usr/bin/env sh
save_state=$(stty -g)
stty -f /dev/tty raw

# reads immediately after keypress
# elixir --erl '-noshell' -e '1 |> |> IO.inspect(label: "read")'

# reads immediately after keypress but bytes, not codepoints
# elixir --erl '-noshell' -e '"/dev/tty" |>!([:read, :utf8, :raw]) |> |> IO.inspect(label: "read")'

# reads after enter + eof (ctrl+d)
# stty -f /dev/tty icanon icrnl
# elixir --erl '-noshell' -e '"/dev/tty" |>!([:read, :utf8]) |> :io.get_chars([], 1) |> IO.inspect(label: "read")'

# reads after enter + eof (ctrl+d)
stty -f /dev/tty icanon icrnl
elixir --erl '-noshell' -e '"/dev/tty" |>!([:read, :utf8]) |> |> IO.inspect(label: "read")'

stty -f /dev/tty "$save_state"