Advertisement
BigSHinyToys

TUI method

Jun 1st, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --[[
  2.         method decovery
  3.         by BigSHinyToys
  4. ]]--
  5.  
  6. local tab = 1
  7. local tSide = rs.getSides()
  8. local width,hight = term.getSize()
  9. local space = math.floor(width/6)
  10. local methods = {}
  11.  
  12. local function clear()
  13.     term.setBackgroundColor(colors.black)
  14.     term.setTextColor(colors.white)
  15.     term.clear()
  16.     term.setCursorBlink(false)
  17.     term.setCursorPos(1,1)
  18. end
  19.  
  20. local function clearL(y)
  21.     term.setCursorPos(1,y)
  22.     term.clearLine()
  23. end
  24.  
  25. local function clearBox(backCol)
  26.     term.setBackgroundColor(colors[backCol])
  27.     for i = 2,hight do
  28.         clearL(i)
  29.     end
  30. end
  31.  
  32. local function setCol(textCol,backCol)
  33.     term.setTextColor(colors[textCol])
  34.     term.setBackgroundColor(colors[backCol])
  35. end
  36.  
  37. local function printC(posX,posY,textCol,backCol,text)
  38.     term.setCursorPos(posX,posY)
  39.     term.setTextColor(colors[textCol])
  40.     term.setBackgroundColor(colors[backCol])
  41.     term.write(text)
  42. end
  43.  
  44. local function drawTabs()
  45.     for i = 1,6 do
  46.         if i == tab then
  47.             printC((space*(i-1))+1,1,"green","lime","["..tSide[i]..string.rep(" ",space-#tSide[i]-2).."]")
  48.         else
  49.             printC((space*(i-1))+1,1,"blue","lightBlue","["..tSide[i]..string.rep(" ",space-#tSide[i]-2).."]")
  50.         end
  51.     end
  52.     printC(width,1,"white","red","X")
  53. end
  54.  
  55. local function drawData()
  56.     if peripheral.isPresent(tSide[tab]) then
  57.         local pos = 3
  58.         printC(1,2,"green","lime","Peripheral : "..peripheral.getType(tSide[tab]))
  59.         methods = peripheral.getMethods(tSide[tab])
  60.         for i,v in pairs(methods) do
  61.             printC(1,pos,"green","lime",tostring(i).." "..tostring(v))
  62.             pos = pos + 1
  63.         end
  64.     else
  65.         printC(1,2,"green","lime","Peripheral : NIL")
  66.         methods = {}
  67.     end
  68. end
  69.  
  70. clear()
  71. local lastTab
  72.  
  73. local function draw()
  74.     clearBox("lime")
  75.     drawTabs()
  76.     drawData()
  77.     lastTab = tab
  78. end
  79.  
  80. while true do
  81.     if lastTab ~= tab then
  82.         draw()
  83.     end
  84.     local event = {os.pullEvent()}
  85.     if event[1] == "mouse_click" then
  86.         if event[2] == 1 then -- left click
  87.             if event[4] == 1 then -- first line
  88.                 for i = 1,6 do
  89.                     if event[3] > space*(i-1) and event[3] < (space*(i))+1 then
  90.                         tab = i
  91.                     end
  92.                 end
  93.                 if event[3] == width then
  94.                     break
  95.                 end
  96.             elseif methods[event[4] - 2] and type(methods[event[4] - 2]) == "string" and event[3] < string.len(methods[event[4]-2])+string.len(tostring(event[4] - 2))+1 then
  97.                 draw()
  98.                 local test = {pcall(peripheral.call,tSide[tab],methods[event[4] - 2])}
  99.                 term.setCursorPos(1,hight - 1)
  100.                 setCol("red","lightBlue")
  101.                 write(tostring(methods[event[4] - 2]).." ")
  102.                 for i = 1,#test do
  103.                     write(tostring(test[i]).." ")
  104.                 end
  105.             end
  106.         end
  107.     elseif event[1] == "peripheral" or event[1] == "peripheral_detach" then
  108.         lastTab = nil
  109.     end
  110. end
  111. clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement