promptly
Validated user input.
Installation
gleam add promptly
Usage
Configure your prompter to be as simple as you like:
import gleam/io
import promptly
pub fn main() -> Nil {
let name = promptly.new() |> promptly.prompt(fn(_) { "Name: " })
io.println("Hello, " <> name)
}
Name: Joe
Hello, Joe
… or build something more complex with input validation:
import gleam/io
import gleam/option.{type Option, None, Some}
import gleam/string
import promptly.{type Error, InputError, ValidationFailed}
type EntityError {
NotProvided
Bad(String)
}
pub fn main() -> Nil {
let entity =
promptly.new()
|> promptly.with_validator(validator)
|> promptly.prompt(formatter)
io.println("Hello, " <> entity <> "!")
}
fn validator(entity: String) -> Result(String, EntityError) {
case string.lowercase(entity) {
"" -> Error(NotProvided)
"joe" | "world" -> Ok(entity)
_ -> Error(Bad(entity))
}
}
fn formatter(error: Option(Error(EntityError))) -> String {
let prompt = "Who are you: "
case error {
None -> prompt
Some(error) -> {
let error = case error {
InputError -> "Input failed!"
ValidationFailed(error) ->
case error {
NotProvided -> "C'mon!"
Bad(entity) ->
promptly.quote_text(entity)
<> "? That sounds lovely, but try again!"
}
}
error <> "\n" <> prompt
}
}
}
Who are you:
C'mon!
Who are you: Bob
"Bob"? That sounds lovely, but try again!
Who are you: Joe
Hello, Joe!
Tips
- Use your own error types, along with
prompt_once
, to build custom prompt loops with specialized logic. - Add
gleam-community/ansi
for pretty output.