Advertisement
Ganeesya

peripheral checker

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