zachallaun

zachallaun

For anyone who’s worked on a terminal interface in Elixir, you may be excited to know that raw mode is coming to OTP 28: Implement lazy-read and noshell raw mode by garazdawi · Pull Request #8962 · erlang/otp · GitHub

If you’re unfamiliar with raw mode, the gist of it is that it allows terminals to read input without waiting for a newline, allowing for much more responsive terminal UIs. (There are drawbacks as well: You are fully responsible for what is printed, you lose all cursor movement (unless you re-implement it), et al.)

As a quick demo, here’s a script you can run if you have Erlang/OTP master installed:

# elixir getch.exs

defmodule Getch do
  def run do
    # new incantation to switch the terminal to raw mode in OTP 28
    :shell.start_interactive({:noshell, :raw})
    loop(nil)
  end

  def loop("q") do
    IO.write("\rDone!")
    System.halt(0)
  end

  def loop(last) do
    if last, do: print(last)
    loop(IO.getn("Next: ", 1))
  end

  def print(last) do
    IO.write(IO.ANSI.format(["\r", "Got: ", :green, inspect(last), "\r\n"]))
  end
end

Getch.run()

If you run this, you’ll notice that the script immediately responds to each character press. If you were to remove the :shell.start_interactive({:noshell, :raw}) and run the script using OTP 27, you’ll find the behavior to be quite different.

I’m really excited about this and hope to eventually incorporate some of this into Mneme. It seems like you can switch between raw and cooked modes interactively with ease, so it should be possible to “progressively enhance” terminal interfaces by switching to raw mode where you would normally accept input and then back to cooked mode afterwards. A quick example of such switching (use “s” to switch between modes):

defmodule Getch do
  def run do
    :shell.start_interactive({:noshell, :raw})
    loop(nil, :raw)
  end

  def loop("q", _) do
    IO.write("\rDone!")
    System.halt(0)
  end

  def loop("s", :raw) do
    :shell.start_interactive({:noshell, :cooked})
    loop(nil, :cooked)
  end

  def loop("s", :cooked) do
    :shell.start_interactive({:noshell, :raw})
    loop(nil, :raw)
  end

  def loop(last, state) do
    if last, do: print(last)
    IO.write([IO.ANSI.clear_line(), "\r"])
    loop(IO.getn("#{state}: ", 1), state)
  end

  def print(last) do
    IO.write(IO.ANSI.format(["\r", "got ", :green, inspect(last), "\r\n"]))
  end
end

Getch.run()

I’m excited to play around with this more and am eager to see/hear about what others are doing! Would love to hear from folks who have also done TUI work in Elixir. Some that come to mind: @fuelen @ausimian @AndyL

Showing Posts 1 to 10

zachallaun

zachallaun OP

Here’s what that last demo looks like:

getch

fuelen

fuelen

Whoa! Whistles and bells! I have to play with this :slightly_smiling_face:

lawik

lawik

Nerves Core Team

Owl can get a lot more live now :slight_smile:

AndyL

AndyL

@zachallaun thanks for posting these demos! Ability to on-the-fly switch between raw and cooked mode is super nice.

Here’s a demo tweak to handle special keys:

defmodule Getch do
  def run do
    # new incantation to switch the terminal to raw mode in OTP 28
    :shell.start_interactive({:noshell, :raw})
    loop(nil)
  end

  def loop("q") do
    IO.write("\rDone!")
    System.halt(0)
  end

  def loop("\d"), do: loop(:backspace)
  def loop("\r"), do: loop(:enter)
  def loop("\t"), do: loop(:tab)
  def loop("\e"), do: loop(:escape)
  def loop("\e[A"), do: loop(:arrow_up)
  def loop("\e[B"), do: loop(:arrow_down)
  def loop("\e[C"), do: loop(:arrow_right)
  def loop("\e[D"), do: loop(:arrow_left)
  def loop("\e[F"), do: loop(:end)
  def loop("\e[H"), do: loop(:home)
  def loop("\e[1~"), do: loop(:home)
  def loop("\e[3~"), do: loop(:delete)
  def loop("\e[4~"), do: loop(:end)
  def loop("\e[5~"), do: loop(:pg_up)
  def loop("\e[6~"), do: loop(:pg_dn)

  def loop(last) do
    if last, do: print(last)
    loop(IO.getn("Next: ", 4))
  end

  def print(last) do
    IO.write(IO.ANSI.format(["\r", "Got: ", :green, inspect(last), "\r\n"]))
  end
end

Getch.run()

Anyone have a guesstimate for OTP28 release date?

zachallaun

zachallaun OP

The last three major releases (at least, that’s as far back as I looked) have been in May, so we still have a good half a year to go. The first RC releases in February though!

MRdotB

MRdotB

Does this mean iex can benefit from it and we can have vim bindings :slight_smile: ?

zachallaun

zachallaun OP

It’ll be ~4 years before Elixir exclusively supports OTP 28+. It may be possible to conditionally add certain IEx features, but the core team may not want a divergent experience, so I won’t personally hold out hope for that just yet. :slight_smile:

There certainly is a laundry-list of really neat shell features that become possible with raw mode, though! I’d love as-you-type completion options.

MRdotB

MRdotB

Custom IEx feature !
Good thing livebook has a vi mode :grinning:
What I would like for IEx to be perfect is a history fuzzy finder / session history per project / vi mode.
I hacked something with tmux + fzf 4 years ago that I’m still using today but the integration is not perfect
https://asciinema.org/a/532551

venkatd

venkatd

Wow this looks neat! Does this mean that, in theory, a iex could allow a mode to navigate a large data structure, then go back to using iex as normal?

zachallaun

zachallaun OP

Possibly, yes.

I did a little bit of playing around and if you call :shell.start_interactive in a shell, you get {:error, :already_started}, so you can’t “swap” into raw mode without some additional control over what IEx is already doing. I believe Elixir 1.19 will be the first one to support OTP 28, so perhaps a discussion can be had about what changes could be made to support richer terminal interactions once 1.19 dev starts.

Where Next? Top

Trending in Discussions Top

AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4663 61218 595
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
515 12931 101
New
wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews