You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
4.4 KiB
107 lines
4.4 KiB
-- Showcase of the tui module's interactive prompts. Requires a terminal.
|
|
-- Esc returns nil from any prompt (handled below); Ctrl-C aborts the script.
|
|
--
|
|
-- Run with: cargo run -- lua/tui_prompts.lua
|
|
|
|
local function banner(s)
|
|
print("\n=== " .. s .. " ===")
|
|
end
|
|
|
|
----------------------------------------------------------------------
|
|
banner("1. confirm — yes/no")
|
|
----------------------------------------------------------------------
|
|
local go = tui.confirm("Run the whole showcase?", true, { help = "Esc anywhere skips that prompt" })
|
|
if go == nil then
|
|
print("(Esc pressed — nil means the user backed out, false means they answered no)")
|
|
elseif not go then
|
|
return
|
|
end
|
|
|
|
----------------------------------------------------------------------
|
|
banner("2. promptText — defaults, placeholder, suggestions")
|
|
----------------------------------------------------------------------
|
|
-- The positional default is pre-filled and editable in place.
|
|
local name = tui.promptText("Your name?", "ondra") or "anonymous"
|
|
|
|
-- suggestions: type to filter (case-insensitive substring), Tab completes.
|
|
-- opts.default (unlike the positional one) is only used on empty submit.
|
|
local editor = tui.promptText("Favorite editor?", nil, {
|
|
placeholder = "type to autocomplete",
|
|
suggestions = { "vim", "neovim", "emacs", "helix", "vscode", "zed" },
|
|
default = "vim",
|
|
})
|
|
|
|
-- minLen/maxLen re-prompt inline until satisfied.
|
|
local tag = tui.promptText("Release tag? (3-10 chars)", nil, { minLen = 3, maxLen = 10 })
|
|
|
|
----------------------------------------------------------------------
|
|
banner("3. promptSelect — single and multiple choice")
|
|
----------------------------------------------------------------------
|
|
-- Single: returns the chosen string; the default sets the cursor.
|
|
local lang = tui.promptSelect("Favorite language?", "lua", {
|
|
options = { "lua", "rust", "c", "python", "javascript" },
|
|
help = "type to filter the list",
|
|
})
|
|
|
|
-- Multiple: space toggles, enter submits; returns an array of strings.
|
|
local toppings = tui.promptSelect("Pizza toppings?", { "cheese" }, {
|
|
options = { "cheese", "ham", "mushrooms", "olives", "pineapple" },
|
|
multiple = true,
|
|
minSelected = 1,
|
|
pageSize = 4,
|
|
})
|
|
|
|
----------------------------------------------------------------------
|
|
banner("4. Numbers and dates")
|
|
----------------------------------------------------------------------
|
|
-- promptNumber accepts any number, promptInt only whole numbers; both
|
|
-- re-prompt inline when the input doesn't parse or is out of range.
|
|
local ratio = tui.promptNumber("Quality (0-1)?", 0.8, { min = 0, max = 1 })
|
|
local port = tui.promptInt("Port?", 8080, { min = 1, max = 65535 })
|
|
|
|
-- promptDate opens a calendar; dates are YYYY-MM-DD strings both ways.
|
|
local day = tui.promptDate("Release day?", os.date("%Y-%m-%d"), {
|
|
min = os.date("%Y-01-01"),
|
|
max = os.date("%Y-12-31"),
|
|
weekStart = "monday",
|
|
})
|
|
|
|
----------------------------------------------------------------------
|
|
banner("5. promptSecret — hidden input")
|
|
----------------------------------------------------------------------
|
|
-- No default parameter on purpose. display: "masked" (default) shows *,
|
|
-- "hidden" echoes nothing, "full" shows the text; toggle allows Ctrl-R.
|
|
local secret = tui.promptSecret("API token?", { minLen = 4, toggle = true })
|
|
|
|
----------------------------------------------------------------------
|
|
banner("6. promptLongText — opens $EDITOR")
|
|
----------------------------------------------------------------------
|
|
local notes
|
|
if tui.confirm("Open your editor for release notes?", false) then
|
|
notes = tui.promptLongText("Release notes", "## " .. (tag or "next") .. "\n\n- ", { extension = ".md" })
|
|
end
|
|
|
|
----------------------------------------------------------------------
|
|
banner("Summary")
|
|
----------------------------------------------------------------------
|
|
local function show(k, v)
|
|
if v == nil then
|
|
v = "<gray;i>(skipped)</>"
|
|
elseif type(v) == "table" then
|
|
v = table.concat(v, ", ")
|
|
end
|
|
print(tui.styled(" <info>" .. k .. "</info>: " .. tostring(v)))
|
|
end
|
|
|
|
show("name", name)
|
|
show("editor", editor)
|
|
show("tag", tag)
|
|
show("language", lang)
|
|
show("toppings", toppings)
|
|
show("quality", ratio)
|
|
show("port", port)
|
|
show("release day", day)
|
|
show("token", secret and string.rep("*", #secret))
|
|
show("notes", notes and (#notes .. " chars"))
|
|
|
|
tui.success("Showcase finished — see lua/tui_styles.lua for the output half of the module")
|
|
|