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

How do you make a program search as you type?

Started by CCJJSax, 11 March 2013 - 07:14 AM
CCJJSax #1
Posted 11 March 2013 - 08:14 AM
I'm making a program that acts as an advanced thauminomicon (thaumcraft item). I am trying to make it so you type what aspect you need and it will show you the best suited item, ranked by rarity, stackability, and excess aspects.

I want it to work similarly to Google's auto search where it searches as you type. My idea was to use an os.pullEvent("char") and have then have it display what you type, but I can't figure out how to display what button you push (though I swear I've done it before, blonde moment I guess). any pointers?
JokerRH #2
Posted 11 March 2013 - 09:07 AM
os.pullEvent will return the charackter you clicked, As you already figured out.
What you may want to do next is concat them to a string.
The last step would be to check if your string matches with anything stored in a table.
So it should look like this:


function search()
  tab = {"Fire", "Earth", "Permutatio"}
  text = ""

  while true do
	local event, p1 = os.pullEvent()

	if event == "char" then
	  text = text..p1
	elseif event == "key" and p1 == keys.backspace then
	  text = string.sub(text, 1, #text - 1)
	elseif event == "key" and p1 == keys.enter then
	  return results
	end

	local results = {}

	for ind, param in pairs(tab) do
	  if string.find(string.upper(param), string.upper(text)) then
		table.insert(results, param)
	  end
	end

	term.clear()

	term.setTextColor(colors.white)
	term.setCursorPos(1, 1)
	print(text)

	term.setTextColor(colors.green)
	for ind, param in pairs(results) do
	  print(param)
	end
  end
end

This should print out any results it finds.
This function will return results (that is the table I defined in the programm). So I think you can do the ranking yourself, shouldn't be that hard :D/>

Edit: Before anyone wonders about the typo CCJJSax talked about, I modified this post a little bit before I knew that he already responsed to it…
CCJJSax #3
Posted 11 March 2013 - 09:11 AM
os.pullEvent will return the charackter you clicked, As you already figured out.
What you may want to do next is concat them to a string.
The last step would be to check if your string matches with anything stored in a table.
So it should look like this:


tab = {"Fire", "Earth", "Permutatio"}
results = {}
local text = ""
while true do
  local event, char = os.pullEvent("char")
  text = text..char

  for ind, param in pairs(tab) do
	if string.find(string.upper(param), string.upper(text)) then
	  table.insert(results, param)
	end
  end

  term.clear()

  term.setTextColor(colors.white)
  term.setCursorPos(1, 1)
  print(text)

  term.setTextColor(colors.green)
  for ind, param in pairs(results) do
	print(param)
  end
end

This should print out any results it finds.

other than the mistype of string on line 21, it works great! :D/> thanks!