Advertisement
Ganeesya

peripheral Checker

Jul 4th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.55 KB | None | 0 0
  1.  
  2. local LINE_MAX = 10
  3.  
  4. function needArgFunc(target)
  5.     if target.func then
  6.         return true
  7.     end
  8.     return false
  9. end
  10.  
  11. function canOpen(target)
  12.     if type(target) == "table" then
  13.         return true
  14.     end
  15.     if type(target) == "function" then
  16.         local suc,ft = pcall(target)
  17.         return canOpen(ft)
  18.     end
  19.     return false
  20. end
  21.  
  22. function isCutingString(data)
  23.     if type(data) == "string" and string.len(data) > 10 then
  24.         return true
  25.     end
  26.     return false
  27. end
  28.  
  29. function open(target)
  30.     if type(target) == "table" then
  31.         local op = {}
  32.         for k, v in pairs(target) do
  33.             local additem = {name = k,data = v}
  34.             if type(v) == "function" then
  35.                 local suc,data = pcall(v)
  36.                 additem["data"] = data
  37.                 additem["func"] = v
  38.                 additem["suc"] = suc
  39.             end
  40.             op[#op+1] = additem
  41.         end
  42.         return op
  43.     end
  44.     if type(target) == "function" then
  45.         local suc,ft = pcall(target)
  46.         return open(ft)
  47.     end
  48.     return {}
  49. end
  50.  
  51. function slidePos(nx)
  52.     local x,y = term.getCursorPos()
  53.     term.setCursorPos(nx, y)
  54. end
  55.  
  56. function writeClrMax(str,clr,max)
  57.     writeClr(string.sub(str,1,max),clr)
  58.     if string.len(str) > max then
  59.         writeClr("~",colors.orange)
  60.     end
  61. end
  62.  
  63. function writeClr(str,clr)
  64.     term.setTextColor(clr)
  65.     write(str)
  66.     term.setTextColor(colors.white)
  67. end
  68.  
  69. function viewFolders(folders)
  70.     if #folders - 1 > 0 then
  71.         writeClr(folders[#folders-1].name.." > ",colours.grey)
  72.     end
  73.     writeClr(folders[#folders].name.."\n",colors.white)
  74. end
  75.  
  76. function writeItemData(data)
  77.     if data and type(data) == "table" then
  78.         return writeClr("{}",colors.yellow)
  79.     end
  80.     return writeClrMax(tostring(data),colors.white,10)
  81. end
  82.  
  83. function writeFucnData(item)
  84.     if item.func and not item.suc then
  85.         writeClr("func error",colors.red)
  86.     else
  87.         writeItemData(item.data)
  88.     end
  89. end
  90.  
  91. function writeItem(item)
  92.     if item.func then
  93.         writeClr(item.name.."() = ",colors.pink)
  94.         writeFucnData(item)
  95.         write("\n")
  96.         return
  97.     end
  98.     if type(item.data) == "table" then
  99.         writeClr(item.name.." {}\n",colors.yellow)
  100.         return
  101.     end
  102.     writeClr(item.name.." = ",colors.white)
  103.     writeClrMax(tostring(item.data),colors.white,10)
  104.     write("\n")
  105. end
  106.  
  107. function viewItems(list,cursol,slide)
  108.     if not list then write("not datas") end
  109.     for i, v in ipairs(list) do
  110.         if i - slide > 0 and i - slide <= LINE_MAX then
  111.             if i == cursol then
  112.                 write(">")
  113.             end
  114.             slidePos(3)
  115.             writeItem(v)
  116.         end
  117.     end
  118. end
  119.  
  120. function initRoot()
  121.     local re = {}
  122.  
  123.     for i, v in ipairs(peripheral.getNames()) do
  124.         local newitem = {}
  125.         re[#re+1] = {name = v,data = peripheral.wrap(v)}
  126.     end
  127.  
  128.     return re
  129. end
  130.  
  131. function viewArgs(Arguments,cursol)
  132.     for i, v in ipairs(Arguments) do
  133.         if i == cursol then
  134.             write(">")
  135.         end
  136.         slidePos(3)
  137.         writeClr("arg"..i,colors.white)
  138.         slidePos(8)
  139.         writeItemData(v.arg)
  140.         write("\n")
  141.     end
  142.     if (#Arguments + 1) == cursol then
  143.         write(">")
  144.     end
  145.     slidePos(3)
  146.     writeClr("ADD ARGUMENT\n",colors.lime)
  147. end
  148.  
  149. function initArgus(target,folders)
  150.     return {}
  151. end
  152.  
  153. function SetArgumentMode(target,folders)
  154.     local arguments = initArgus()
  155.     local cursol = 1
  156.     while true do
  157.         ----------------------------------
  158.         term.clear()
  159.         term.setCursorPos(1, 1)
  160.         writeClr("SET ARGUMENTS MODE"..cursol,colors.purple)
  161.         viewFolders(folders)
  162.         writeClr(target.name.."() = ",colors.pink)
  163.         writeItemData(target.data)
  164.         write("\n")
  165.         viewArgs(arguments,cursol)
  166.         ----------------------------------
  167.  
  168.         local eve = {os.pullEvent()}
  169.         if eve[1] == "key" then
  170.             if eve[2] == 208 then -- up key
  171.                 cursol = math.min(cursol+1,#arguments+1)
  172.             end
  173.             if eve[2] == 200 then -- down key
  174.                 cursol = math.max(cursol-1,1)
  175.             end    
  176.             if eve[2] == 205 then -- right key
  177.             end
  178.             if eve[2] == 203 then -- left key
  179.                 return
  180.             end    
  181.             if eve[2] == 57 or eve[2] == 28 then -- space key
  182.                 if cursol == #arguments + 1 then
  183.                     arguments[#arguments + 1] = {}
  184.                 else
  185.                     term.setCursorPos(8,cursol+2)
  186.                     write("            ")
  187.                     term.setCursorPos(8,cursol+2)
  188.                     arguments[cursol].arg=read()
  189.                     local suc,rt = pcall(target.func,unpack(arguments))
  190.                     target.data = rt
  191.                     target.suc = suc
  192.                 end
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. local folders = {}
  199. folders[1]= {name="root",folder=initRoot()}
  200. local cursol = 1
  201. local slide = 0
  202.  
  203. while true do
  204.     -------------------------------------
  205.     term.clear()
  206.     term.setCursorPos(1, 1)
  207.     --write("Dc="..cursol.."/s="..slide)
  208.     viewFolders(folders)
  209.     viewItems(folders[#folders].folder,cursol,slide)
  210.     -------------------------------------
  211.  
  212.     local nowSelectItem = folders[#folders].folder[cursol]
  213.  
  214.     local eve = {os.pullEvent()}
  215.     if eve[1] == "key" then
  216.         if eve[2] == 208 then -- up key
  217.             cursol = math.min(cursol + 1,#folders[#folders].folder)
  218.             slide = math.max(slide,cursol-LINE_MAX)
  219.         end
  220.         if eve[2] == 200 then -- down key
  221.             cursol = math.max(cursol - 1,1)
  222.             slide = math.min(slide,cursol - 1)
  223.         end    
  224.         if eve[2] == 205 then -- right key
  225.             if isCutingString( nowSelectItem.data ) then
  226.                 writeClr( nowSelectItem.data,colors.green )
  227.                 os.pullEvent("key")
  228.             end
  229.         end
  230.         if eve[2] == 203 then -- left key
  231.             if folders[#folders].name ~= "root" then
  232.                 table.remove(folders,#folders)
  233.                 cursol = 1
  234.                 slide = 0
  235.             end
  236.         end    
  237.         if eve[2] == 30 then ---a
  238.             if needArgFunc( nowSelectItem ) then
  239.                 SetArgumentMode(nowSelectItem,folders)
  240.             end
  241.         end
  242.         if eve[2] == 57 or eve[2] == 28 then -- space key
  243.             if canOpen( nowSelectItem.data ) then
  244.                 local newFld = {}
  245.                 newFld["name"]   = nowSelectItem.name
  246.                 newFld["folder"] = open( nowSelectItem.data )
  247.                 folders[#folders+1] = newFld
  248.                 cursol = 1
  249.                 slide = 0
  250.             end
  251.         end
  252.     end
  253. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement