aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/priv@prompt.erl
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2024-02-03 15:09:54 -0500
committerHJ <thechairman@thechairman.info>2024-02-03 15:09:54 -0500
commit96a3c5c179d8d3fff24eb2953e45f8dd15e2714c (patch)
tree0a67bc0cfeabe51740bb049c61f16f1ac3bdd4ff /aoc2023/build/packages/adglent/src/priv@prompt.erl
parent547fe03cf43105f46160e2dd9afff21637eaaf47 (diff)
downloadgleam_aoc-96a3c5c179d8d3fff24eb2953e45f8dd15e2714c.tar.gz
gleam_aoc-96a3c5c179d8d3fff24eb2953e45f8dd15e2714c.zip
cleanup
Diffstat (limited to 'aoc2023/build/packages/adglent/src/priv@prompt.erl')
-rw-r--r--aoc2023/build/packages/adglent/src/priv@prompt.erl53
1 files changed, 53 insertions, 0 deletions
diff --git a/aoc2023/build/packages/adglent/src/priv@prompt.erl b/aoc2023/build/packages/adglent/src/priv@prompt.erl
new file mode 100644
index 0000000..0277f14
--- /dev/null
+++ b/aoc2023/build/packages/adglent/src/priv@prompt.erl
@@ -0,0 +1,53 @@
+-module(priv@prompt).
+-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
+
+-export([get_line/1, confirm/2, value/3]).
+-export_type([get_line_error/0]).
+
+-type get_line_error() :: eof | no_data.
+
+-spec get_line(binary()) -> {ok, binary()} | {error, get_line_error()}.
+get_line(Prompt) ->
+ adglent_ffi:get_line(Prompt).
+
+-spec confirm(binary(), boolean()) -> boolean().
+confirm(Message, Auto_accept) ->
+ Auto_accept orelse case begin
+ _pipe = adglent_ffi:get_line(<<Message/binary, "? (Y/N): "/utf8>>),
+ _pipe@1 = gleam@result:unwrap(_pipe, <<"n"/utf8>>),
+ gleam@string:trim(_pipe@1)
+ end of
+ <<"Y"/utf8>> ->
+ true;
+
+ <<"y"/utf8>> ->
+ true;
+
+ _ ->
+ false
+ end.
+
+-spec get_value_of_default(binary(), binary(), boolean()) -> binary().
+get_value_of_default(Message, Default, Auto_accept) ->
+ case Auto_accept of
+ true ->
+ Default;
+
+ false ->
+ _pipe = adglent_ffi:get_line(
+ <<<<<<Message/binary, "? ("/utf8>>/binary, Default/binary>>/binary,
+ "): "/utf8>>
+ ),
+ _pipe@1 = gleam@result:unwrap(_pipe, <<""/utf8>>),
+ gleam@string:trim(_pipe@1)
+ end.
+
+-spec value(binary(), binary(), boolean()) -> binary().
+value(Message, Default, Auto_accept) ->
+ case get_value_of_default(Message, Default, Auto_accept) of
+ <<""/utf8>> ->
+ Default;
+
+ Value ->
+ Value
+ end.