Skip to content

silbermm/prompt

Repository files navigation

HEADS UP

All future development will happen at https://git.sr.ht/~ahappydeath/prompt

prompt_with_text_lighter

Hex.pm

Terminal UI Library for Elixir

Run in Livebook

Motivation

To create a really great development experience and API for Elixir developers that want to build commandline tools.

Installation

Add prompt to your list of dependencies in mix.exs:

def deps do
  [
    {:prompt, "~> 0.8.0"}
  ]
end

Read the official documentation

Basic Usage

All of the following commands take a keyword list of options for things like text color and positioning.

Display text on the screen

Prompt.display/2

Prompt.display("Hello, world!")

Ask the user for input

Prompt.text/2 Useful for prompting the user to enter freeform text

Prompt.text("Enter info here")

Will display:

> Enter info here:

and wait for the user to enter text

Ask the user for a password

Prompt.password/2 When you need to hide the input that the user types

Prompt.password("Enter password")

Ask the user for confirmation

Prompt.confirm/2

Prompt.confirm("Are you sure?")

Will display:

> Are you sure? (Y/n):

and will allow the user to just press [enter] to confirm

If you'd prefer n to be the default pass the default_answer option

Prompt.confirm("Are you sure?", default_answer: :no)

Returns :yes or :no based on the answer

Custom confirmation choices

Prompt.choice/2 Sometimes yes/no aren't the only choices a user can make, this method allows you to pass any choices as the confirmation.

Prompt.choice("Accept, Reload or Cancel", accept: "a", reload: "r", cancel: "c")

displays

> Accept, Reload or Cancel (A/r/c):

Returns the key of the answer i.e :accept, :reload or cancel in this exammple

List of selections

Prompt.select/2 To show the user a list of options to select from

Prompt.select("Choose a protocol", ["file://", "ssh://", "ftp://"])

Displays:

  [1] file://
  [2] ssh://
  [3] ftp://
> Choose a protocol [1-3]:

and returns a string of their choice

Tables

Prompt.table/2 To show a table of data

Prompt.table([["Hello", "from", "the", "terminal!"],["this", "is", "another", "row"]])

Will display

> +-------+------+---------+----------+
  | Hello | from | the     | terminal |
  | this  | is   | another | row      |
  +-------+------+---------+----------+

Advanced Usage with Subcommands

To use the more advanced features, see the official documentation

Example

For a complete example, take a look at Slim - a cherry-picking tool