Wait... ExRatatui apps in Erlang and Gleam? Why not?
I love the BEAM.
So we all know the BEAM is great, no? Talking to other languages is something it’s really good at. But especially between BEAM brothers.
And I got nerd-sniped at Code BEAM Europe. I went there to talk about TUIs in Elixir, and ended up surrounded by Erlang and Gleam people. So…
Yes. Since 0.20 you can write ex_ratatui apps in Erlang and Gleam, and bring its superpowers into your toolset.
How it works
There’s no separate API. No wrapper library either.
An app was always just a module with a few callbacks: mount, render, handle_event. The only Elixir piece was the use ExRatatui.App macro. So now ExRatatui.App.start_link/2 starts any module that implements them, and it doesn’t care which language wrote it.
That was pretty much the whole change.
Erlang
rebar3 needs a plugin to build Elixir deps. The usual one, rebar_mix, leaves the NIF behind, so I went with exerl, which keeps it. Then just write the callbacks:
-module(my_tui).
-behaviour('Elixir.ExRatatui.App').
-export([start/0, mount/1, render/2, handle_event/2]).
start() ->
{ok, Pid} = 'Elixir.ExRatatui.App':start_link(?MODULE, []),
Ref = monitor(process, Pid),
receive {'DOWN', Ref, process, Pid, _Reason} -> ok end.
mount(_Opts) ->
{ok, 0}.
render(Count, #{width := Width, height := Height}) ->
Text = <<"Counter: ", (integer_to_binary(Count))/binary>>,
Paragraph = 'Elixir.ExRatatui.Widgets.Paragraph':'__struct__'(#{text => Text}),
Area = 'Elixir.ExRatatui.Layout.Rect':'__struct__'(#{x => 0, y => 0, width => Width, height => Height}),
[{Paragraph, Area}].
handle_event(#{code := <<"q">>}, Count) -> {stop, Count};
handle_event(#{code := <<"k">>}, Count) -> {noreply, Count + 1};
handle_event(_Event, Count) -> {noreply, Count}.
Elixir modules are just atoms and structs are just maps, so Erlang builds the widgets directly. Not the prettiest thing, but a couple of helper functions and you forget about it.
There’s a full example, tests included.
Gleam
gleam add ex_ratatui
And here it gets fun, because Gleam’s custom types compile to exactly the atoms and tuples the runtime expects. Ok(0) is {ok, 0}, Noreply(count) is {noreply, Count}.
pub type Next {
Noreply(Int)
Stop(Int)
}
pub fn mount(_options: Dynamic) -> Result(Int, Nil) {
Ok(0)
}
pub fn render(count: Int, frame: Frame) -> List(#(Widget, Rect)) {
[#(paragraph("Counter: " <> int.to_string(count)), area(frame))]
}
pub fn handle_event(event: Event, count: Int) -> Next {
case key_code(event) {
"q" -> Stop(count)
"k" -> Noreply(count + 1)
_ -> Noreply(count)
}
}
The one thing Gleam can’t write is an Elixir struct. So paragraph, area and key_code live in a tiny Erlang file next to it, bound with @external.
The full example has all of it, tests included too.
Limitations
Some, to be honest. Elixir still has to be installed, since ex_ratatui is a Mix project. No custom widgets, as that’s an Elixir protocol. And in Gleam every widget needs that small Erlang shim today. A package with typed widgets and events is the obvious next step for this, so let’s see if it’s needed.
The rest comes along
Same runtime underneath, so supervision, SSH, Erlang distribution, the reducer style and headless testing all work from both languages. The guide has all the details.
I never get tired of all the awesome stuff I’ve been able to do and discover by building this library. And this is no exception :)!
Tell your Erlangers and Gleamers about it, and happy BEAM-TUI coding 🍻.