LuaPlay/prompt.lua
2025-08-27 19:44:15 +02:00

54 lines
1.1 KiB
Lua

local function prompt_yn(question)
while true do
io.write(question .. " [y/N]: ")
local input = io.read()
if input == "y" or input == "Y" then
return true
end
io.write("Exiting...\n")
os.exit(1)
return false
end
end
-- Run a command using the OS shell and capture the stdout
-- Strips exactly one trailing newline if present, does not strip any other whitespace.
-- Asserts that the command exits cleanly
local function capture(command)
local p = io.popen(command)
local output = p:read("*a")
assert(p:close())
-- Strip exactly one trailing newline from the output, if there is one
return output:match("(.-)\n$") or output
end
local funct
local function append_list(list, other)
for _, item in ipairs(other) do
table.insert(list, item)
end
end
local function err(msg)
io.stderr:write("Error: " .. msg .. "\n")
end
local function fatal(msg)
err(msg)
os.exit(1)
end
local demolist = { 1, 2, 3 }
-- append_list(demolist, 4)
demolist[#demolist + 1] = 4
for _, value in ipairs(demolist) do
print(value)
end
print(capture("ls"))
prompt_yn("Testing")
err("Something bad happened")
fatal("Crashing...")