This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

Color support for prompt api

Started by KingofGamesYami, 05 April 2014 - 12:55 AM
KingofGamesYami #1
Posted 05 April 2014 - 02:55 AM
So, here is what I have now in my api (relevant anyway), full version can be found here
function prompt(text, ...) --#Improved read() basically, accepts 'valid' input and will keep repeating until it gets a valid answer.
local tArgs = {...}
local tValid = {}
for i = 1, #tArgs do
  tValid[tArgs[i]] = true
end
term.write(text..": ")
local input = read()
if tValid[input] or #tArgs == 0 then
  return input
else
  print("Valid Options:")
  for i = 1, #tArgs do
   print(tArgs[i])
   if i < #tArgs then
	term.write("or ")
   end
  end
  return prompt(text, ...)
end
end
function parseColor(input) --#parses input from user into color data
if (colors[input] and type(colors[input]) == "number") or (colours[input] and type(colours[input]) == "number") then
   return colors[input] or colours[input]
elseif tonumber(input) then
   return tonumber(input)
else
   return false
end
end
What I want to do is call prompt in a way as to use the parseColor to make sure they entered a color, then return that color to the program using it. It might look something like this:
local mColor = api.prompt("Monitor Color", "color")
Edited on 05 April 2014 - 12:56 AM
KingofGamesYami #2
Posted 05 April 2014 - 04:23 AM
Nevermind, figured it out. If you are wondering how:

function prompt(text, ...)
 local tArgs = {...}
 local tValid = {}
 for i = 1, #tArgs do
  tValid[tArgs[i]] = true
 end
 term.write(text..": ")
 local input = read()
 local c = parseColor(input)
 if #tArgs == 1 and tArgs[1] == "color" then
  if c then
   return c
  else
   print("Please enter a color")
   return prompt(text, ...)
  end
 elseif tValid[input] or #tArgs == 0 then
  return input
 else
  print("Valid Options:")
  for i = 1, #tArgs do
   print(tArgs[i])
   if i < #tArgs then
    term.write("or ")
   end
  end
  return prompt(text, ...)
 end
end