Xmann1

term.lua

Apr 22nd, 2025 (edited)
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. local chest = peripheral.wrap("front")
  2. local query = nil
  3.  
  4. rednet.open("bottom")
  5.  
  6. function io()
  7.   if query == nil then
  8.     return
  9.   end
  10.  
  11.   -- Pipe matching items towards player
  12.   local chest_items = chest.list()
  13.   if chest_items ~= nil then
  14.     for slot, item in pairs(chest_items) do
  15.       if string.find(item.name, query) then
  16.         turtle.select(1)
  17.         turtle.suck()
  18.       end
  19.  
  20.       break
  21.     end
  22.   end
  23. end
  24.  
  25. function empty_garbage()
  26.   -- Pipe non-matching items away from player
  27.   for i=1, 16 do
  28.     turtle.select(i)
  29.  
  30.     itemDetail = turtle.getItemDetail()
  31.  
  32.     if query == nil or (itemDetail ~= nil and not string.find(itemDetail.name, query)) then
  33.       turtle.drop()
  34.     end
  35.   end
  36.  
  37.   turtle.select(1)
  38. end
  39.  
  40. local query_input = ""
  41.  
  42. local width, height = term.getSize()
  43.  
  44. while true do
  45.   term.setCursorPos(1, height)
  46.   term.clearLine()
  47.   term.write("> " .. query_input)
  48.  
  49.   os.startTimer(1)
  50.  
  51.   local eventData = { os.pullEvent() }
  52.   local event = eventData[1]
  53.  
  54.   if event == "key" then
  55.     local key = eventData[2]
  56.  
  57.     if key == 257 then
  58.       if string.len(query_input) > 0 then
  59.         query = query_input
  60.  
  61.         rednet.broadcast(query)
  62.  
  63.         empty_garbage()
  64.         query_input = ""
  65.       end
  66.     elseif key == 259 then
  67.       query_input = string.sub(query_input, 1, -2)
  68.     elseif key >= 39 and key <= 126 then
  69.       query_input = query_input .. string.char(key)
  70.     end
  71.   end
  72.  
  73.   io();
  74. end
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment