Packages

A TUI library for gleam

Current section

Files

Jump to
gtui src gtui.erl
Raw

src/gtui.erl

-module(gtui).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_application/4, on_alt_screen/1, run/1]).
-export_type([application/2]).
-opaque application(GDE, GDF) :: {application,
GDE,
fun((GDE) -> binary()),
fun((GDE, GDF) -> {GDE, boolean()}),
list(fun(() -> {gleam@erlang@process:subject(GDF), fun(() -> nil)})),
boolean()}.
-spec new_application(
GDL,
fun((GDL) -> binary()),
fun((GDL, GDN) -> {GDL, boolean()}),
list(fun(() -> {gleam@erlang@process:subject(GDN), fun(() -> nil)}))
) -> application(GDL, GDN).
new_application(State, Render, Update, Events) ->
{application, State, Render, Update, Events, false}.
-spec on_alt_screen(application(GDU, GDV)) -> application(GDU, GDV).
on_alt_screen(Appl) ->
erlang:setelement(6, Appl, true).
-spec clear_lines(integer()) -> nil.
clear_lines(Num) ->
gleam@io:print(
gleam@string:repeat(
<<<<<<"\x{001b}["/utf8, "2K"/utf8>>/binary, "\x{001b}["/utf8>>/binary,
"1A"/utf8>>,
Num
)
),
gleam@io:print(<<"\r"/utf8>>).
-spec show(application(GEC, any()), GEC, integer()) -> integer().
show(Application, State, Previous_lines) ->
clear_lines(Previous_lines),
Content = (erlang:element(3, Application))(State),
gleam@io:print(Content),
erlang:length(gleam@string:split(Content, <<"\n"/utf8>>)).
-spec do_run(
application(GEK, GEL),
GEK,
gleam@erlang@process:selector(GEL),
list(fun(() -> nil)),
integer()
) -> nil.
do_run(Application, State, Selector, Cleanup, Previous_lines) ->
Lines = show(Application, State, Previous_lines - 1),
Msg = gleam_erlang_ffi:select(Selector),
{New_state, Running} = (erlang:element(4, Application))(State, Msg),
case Running of
true ->
do_run(Application, New_state, Selector, Cleanup, Lines);
false ->
show(Application, New_state, Previous_lines - 1),
case erlang:element(6, Application) of
true ->
gleam@io:print(<<"\x{001b}["/utf8, "?1049l"/utf8>>);
false ->
nil
end,
_pipe = Cleanup,
gleam@list:map(_pipe, fun(X) -> X() end),
nil
end.
-spec run(application(any(), any())) -> nil.
run(Application) ->
Subjects = begin
_pipe = erlang:element(5, Application),
gleam@list:map(_pipe, fun(T) -> T() end)
end,
case erlang:element(6, Application) of
true ->
gleam@io:print(<<"\x{001b}["/utf8, "?1049h"/utf8>>);
false ->
nil
end,
Selector = gleam_erlang_ffi:new_selector(),
Added_selector = begin
_pipe@1 = Subjects,
gleam@list:fold(
_pipe@1,
Selector,
fun(Sel, Sub) ->
gleam@erlang@process:selecting(
Sel,
erlang:element(1, Sub),
fun gleam@function:identity/1
)
end
)
end,
Cleanup = begin
_pipe@2 = Subjects,
gleam@list:map(_pipe@2, fun(T@1) -> erlang:element(2, T@1) end)
end,
do_run(
Application,
erlang:element(2, Application),
Added_selector,
Cleanup,
1
).