Packages

A Gleam TUI (terminal user interface) framework targeting erlang

Current section

Files

Jump to
shore src shore.erl
Raw

src/shore.erl

-module(shore).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([spec/6, start/1, keybinds/5, default_keybinds/0, send/1, exit/0, on_timer/1, on_update/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/shore.gleam", 44).
?DOC(
" A shore application is made up of these base parts. Following The Elm\n"
" Architecture, you must define an init, view and update function which shore\n"
" will handle calling.\n"
"\n"
" Additionally, a simple subject to pass the exit call to is required.\n"
" keybinding for the framework level events such as exiting and ui focusing.\n"
" And finally redraw for defining when the applicaiton should be redrawn, either on update messages or on a timer.\n"
"\n"
" ## Example\n"
" ```\n"
" import gleam/erlang/process\n"
" import shore\n"
"\n"
" pub fn main() {\n"
" let exit = process.new_subject()\n"
" let assert Ok(_actor) =\n"
" shore.spec(\n"
" init:,\n"
" update:,\n"
" view:,\n"
" exit:,\n"
" keybinds: shore.default_keybinds(),\n"
" redraw: shore.on_timer(16),\n"
" )\n"
" |> shore.start\n"
" exit |> process.receive_forever\n"
" }\n"
"\n"
" ```\n"
).
-spec spec(
fun(() -> {HPN, list(fun(() -> HPO))}),
fun((HPN) -> shore@internal:node_(HPO)),
fun((HPN, HPO) -> {HPN, list(fun(() -> HPO))}),
gleam@erlang@process:subject(nil),
shore@internal:keybinds(),
shore@internal:redraw()
) -> shore@internal:spec(HPN, HPO).
spec(Init, View, Update, Exit, Keybinds, Redraw) ->
{spec, Init, View, Update, Exit, Keybinds, Redraw}.
-file("src/shore.gleam", 56).
?DOC(" Starts the application actor and returns its subject\n").
-spec start(shore@internal:spec(any(), HPW)) -> {ok,
gleam@erlang@process:subject(shore@internal:event(HPW))} |
{error, gleam@otp@actor:start_error()}.
start(Spec) ->
shore@internal:start(Spec).
-file("src/shore.gleam", 65).
?DOC(
" Set keybinds for various shore level functions, such as moving between\n"
" focusable elements such as input boxes and buttons, as well as exiting and\n"
" triggering button events.\n"
).
-spec keybinds(
shore@key:key(),
shore@key:key(),
shore@key:key(),
shore@key:key(),
shore@key:key()
) -> shore@internal:keybinds().
keybinds(Exit, Submit, Focus_clear, Focus_next, Focus_prev) ->
{keybinds, Exit, Submit, Focus_clear, Focus_next, Focus_prev}.
-file("src/shore.gleam", 82).
?DOC(
" A typical set of keybindings\n"
"\n"
" - exit: `ctrl+x`\n"
" - submit: `enter`\n"
" - focus_clear: `escape`\n"
" - focus_next: `tab`\n"
" - focus_prv: `shift+tab`\n"
).
-spec default_keybinds() -> shore@internal:keybinds().
default_keybinds() ->
{keybinds, {ctrl, <<"X"/utf8>>}, enter, esc, tab, back_tab}.
-file("src/shore.gleam", 95).
?DOC(
" Allows sending a message to your TUI from another actor. This can be used,\n"
" for example, to push an event to your TUI, rather than have it poll.\n"
).
-spec send(HQD) -> shore@internal:event(HQD).
send(Msg) ->
shore@internal:send(Msg).
-file("src/shore.gleam", 102).
?DOC(
" Manually trigger the exit for your TUI. Normally this would be handled\n"
" through the exit keybind.\n"
).
-spec exit() -> shore@internal:event(any()).
exit() ->
shore@internal:exit().
-file("src/shore.gleam", 107).
?DOC(" Redraw every x milliseconds\n").
-spec on_timer(integer()) -> shore@internal:redraw().
on_timer(Ms) ->
{on_timer, Ms}.
-file("src/shore.gleam", 112).
?DOC(" Redraw in response to events. Suitable for infrequently changing state.\n").
-spec on_update() -> shore@internal:redraw().
on_update() ->
on_update.