Advertisement
NixillUmbreon

nixmodule.lua

Dec 19th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. --====================================================================--
  2. --[ HELP                                                             ]--
  3. --====================================================================--
  4.  
  5. --[[
  6. showMenu(displayed: table or string, returns: table,
  7.   allowNil: boolean or string or nil): function
  8.   Shows a menu.
  9.  
  10.   Parameters:
  11.   - displayed: A string or table of strings. If a string is provided,
  12.     that string is displayed. If a table is provided, the strings are
  13.     displayed one per line.
  14.   - returns: A table of key-value pairs. The value should refer to a
  15.     return value, and the key is the user input choice that causes that
  16.     value to be returned. All keys should be lowercase, as user input
  17.     will be converted to lowercase as well.
  18.   - allowNil: A string or the boolean "true" that, if true, will allow
  19.     the function to return nil when an invalid choice is made, instead
  20.     of looping and requiring a valid response. If a string, it is the
  21.     message displayed when an invalid response is returned by the user.
  22.     If not provided, defaults to "Please pick a choice above!".
  23.    
  24.   Returns: The function
  25. ]]--
  26.  
  27. --====================================================================--
  28. --[ EXAMPLE CODE                                                     ]--
  29. --====================================================================--
  30.  
  31. --[[
  32. -- showMenu ------------------------------------------------------------
  33. display = {"Here's a menu!",
  34.            "1 = print Hello World",
  35.            "2 = print Goodbye",
  36.            "text = print Creative!"}
  37. values = {"Hello World", --1
  38.           "Goodbye", --2
  39.           text="Creative!",
  40.           secret="You found my secrets! o:"}
  41. print(showMenu(display, values))
  42.  
  43. -- end of examples ---------------------------------------------------]]
  44.  
  45. --====================================================================--
  46. --[ FUNCTIONS                                                        ]--
  47. --====================================================================--
  48.  
  49. local function showMenu(displayed, values, allowNil)
  50.   allowNil = allowNil or "Please pick an option above."
  51.  
  52.   if type(displayed) == "string" then
  53.     displayed = {displayed}
  54.   end
  55.  
  56.   for a = 1, #displayed do
  57.     print(displayed[a])
  58.   end
  59.  
  60.   loop = true
  61.  
  62.   while loop do
  63.     choice = term.read():lower()
  64.     result = values[choice]
  65.     if result ~= nil or allowNil == true then loop = false
  66.     else print(allowNil) end
  67.   end
  68.  
  69.   return result
  70. end
  71.  
  72. t = require("term")
  73.  
  74. --====================================================================--
  75. --[ This makes this a module                                         ]--
  76. --====================================================================--
  77.  
  78. local Module = {
  79.   showMenu = showMenu
  80. }
  81.  
  82. return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement