diff options
author | J.J <thechairman@thechairman.info> | 2024-05-30 21:50:02 -0400 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2024-05-30 21:50:02 -0400 |
commit | 612fd986ab1e00b6d34dc1937136250e08e89325 (patch) | |
tree | a3c93952040c6afdf348b5831619a45db7ba0a2e /aoc2023/build/packages/adglent/src/priv/prompt.gleam | |
parent | 231c2b688d1e6cf0846d46e883da30e042a9c6cf (diff) | |
download | gleam_aoc-612fd986ab1e00b6d34dc1937136250e08e89325.tar.gz gleam_aoc-612fd986ab1e00b6d34dc1937136250e08e89325.zip |
cleanup
Diffstat (limited to 'aoc2023/build/packages/adglent/src/priv/prompt.gleam')
-rw-r--r-- | aoc2023/build/packages/adglent/src/priv/prompt.gleam | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/aoc2023/build/packages/adglent/src/priv/prompt.gleam b/aoc2023/build/packages/adglent/src/priv/prompt.gleam new file mode 100644 index 0000000..6cee35a --- /dev/null +++ b/aoc2023/build/packages/adglent/src/priv/prompt.gleam @@ -0,0 +1,38 @@ +import gleam/result +import gleam/string + +pub fn confirm(message: String, auto_accept: Bool) -> Bool { + auto_accept || case + get_line(message <> "? (Y/N): ") + |> result.unwrap("n") + |> string.trim() + { + "Y" | "y" -> True + _ -> False + } +} + +pub fn value(message: String, default: String, auto_accept: Bool) -> String { + case get_value_of_default(message, default, auto_accept) { + "" -> default + value -> value + } +} + +fn get_value_of_default(message: String, default: String, auto_accept: Bool) { + case auto_accept { + True -> default + False -> + get_line(message <> "? (" <> default <> "): ") + |> result.unwrap("") + |> string.trim() + } +} + +pub type GetLineError { + Eof + NoData +} + +@external(erlang, "adglent_ffi", "get_line") +pub fn get_line(prompt prompt: String) -> Result(String, GetLineError) |