View Source Tux.Prompt (Tux v0.4.0)

Helper functions for retrieving user input as: strings, integers, floats or for asking for confirmations.

  • Default values will be displayed between [...] at the end of the prompt.
  • Supports default values when no value (just enter) is typed.

Examples

alias Tux.Prompt

with true <- Prompt.for_confirmation("Really continue"),
     {:ok, name} <- Prompt.for_string("What's your name", "Joe Doe"),
     {:ok, age} <- Prompt.for_string("What's your age"),
     {:ok, salary} <- Prompt.for_float("What's your salary") do
      IO.puts("Name: #{name}, Age: #{age}, Salary: #{salary}")
end

Summary

Types

They type for the prompt message shown to the end user asking for a confirmation or some input.

Functions

Prompt and parse a yes or no answer

Prompt and parse a float answer

Prompt and parse an integer answer

Prompt and collect a string with the whitespace trimmed

Types

prompt()

@type prompt() :: String.t()

They type for the prompt message shown to the end user asking for a confirmation or some input.

Functions

for_confirmation(prompt, default \\ :no)

@spec for_confirmation(prompt(), :yes | :no) :: boolean()

Prompt and parse a yes or no answer:

Tux.Prompt.for_confirmation("Really delete")
false

Tux.Prompt.for_confirmation("Really delete", :yes)
true

for_float(prompt, default \\ nil)

@spec for_float(prompt(), nil | float()) :: {:ok, float()} | :error

Prompt and parse a float answer:

Tux.Prompt.for_integer("What's your salary?")
{:ok, 25.0}

Tux.Prompt.for_integer("What's your salary?")
:error

for_integer(prompt, default \\ nil)

@spec for_integer(prompt(), nil | integer()) :: {:ok, integer()} | :error

Prompt and parse an integer answer:

Tux.Prompt.for_integer("What's your age?")
{:ok, 25}

Tux.Prompt.for_integer("What's your age?")
:error

for_string(prompt, default \\ nil)

@spec for_string(prompt(), nil | String.t()) :: {:ok, String.t()}

Prompt and collect a string with the whitespace trimmed:

Tux.Prompt.for_integer("What's your name?")
{:ok, "Tux"}

Tux.Prompt.for_integer("What's your name?", "Joe Doe")
{:ok, "Joe Doe"}