aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/priv/prompt.gleam
blob: 6cee35ae8675f38974676fd66d185921c0689d48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)