Created
February 3, 2025 10:22
-
-
Save SkyyySi/b0e7eb2171a00f6ad476602294d26df4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
local type = type | |
local print = print | |
local pcall = pcall | |
local select = select | |
local string = string | |
local io = io | |
local string_format = string.format | |
local io_open = io.open | |
local io_close = io.close | |
local io_read = io.read | |
local io_write = io.write | |
local io_flush = io.flush | |
local _M = {} | |
--- Print text that's formatted using `string.format()`. | |
--- | |
--- This function may fail. | |
---@param format string | |
---@param ... unknown | |
local function printf(format, ...) | |
print(string_format(format, ...)) | |
end | |
--- Check whether a given string is a valid filename, including whether the file | |
--- exists. | |
--- | |
--- This function never fails. | |
---@param filename any | |
---@return boolean success | |
---@return string? filename_or_error | |
local function validate_filename(filename) | |
local type_of_filename = type(filename) | |
if type_of_filename ~= "string" then | |
return false, string_format( | |
"bad argument #1 to 'validate_filename' (string expected, got %s)", | |
type_of_filename | |
) | |
end | |
local file, error_message = io_open(filename, "r") | |
if file == nil then | |
return false, string_format( | |
"could not open file %q in read mode: %s", | |
filename, | |
error_message or "unknown reason" | |
) | |
end | |
local success, statuscode, exitcode = io_close(file) | |
if not success then | |
return false, string_format( | |
"could not close file %q (exited with statuscode %q, exitcode %d)", | |
filename, | |
statuscode or "???", | |
exitcode or -1 | |
) | |
end | |
return true, filename | |
end | |
_M.validate_filename = validate_filename | |
--- Try to read a filename from terminal input. | |
--- | |
--- This function never fails. | |
---@return boolean success | |
---@return string? filename_or_error | |
local function try_read_filename() | |
io_write("Enter a filename > ") | |
io_flush() | |
local success, input_or_error = pcall(io_read) | |
if not success then | |
return false, input_or_error | |
end | |
if input_or_error == nil then | |
return false, "no input provided" | |
end | |
return validate_filename(input_or_error) | |
end | |
_M.try_read_filename = try_read_filename | |
--- The main entry point when running this script as a command. | |
--- | |
--- This function never fails. | |
local function main() | |
while true do | |
local success, filename_or_error = try_read_filename() | |
if success then | |
printf("Found file %q!", filename_or_error) | |
break | |
end | |
printf("Error: %s", filename_or_error) | |
end | |
end | |
if select("#", ...) == 0 then | |
main() | |
end | |
return _M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment