std/prompt

Every function here works on a terminal and also when input is a file or a pipe: menus and the line editor then read whole lines, so a program that uses them can be scripted and tested.

import "std/prompt"

str name = ask("Project name", "demo")
int language = choose("Language", ["Cuarzo", "C", "Other"])
if confirm("Create it?", true) then
    run_with_spinner("Creating " + name, ["git", "init", name])
end
Function Result
ask(question, fallback) the answer, or fallback for an empty answer
ask_int(question, fallback, min, max) a whole number in a range
confirm(question, fallback) true for y or yes, false for n or no
password(question) a secret, shown as • while typed
choose(question, choices) the index chosen with the arrow keys and Enter, or -1
choose_many(question, choices) the indexes toggled with Space; a toggles all
edit_line(prompt, history) a line read with editing and history, or NONE for Ctrl+C or Ctrl+D
edit_line_with(prompt, history, words) the same, with Tab completing from words
start_spinner(message), spin(s), stop_spinner(s, line) a spinner with the time it has run
elapsed(s) the time since a spinner started, such as 1.4s
spin_while(message, process) a spinner while a process from start_process runs; returns its status
run_with_spinner(message, command) runs a command quietly behind a spinner
start_progress(label, total), advance(p, steps), finish_progress(p) a progress bar with the count and the time left
is_text_key(key) whether a key from read_key is text rather than a named key

The line editor knows the arrow keys, Home and End (or Ctrl+A and Ctrl+E), Ctrl+Left and Ctrl+Right to move by words, Backspace and Delete, Ctrl+U and Ctrl+K to clear before or after the cursor, Ctrl+W to delete a word, and Up and Down to go through history, to which it adds each line.