Advertisement
Xab

ComputerCraft User Input

Xab
Oct 23rd, 2014
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. -- Prompts user for a non-nil value of a specified type, providing a specified default value
  2. -- Returns a string, number, or boolean value
  3. function promptt(msg, expType, default)
  4.   msg = msg or ""
  5.   expType = expType or "string"
  6.   write(tostring(msg))
  7.   local px, py = term.getCursorPos()
  8.  
  9.   while true do
  10.     -- get user input
  11.     local value
  12.     if default then
  13.       write(default)
  14.       local _,key = os.pullEvent("key")
  15.       if key == keys.enter then
  16.         value = default
  17.         print()
  18.       else
  19.         -- clear default value
  20.         term.setCursorPos(px, py)
  21.         write(string.rep(" ", string.len(default)))
  22.         term.setCursorPos(px, py)
  23.         value = read()
  24.       end
  25.     else
  26.       value = read()
  27.     end
  28.    
  29.     -- format value to correct type
  30.     local strLen = string.len(value)
  31.     if expType == "number" then
  32.       value = tonumber(value)
  33.     elseif expType == "boolean" then
  34.       if string.upper(value) == "TRUE" then
  35.         value = true
  36.       elseif string.upper(value) == "FALSE" then
  37.         value = false
  38.       else
  39.         value = nil
  40.       end
  41.     end
  42.    
  43.     if value == nil or value == "" then -- if not a valid type..
  44.       -- clear user input
  45.       term.setCursorPos(px, py)
  46.       write(string.rep(" ", strLen))
  47.       term.setCursorPos(px, py)
  48.     else
  49.       return value
  50.     end
  51.   end
  52. end
  53.  
  54.  
  55. -- Prompts user for a yes/no value
  56. -- Returns true if "yes", false if "no"
  57. function promptYesNo(msg)
  58.   msg = msg or ""
  59.   msg = msg.."(y/n): "
  60.   write(msg)
  61.   local px, py = term.getCursorPos()
  62.   while true do
  63.     local value = string.upper(read())
  64.     if value == "Y" or value == "YES" then
  65.       return true
  66.     elseif value == "N" or value == "NO" then
  67.       return false
  68.     else
  69.       -- clear input
  70.       term.setCursorPos(px, py)
  71.       write(string.rep(" ", string.len(value)))
  72.       term.setCursorPos(px, py)
  73.     end
  74.   end
  75. end
  76.  
  77.  
  78. function main()
  79.   local a = promptt("Enter a string: ")
  80.   local b = promptt("Enter another string: ", "string", "ComputerCraft")
  81.   local c = promptt("Enter a number: ", "number", "123456789")
  82.   local d = promptt("Enter a boolean value: ", "boolean", "true")
  83.   local e = promptYesNo("Having fun yet?")
  84.   print(a)
  85.   print(b)
  86.   print(c)
  87.   print(d)
  88.   print(e)
  89. end
  90. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement