Advertisement
PandaDoddo72Rus

FileManager

Aug 18th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. local cmp = require('component')
  2. local event = require('event')
  3. local serialization = require('serialization')
  4. local gpu=cmp.gpu
  5. local term =require('term')
  6. local computer = require('computer')
  7. local filesystem=require('filesystem')
  8. local unicode = require('unicode')
  9. local sides = require("sides")
  10. local keyboard = require("keyboard")
  11. local thread = require('thread')
  12. local tunnel = cmp.tunnel
  13. local draw = require('draw')
  14. ----
  15. local oldBack=gpu.getBackground()
  16. local oldFore=gpu.getForeground()
  17. pathArr={'../'}
  18. sel=0
  19. ----
  20. function exit()
  21. gpu.setBackground(oldBack)
  22. gpu.setForeground(oldFore)
  23. term.clear()
  24. os.exit()
  25. end
  26. function checkEvent()
  27.   key,_,x,y,clickMouse,name=event.pull()
  28.   return key , x , y , clickMouse  
  29. end
  30. function getPath()
  31.   path=''
  32.   for i=1,#pathArr do
  33.     path=path..pathArr[i]
  34.   end
  35.   return path
  36. end
  37.  
  38. function drawMenu()
  39.  draw.field(1,1,80,25,' ',0x222222,0xFFFFFF) -- <frame>
  40.  draw.field(1,2,18,24,' ',0xAAAAAA,0xFFFFFF)
  41.  draw.field(19,2,62,24,' ',0xFFFFFF,0xFFFFFF)
  42.  draw.field(39,2,1,24,"▌",0xFFFFFF,0x00FF00)
  43.  draw.field(60,2,1,24,"▌",0xFFFFFF,0x00FF00)
  44.  draw.field(3,1,3,1,' ',0x0000FF,0xFFFFFF)
  45.  draw.field(4,1,1,1,'←',0x0000FF,0xFFFFFF)
  46.  draw.field(8,1,3,1,' ',0x0000FF,0xFFFFFF)
  47.  draw.field(9,1,1,1,'→',0x0000FF,0xFFFFFF)
  48.  draw.field(1,1,1,1,'X',0xFF0000,0xFFFFFF)
  49. end
  50. function getFiles(path)
  51.   arr={} arr2={}
  52.   for val,val2 in filesystem.list(path) do if val~= nil then arr[#arr+1]=val end end
  53.   for i=1,#arr do if unicode.sub(arr[i],unicode.len(arr[i]),unicode.len(arr[i])) == '/' then arr2[#arr2+1]=arr[i] end end
  54.   for i=1,#arr do if unicode.sub(arr[i],unicode.len(arr[i]),unicode.len(arr[i])) ~= '/' then arr2[#arr2+1]=arr[i] end end
  55.   arr=arr2
  56.   return arr
  57. end
  58. function drawName(x,arr,i,deb)
  59.   j=i-deb
  60.   if unicode.sub(arr[i],unicode.len(arr[i]),unicode.len(arr[i])) == '/' then
  61.     gpu.setForeground(0xBBBB00)
  62.   gpu.set(x,1+j,arr[i])
  63.   else
  64.     gpu.setForeground(0)
  65.     gpu.set(x,1+j,arr[i])
  66.   end
  67.   gpu.setForeground(0)
  68.   gpu.setBackground(0xFFFFFF)
  69. end
  70.  
  71. function drawSelect(x,arr,sel,deb)
  72.   j=sel-deb
  73.   if oldClickFile ~= nil then
  74.   l=oldSelect[3]-oldSelect[4]
  75.   if oldClickFile ~= sel and arr[oldClickFile]~= nil then
  76.     if unicode.sub(arr[oldClickFile],unicode.len(arr[oldClickFile]),unicode.len(arr[oldClickFile])) == '/' then
  77.       gpu.setForeground(0xBBBB00)
  78.       gpu.setBackground(0xFFFFFF)
  79.       gpu.fill(oldSelect[1],1+l,20,1,' ')
  80.       gpu.set(oldSelect[1],1+l,arr[oldClickFile])
  81.     else
  82.       gpu.setBackground(0xFFFFFF)
  83.       gpu.fill(oldSelect[1],1+l,20,1,' ')
  84.       gpu.setForeground(0)
  85.       gpu.set(oldSelect[1],1+l,arr[oldClickFile])
  86.     end
  87.   end
  88.   end
  89.   if unicode.sub(arr[sel],unicode.len(arr[sel]),unicode.len(arr[sel])) == '/' then
  90.     gpu.setForeground(0xBBBB00)
  91.   gpu.setBackground(0x55AAEE)
  92.   gpu.fill(x,1+j,20,1,' ')
  93.   gpu.set(x,1+j,arr[sel])
  94.   else
  95.     gpu.setBackground(0x55AAEE)
  96.   gpu.fill(x,1+j,20,1,' ')
  97.     gpu.setForeground(0)
  98.     gpu.set(x,1+j,arr[sel])
  99.   end
  100.   gpu.setForeground(0)
  101.   gpu.setBackground(0xFFFFFF)
  102.   oldSelect={x,arr,sel,deb}
  103. end
  104.  
  105. function drawFiles(k,v,path)
  106.   drawPath()
  107.   draw.field(19,2,62,24,' ',0xFFFFFF,0xFFFFFF)
  108.   draw.field(39,2,1,24,"▌",0xFFFFFF,0x00FF00)
  109.   draw.field(60,2,1,24,"▌",0xFFFFFF,0x00FF00)
  110.   arr=getFiles(path)
  111.   for i=k,v do
  112.     if i < 25 then  
  113.       if arr[i] ~= nil then
  114.       drawName(19,arr,i,0)
  115.       end    
  116.     elseif i >24 and i < 49 then
  117.       if arr[i] ~= nil then
  118.       drawName(40,arr,i,24)
  119.       end
  120.     elseif i > 48 and i < 73 then  
  121.       if arr[i] ~= nil then
  122.       drawName(61,arr,i,48)
  123.       end
  124.     end
  125.   end
  126.   gpu.setForeground(0)
  127.   gpu.setBackground(0xFFFFFF)
  128. end
  129. --55AAEE
  130. function drawPath()
  131.   draw.field(20,1,40,1,' ',0x222222,0xFFFFFF)
  132.   path=getPath()
  133.   gpu.setForeground(0xFFFFFF)
  134.   gpu.setBackground(0x222222)
  135.   if unicode.len(path) > 40 then
  136.     path2=unicode.sub(path,unicode.len(path)-40,unicode.len(path))
  137.   else
  138.     path2=path
  139.   end
  140.   gpu.set(20,1,path2)
  141.   gpu.setForeground(0)
  142.   gpu.setBackground(0xFFFFFF)
  143. end
  144. function menu()
  145.   drawMenu()
  146.   drawFiles(1,72,getPath())
  147.   while true do
  148.     key , x , y , clickMouse = checkEvent()
  149.   if key == 'touch' then
  150.     if x > 18 and x < 39 then
  151.         if arr[y-1] ~= nil then    
  152.         clickFile=y-1
  153.     end
  154.     elseif x > 39 and x < 60 then
  155.         if arr[(y-1)+24] ~= nil then    
  156.         clickFile=(y-1)+24
  157.     end
  158.     elseif x > 60 and x < 81 then
  159.       if arr[(y-1)+48] ~= nil then
  160.         clickFile=(y-1)+48
  161.     end
  162.     elseif x > 2 and x < 7 and y == 1 then
  163.       if #pathArr > 1 then
  164.       pathArr2={}
  165.       for i=1,#pathArr-1 do
  166.         pathArr2[#pathArr2+1]=pathArr[i]
  167.       end
  168.       pathArr=pathArr2
  169.       sel=0
  170.       oldClickFile=nil
  171.       clickFile=0
  172.         drawFiles(1,72,getPath())
  173. end
  174.     elseif x==1 and y==1 then exit()    
  175.     end
  176.     if oldClickFile ~= clickFile then
  177.       sel=clickFile
  178.         if sel < 25 then  
  179.           if arr[sel] ~= nil then
  180.       drawSelect(19,arr,sel,0)
  181.           end    
  182.         elseif sel >24 and sel < 49 then
  183.           if arr[sel] ~= nil then
  184.       drawSelect(40,arr,sel,24)
  185.           end
  186.         elseif sel > 48 and sel < 73 then  
  187.           if arr[sel] ~= nil then
  188.       drawSelect(61,arr,sel,48)
  189.           end
  190.         end
  191.     elseif oldClickFile == clickFile then
  192.       if arr[clickFile] ~= nil and unicode.sub(arr[clickFile],unicode.len(arr[clickFile]),unicode.len(arr[clickFile])) == '/' then
  193.       pathArr[#pathArr+1]=tostring(arr[clickFile])
  194.       sel=0
  195.       oldClickFile=nil
  196.       clickFile=0
  197.         drawFiles(1,72,getPath())
  198.     else
  199.       --
  200.     end
  201.     end
  202.     oldClickFile=clickFile
  203.   end
  204.   end
  205. end
  206. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement