Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --====================================================================--
- --[ HELP ]--
- --====================================================================--
- --[[
- showMenu(displayed: table or string, returns: table,
- allowNil: boolean or string or nil): function
- Shows a menu.
- Parameters:
- - displayed: A string or table of strings. If a string is provided,
- that string is displayed. If a table is provided, the strings are
- displayed one per line.
- - returns: A table of key-value pairs. The value should refer to a
- return value, and the key is the user input choice that causes that
- value to be returned. All keys should be lowercase, as user input
- will be converted to lowercase as well.
- - allowNil: A string or the boolean "true" that, if true, will allow
- the function to return nil when an invalid choice is made, instead
- of looping and requiring a valid response. If a string, it is the
- message displayed when an invalid response is returned by the user.
- If not provided, defaults to "Please pick a choice above!".
- Returns: The function
- ]]--
- --====================================================================--
- --[ EXAMPLE CODE ]--
- --====================================================================--
- --[[
- -- showMenu ------------------------------------------------------------
- display = {"Here's a menu!",
- "1 = print Hello World",
- "2 = print Goodbye",
- "text = print Creative!"}
- values = {"Hello World", --1
- "Goodbye", --2
- text="Creative!",
- secret="You found my secrets! o:"}
- print(showMenu(display, values))
- -- end of examples ---------------------------------------------------]]
- --====================================================================--
- --[ FUNCTIONS ]--
- --====================================================================--
- local function showMenu(displayed, values, allowNil)
- allowNil = allowNil or "Please pick an option above."
- if type(displayed) == "string" then
- displayed = {displayed}
- end
- for a = 1, #displayed do
- print(displayed[a])
- end
- loop = true
- while loop do
- choice = term.read():lower()
- result = values[choice]
- if result ~= nil or allowNil == true then loop = false
- else print(allowNil) end
- end
- return result
- end
- t = require("term")
- --====================================================================--
- --[ This makes this a module ]--
- --====================================================================--
- local Module = {
- showMenu = showMenu
- }
- return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement