survey

Types

Answer is used to have different result types for Questions and Confirmations. Also allows handling errors

pub type Answer {
  StringAnswer(String)
  BoolAnswer(Bool)
  AnswerError(AskError)
  NoAnswer
}

Constructors

  • StringAnswer(String)
  • BoolAnswer(Bool)
  • AnswerError(AskError)
  • NoAnswer

AskError are different errors that could occur when handling prompts

pub type AskError {
  Input
  InvalidType
  Validation
}

Constructors

  • Input
  • InvalidType
  • Validation

Configure a survey prompt to present to the user.

Question allows receiving freeform String responses

Confirmation presents the user with a [y/n] prompt to record a Bool response

pub type Survey {
  Question(
    prompt: String,
    help: Option(String),
    default: Option(String),
    validate: Option(fn(String) -> Bool),
    transform: Option(fn(String) -> String),
  )
  Confirmation(
    prompt: String,
    help: Option(String),
    default: Option(Bool),
    transform: Option(fn(Bool) -> Bool),
  )
}

Constructors

  • Question(
      prompt: String,
      help: Option(String),
      default: Option(String),
      validate: Option(fn(String) -> Bool),
      transform: Option(fn(String) -> String),
    )

    Question allows receiving freeform String responses

    • prompt: printed prompt so the user knows expectations
    • help: optional help message to display if input is invalid or using ask(help: True)
    • default: optional default to use if empty input is received. If this is None, then input is required
    • validate: optional validation function to determine if input is acceptable
    • transform: optional transformation function to modify input
  • Confirmation(
      prompt: String,
      help: Option(String),
      default: Option(Bool),
      transform: Option(fn(Bool) -> Bool),
    )

    Confirmation presents the user with a [y/n] prompt to record a Bool response

    • prompt: printed prompt so the user knows expectations
    • help: optional help message to display if input is invalid or using ask(help: True)
    • default: optional default to use if empty input is received. If this is None, then input is required.
      • [y/n], [Y/n], [y/N] are added to the prompt for default None, True, and False respectively
    • transform: optional transformation function to modify input

Functions

pub fn ask(q: Survey, help help: Bool) -> Answer

ask will present the user with a prompt and handle the Answer

pub fn ask_fn(
  q: Survey,
  get_line: fn(String) -> Result(String, AskError),
  help help: Bool,
) -> Answer

same as ask, but allows providing a custom input handler

pub fn ask_many(
  qs: List(#(String, Survey)),
  help help: Bool,
) -> List(#(String, Answer))

ask_many allows presenting the user with many prompts sequentially

pub fn ask_many_fn(
  qs: List(#(String, Survey)),
  get_lines: List(fn(String) -> Result(String, AskError)),
  help help: Bool,
) -> List(#(String, Answer))

same as ask_many, but allows providing a custom input handler

pub fn new_confirmation(
  prompt prompt: String,
  help help: Option(String),
  default default: Option(Bool),
  transform transform: Option(fn(Bool) -> Bool),
) -> Survey

Constructor for a Confirmation that can make code more readable with labelled arguments

Example

survey.new_confirmation(
  prompt: "Are you a survey fan?:",
  help: Some("It's a great library"),
  default: Some(True),
  transform: Some(fn(_: Bool) -> Bool { True }),
),
pub fn new_question(
  prompt prompt: String,
  help help: Option(String),
  default default: Option(String),
  validate validate: Option(fn(String) -> Bool),
  transform transform: Option(fn(String) -> String),
) -> Survey

Constructor for a Question that can make code more readable with labelled arguments

Example

survey.new_question(
  prompt: "First Name:",
  help: Some("Please enter your first name"),
  default: None,
  validate: None,
  transform: None,
),
Search Document