brettbeatty

brettbeatty

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 :file.read/2 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 IO.read/2 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 IO.read/2 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 IO.read/2 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.read |> IO.inspect(label: "read")'

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

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

stty -f /dev/tty "$save_state"
#io

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement