Advertisement
BigSHinyToys

Wire Peripheral Lister

Mar 24th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. --[[
  2.     ajustable grid -- concept test
  3.     by big SHiny Toys
  4. ]]--
  5.  
  6. local ver = "0.1aplha"
  7.  
  8. local function test(...)
  9.     term.setCursorPos(1,1)
  10.     print(...)
  11.     local event,number = os.pullEvent()
  12.     if event == "key" and number == 14 then
  13.         error()
  14.     end
  15. end
  16.  
  17. --[[
  18.         {"type","player","other","y/n","number",},
  19.         {"random type","Offline","I dont know what",true,7},
  20.         {"random type","Offline","I dont know what",true,7},
  21.         {"random type","Offline","I dont know what",true,7},
  22.         {"random type","Offline","I dont know what",true,7},
  23.         {"random type","Offline","I dont know what",true,7},
  24.         {"random type","Offline","I dont know what",true,7},
  25.         {"random type","Offline","I dont know what",true,7},
  26.         {"random type","Offline","I dont know what",true,7},
  27.         {"random type","Offline","I dont know what",true,7},
  28. ]]--
  29.  
  30. local testGrid = {
  31.     posX = 2,
  32.     posY = 2,
  33.     width = 50,
  34.     hight = 16,
  35.     selOffX = 0,
  36.     selOffY = 0,
  37.     colWidth = {6,12,10,},
  38.     data = {}
  39. }
  40.  
  41. local function getperpheralData()
  42.     local tree = {}
  43.     local data = {}
  44.     print("Welcome to netSearch")
  45.     for k,v in pairs(rs.getSides()) do
  46.         if peripheral.getType(v) == "modem" and not peripheral.call(v,"isWireless") then
  47.             tree[v] = {["type"] = "side",["status"] = "closed",["sub"] = {},}
  48.         end
  49.     end
  50.     print("Found "..#tree.." wired access points.")
  51.  
  52.     for k,v in pairs(tree) do
  53.         print(type(k),k," ",type(v))
  54.         local devices = peripheral.call(k,"getNamesRemote")
  55.         for a,b in pairs(devices) do
  56.             print(type(a),tostring(a)," ",type(b),tostring(b))
  57.             tree[k].sub[b] = peripheral.call(k,"getTypeRemote",b)
  58.         end
  59.     end
  60.  
  61.     term.clear()
  62.     term.setCursorPos(1,1)
  63.    
  64.     table.insert(data,{"Side","Name","Type"})
  65.    
  66.     for k,v in pairs(tree) do
  67.         print("--- Side "..k.."---")
  68.         for j,l in pairs(v.sub) do
  69.             table.insert(data,{k,j,l})
  70.         end
  71.     end
  72.     return data
  73. end
  74.  
  75. local function drawGrid(grid)
  76.     local sBlank = ""
  77.     for k,v in pairs(grid.colWidth) do
  78.         sBlank = sBlank..string.rep(" ",v-1).."|"
  79.     end
  80.     for i = grid.posY,grid.hight+grid.posY do
  81.         term.setCursorPos(grid.posX,i)
  82.         local sel = i - grid.posY + 1 + grid.selOffY
  83.         if grid.data[sel] then
  84.             if sel == 1 then
  85.                 term.setBackgroundColor(colors.gray)
  86.             else
  87.                 term.setBackgroundColor(colors.black)
  88.             end
  89.             for k,v in pairs(grid.data[sel]) do
  90.                 term.write(string.sub(tostring(v)..string.rep(" ",grid.colWidth[k] - 1),1,grid.colWidth[k] - 1).."|")
  91.             end
  92.         else
  93.             term.write(sBlank)
  94.         end
  95.     end
  96. end
  97.  
  98. local function manipulateGrid(grid)
  99.     local lastEvent = nil
  100.     local lastBlock = nil
  101.     while true do
  102.         term.clear()
  103.         drawGrid(grid)
  104.         local event = {os.pullEvent()}
  105.         if event[1] == "mouse_click" then
  106.             if event[2] == 1 then -- left button
  107.                 if event[4] == grid.posY - grid.selOffY then -- X line
  108.                     local total = grid.posX - 1
  109.                     for i = 1,#grid.colWidth do
  110.                         total = total + grid.colWidth[i]
  111.                         if event[3] == total then
  112.                             lastBlock = i
  113.                         end
  114.                     end
  115.                 end
  116.             end
  117.         elseif event[1] == "mouse_drag" then
  118.             if event[2] == 1 then -- left button
  119.                 if event[4] == grid.posY - grid.selOffY and lastBlock then -- X line
  120.                     local nTest = event[3] - lastEvent[3]
  121.                     if nTest == 1 or nTest == -1 then
  122.                         grid.colWidth[lastBlock] = grid.colWidth[lastBlock] + nTest
  123.                     else
  124.                         lastBlock = nil
  125.                     end
  126.                     if lastBlock and grid.colWidth[lastBlock] < 1 then
  127.                         grid.colWidth[lastBlock] = 1
  128.                         lastBlock = nil
  129.                     end
  130.                 else
  131.                     lastBlock = nil
  132.                 end
  133.             end
  134.         elseif event[1] == "mouse_scroll" then
  135.             grid.selOffY = grid.selOffY + event[2]
  136.         elseif event[1] == "key" then
  137.             if event[2] == 14 then
  138.                 return
  139.             elseif event[2] == 200 then -- up
  140.                 grid.posY = grid.posY - 1
  141.             elseif event[2] == 208 then -- down
  142.                 grid.posY = grid.posY + 1
  143.             elseif event[2] == 203 then -- left
  144.                 grid.posX = grid.posX - 1
  145.             elseif event[2] == 205 then -- right
  146.                 grid.posX = grid.posX + 1
  147.             end
  148.         elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
  149.             grid.data = getperpheralData()
  150.         end
  151.         lastEvent = event
  152.     end
  153. end
  154.  
  155. testGrid.data = getperpheralData()
  156.  
  157. manipulateGrid(testGrid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement