Advertisement
MeXaN1cK

OS

Aug 24th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.64 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. local shell = require("shell")
  15. ----
  16. local oldBack=gpu.getBackground()
  17. local oldFore=gpu.getForeground()
  18. local pageDisk = 0
  19. sel=0
  20. ----
  21. function ob()
  22. while true do
  23. a,b,c,d,e,f = event.pull("modem_message")
  24. if unicode.sub(f,0,3)=="ls:" then
  25.  path = unicode.sub(f,4,unicode.len(f))
  26.  pathArr=serialization.unserialize(path)
  27.  sel=0
  28.  oldClickFile=nil
  29.  clickFile=0
  30.  
  31.  drawFiles(1,72,getPath())
  32.  tunnel.send(serialization.serialize(getFiles(getPath())))
  33.  elseif unicode.sub(f,0,4)=="run:" then
  34.   prog=unicode.sub(f,5,unicode.len(f))
  35.   shell.execute(prog)
  36.  elseif unicode.sub(f,0,3)=="cls" then
  37.   term.clear()
  38.  end
  39. os.sleep(0.00001)
  40. end
  41. end
  42. function exit()
  43. gpu.setBackground(oldBack)
  44. gpu.setForeground(oldFore)
  45. term.clear()
  46. os.exit()
  47. end
  48. function checkEvent()
  49.   key,_,x,y,clickMouse,name=event.pull()
  50.   return key , x , y , clickMouse  
  51. end
  52. function getPath()
  53.   path=''
  54.   for i=1,#pathArr do
  55.     path=path..pathArr[i]
  56.   end
  57.   return path
  58. end
  59.  
  60. function drawMenu()
  61.  draw.field(1,1,80,25,' ',0x222222,0xFFFFFF) -- <frame>
  62.  draw.field(1,2,18,24,' ',0xAAAAAA,0xFFFFFF)
  63.  draw.field(19,2,62,24,' ',0xFFFFFF,0xFFFFFF)
  64.  draw.field(39,2,1,24,"¦",0xFFFFFF,0x00FF00)
  65.  draw.field(60,2,1,24,"¦",0xFFFFFF,0x00FF00)
  66.  draw.field(3,1,3,1,' ',0x0000FF,0xFFFFFF)
  67.  draw.field(4,1,1,1,'<',0x0000FF,0xFFFFFF)
  68.  draw.field(8,1,3,1,' ',0x0000FF,0xFFFFFF)
  69.  draw.field(9,1,1,1,'>',0x0000FF,0xFFFFFF)
  70.  draw.field(1,1,1,1,'X',0xFF0000,0xFFFFFF)
  71. end
  72. function getFiles(path)
  73.   arr={} arr2={}
  74.   for val,val2 in filesystem.list(path) do if val~= nil then arr[#arr+1]=val end end
  75.   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
  76.   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
  77.   arr=arr2
  78.   return arr
  79. end
  80. function drawName(x,arr,i,deb)
  81.   j=i-deb
  82.   if unicode.sub(arr[i],unicode.len(arr[i]),unicode.len(arr[i])) == '/' then
  83.     gpu.setForeground(0xBBBB00)
  84.   gpu.set(x,1+j,arr[i])
  85.   else
  86.     gpu.setForeground(0)
  87.     gpu.set(x,1+j,arr[i])
  88.   end
  89.   gpu.setForeground(0)
  90.   gpu.setBackground(0xFFFFFF)
  91. end
  92.  
  93. function drawSelect(x,arr,sel,deb)
  94.   j=sel-deb
  95.   if oldClickFile ~= nil and oldSelect ~= nil then
  96.   l=oldSelect[3]-oldSelect[4]
  97.   if oldClickFile ~= sel and arr[oldClickFile]~= nil then
  98.     if unicode.sub(arr[oldClickFile],unicode.len(arr[oldClickFile]),unicode.len(arr[oldClickFile])) == '/' then
  99.       gpu.setForeground(0xBBBB00)
  100.       gpu.setBackground(0xFFFFFF)
  101.       gpu.fill(oldSelect[1],1+l,20,1,' ')
  102.       gpu.set(oldSelect[1],1+l,arr[oldClickFile])
  103.     else
  104.       gpu.setBackground(0xFFFFFF)
  105.       gpu.fill(oldSelect[1],1+l,20,1,' ')
  106.       gpu.setForeground(0)
  107.       gpu.set(oldSelect[1],1+l,arr[oldClickFile])
  108.     end
  109.   end
  110.   end
  111.   if unicode.sub(arr[sel],unicode.len(arr[sel]),unicode.len(arr[sel])) == '/' then
  112.     gpu.setForeground(0xBBBB00)
  113.   gpu.setBackground(0x55AAEE)
  114.   gpu.fill(x,1+j,20,1,' ')
  115.   gpu.set(x,1+j,arr[sel])
  116.   else
  117.     gpu.setBackground(0x55AAEE)
  118.   gpu.fill(x,1+j,20,1,' ')
  119.     gpu.setForeground(0)
  120.     gpu.set(x,1+j,arr[sel])
  121.   end
  122.   gpu.setForeground(0)
  123.   gpu.setBackground(0xFFFFFF)
  124.   oldSelect={x,arr,sel,deb}
  125. end
  126.  
  127. function getMyDisks(mode)
  128. local disks={}
  129. local fromAddress = filesystem.get(os.getenv("_")).address
  130. local candidates = {}
  131. for address in cmp.list("filesystem", true) do
  132.   local dev = cmp.proxy(address)
  133.   if --[[not dev.isReadOnly() and]] dev.address ~= computer.tmpAddress() then
  134.     table.insert(candidates, dev)
  135.   end
  136. end
  137. if mode==2 then
  138. for i = 1, #candidates do
  139.   local label = candidates[i].getLabel()
  140.   if label and label ~= 'raid' then
  141.     label = label
  142.   else
  143.     label = candidates[i].address
  144.   end
  145.   disks[#disks+1]={label,candidates[i].address}
  146.   end
  147.   return disks
  148. elseif mode == 1 then
  149. for i = 1, #candidates do
  150.   local label = candidates[i].getLabel()
  151.   if label and label ~= 'raid' then
  152.     label = label
  153.   else
  154.     label = candidates[i].address
  155.   end
  156.   disks[#disks+1]=label
  157.   end
  158.   return disks
  159.   end
  160. end
  161. function drawDisks()
  162.   local disks=getMyDisks(1)
  163.   draw.fileList(0xAAAAAA,0,1,2,18,6,'[Filesystem]',disks,1,disks[posDisk],pageDisk,0xFF00FF,0x55AAEE)
  164. end
  165.  
  166. function drawFiles(k,v,path)
  167.   drawPath()
  168.   draw.field(19,2,62,24,' ',0xFFFFFF,0xFFFFFF)
  169.   draw.field(39,2,1,24,"¦",0xFFFFFF,0x00FF00)
  170.   draw.field(60,2,1,24,"¦",0xFFFFFF,0x00FF00)
  171.   arr=getFiles(path)
  172.   for i=k,v do
  173.     if i < 25 then  
  174.       if arr[i] ~= nil then
  175.       drawName(19,arr,i,0)
  176.       end    
  177.     elseif i >24 and i < 49 then
  178.       if arr[i] ~= nil then
  179.       drawName(40,arr,i,24)
  180.       end
  181.     elseif i > 48 and i < 73 then  
  182.       if arr[i] ~= nil then
  183.       drawName(61,arr,i,48)
  184.       end
  185.     end
  186.   end
  187.   gpu.setForeground(0)
  188.   gpu.setBackground(0xFFFFFF)
  189. end
  190. --55AAEE
  191. function run(f)
  192. shell.execute(tostring(f))
  193. end
  194.  
  195. function delete()
  196.   draw.field(20,10,40,5,' ',0xAAAAAA,0xFFFFFF)
  197.   draw.clound('┌','┐','└','┘','│','─',20,10,40,5,0xAAAAAA,0)
  198.   draw.text(21,12,38,'Вы точно хотите удалить файл?[Y/n]: ',0xAAAAAA,0)
  199.   while not choice do
  200.     _,_,_,per = event.pull('key_up')
  201.   if per == 21 then
  202.     shell.execute('del '..getPath()..arr[clickFile])
  203.     sel=0
  204.       oldClickFile=nil
  205.       clickFile=0
  206.     drawMenu()
  207.       drawPath()
  208.       drawDisks()
  209.     drawFiles(1,72,getPath())
  210.     choice=per
  211.   elseif per == 49 then
  212.     drawMenu()
  213.     drawPath()
  214.       drawDisks()
  215.     drawFiles(1,72,getPath())
  216.     choice=per
  217.   end
  218.   end
  219.   choice=nil
  220. end
  221. function drawPath()
  222.   draw.field(20,1,40,1,' ',0x222222,0xFFFFFF)
  223.   path=getPath()
  224.   gpu.setForeground(0xFFFFFF)
  225.   gpu.setBackground(0x222222)
  226.   if unicode.len(path) > 40 then
  227.     path2=unicode.sub(path,unicode.len(path)-40,unicode.len(path))
  228.   else
  229.     path2=path
  230.   end
  231.   gpu.set(20,1,path2)
  232.   gpu.setForeground(0)
  233.   gpu.setBackground(0xFFFFFF)
  234. end
  235.  
  236. function startDisk()
  237.   local disks=getMyDisks(2)
  238.   arrMyDiskAddress=filesystem.get(os.getenv("_"))
  239.   for i=1,#disks do
  240.   if arrMyDiskAddress.address:sub(1,8) == disks[i][1]:sub(1,8) or arrMyDiskAddress.getLabel():sub(1,8) == disks[i][1]:sub(1,8) then
  241.     posDisk=i
  242.   break
  243.   end
  244.   end
  245.   return 'mnt/'..tostring(arrMyDiskAddress.address:sub(1,3))..'/' , posDisk
  246. end
  247.  
  248. function enter(x,y,w,h,color_back,color_fore)
  249.   got=nil
  250.   function drawTextInPass(x,y,w,h,txt,col)
  251.     if unicode.len(txt) > w-3 then
  252.       txt=unicode.sub(txt,unicode.len(txt)-(w-3),unicode.len(txt))
  253.     end
  254.     draw.field(x+1,y+1,w-2,1,' ',color_back,color_fore)
  255.     oldB=gpu.getBackground()
  256.     oldF=gpu.getForeground()
  257.  gpu.setBackground(color_back)
  258.   gpu.setForeground(col)
  259.   gpu.set(x+1,y+1,txt)
  260.   gpu.setBackground(oldB)
  261.   gpu.setForeground(oldF)
  262.   end
  263.   draw.field(x,y,w,h,' ',color_back,color_fore)
  264.   draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,0xAA0000)
  265.   local function getKey(s)
  266.     local key, _, x1 , code, _ = event.pull()
  267.   if key == 'key_up' or key == 'touch' then
  268.     return code , x1 , key
  269.   end
  270.   end
  271.   local function enterPass(x,y,w,h)
  272.   try = ""
  273.   ex=true
  274.   while ex do
  275.     local err, f , x1 , key  = pcall(getKey)
  276.     if not err then
  277.       return err, try
  278.     end
  279.   if f ~= nil and key == 'key_up' then --
  280.     if keyboard.keys[f] == 'grave' then ex=false end
  281.     if f < 60 and f > 0 then
  282.       if keyboard.keys[f] == 'enter' then
  283.             draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  284.             return err, try , 's'
  285.     end
  286.       if keyboard.keys[f] == 'back' then
  287.         if unicode.len(try) > 0 then
  288.           try=unicode.sub(try,0,unicode.len(try)-1)
  289.       drawTextInPass(x,y,w,h,try,color_fore)
  290.     else
  291.         try = 'cancel'
  292.       draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  293.       return err , try , 'f'
  294.         end
  295.       end
  296.       if unicode.len(keyboard.keys[f]) == 1 then
  297.         try = try..keyboard.keys[f]
  298.       drawTextInPass(x,y,w,h,try,color_fore)
  299.     elseif keyboard.keys[f] == 'space' then
  300.       try = try..'_'
  301. drawTextInPass(x,y,w,h,try,color_fore)
  302.     elseif keyboard.keys[f] == 'minus' then
  303.       try = try..'-'
  304. drawTextInPass(x,y,w,h,try,color_fore)
  305.     elseif keyboard.keys[f] == 'period' then
  306.     try = try..'.'
  307. drawTextInPass(x,y,w,h,try,color_fore)
  308.   elseif keyboard.keys[f] == 'comma' then
  309.     try = try..','
  310. drawTextInPass(x,y,w,h,try,color_fore)
  311.   elseif keyboard.keys[f] == 'slash' then
  312.     try = try..'/'
  313. drawTextInPass(x,y,w,h,try,color_fore)
  314.       end
  315.   end
  316.   elseif key == 'touch' and f ~= nil then
  317.       if x1 > x-1 and x1 < x+w and f > y-1 and f < y+h then
  318.       --
  319.       else
  320.           draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  321.           return err, try , 's'
  322.       end
  323.     end
  324.     end
  325.   end --
  326.   while true do
  327.     err, try , got = enterPass(x,y,w,h)
  328.     if got == 's' then -- success
  329.       break
  330.   elseif got == 'f' then
  331.     try = nil
  332.     break
  333.     end
  334.   end
  335.   return try
  336. end
  337.  
  338. local function drawButtonAddMenu(arr,i)
  339.     draw.field(arr[i][1],arr[i][2],arr[i][3],1,'▄',arr[i][4],arr[i][5])
  340.     draw.field(arr[i][1],arr[i][2]+1,arr[i][3],1,' ',arr[i][5],arr[i][5])
  341.     draw.field(arr[i][1],arr[i][2]+2,arr[i][3],1,'▄',arr[i][5],arr[i][4])
  342. end
  343.  
  344. function drawFrame(txt)
  345.   arrDrawFrame={
  346.   {25,12,12,0xAAAAAA,0xBBBB00,0,'Отмена'},
  347.   {43,12,12,0xAAAAAA,0xBBBB00,0,'Готово'}
  348.   }
  349.   draw.field(20,8,40,8,' ',0xAAAAAA,0xFFFFFF) -- frame
  350.   draw.clound('┌','┐','└','┘','│','─',20,8,40,8,0xAAAAAA,0)
  351.   draw.text(20,8,38,txt,0xAAAAAA,0) -- /frame
  352.   draw.field(25,9,30,3,' ',0xAAAAAA,0xFFFFFF) -- nameFrame
  353.   draw.clound('┌','┐','└','┘','│','─',25,9,30,3,0xAAAAAA,0) -- nameFrame
  354.   for i=1,#arrDrawFrame do
  355.       drawButtonAddMenu(arrDrawFrame,i)
  356.     draw.text(arrDrawFrame[i][1],arrDrawFrame[i][2]+1,arrDrawFrame[i][3],arrDrawFrame[i][7],arrDrawFrame[i][5],arrDrawFrame[i][6])
  357.   end
  358.   while true do
  359.     local key,_,x,y,_,_ = event.pull('touch')
  360.     if x > 20 and x < 40 and y >8 and y < 12 then
  361.     name=enter(25,9,30,3,0xAAAAAA,0)
  362.   elseif x > 24 and x < 36 and y > 12 and y < 16 then
  363.     name = nil
  364.     break
  365.   elseif x > 42 and x < 55 and y > 12 and y < 16 then
  366.     if name ~= nil and unicode.len(name) > 0 then
  367.       break
  368.       end
  369.   end
  370.   end
  371.   print(name)
  372.   return name
  373. end
  374.  
  375. function renameFile()
  376.   oldName=getPath()..tostring(arr[clickFile])
  377.   nn=drawFrame('[Переименование]')
  378.   if nn ~= nil then
  379.     newName=getPath()..nn
  380.   end
  381.   n=1
  382.   if oldName ~= newName and nn~=nil then
  383.   while true do
  384.     if filesystem.exists(getPath()..newName) == false then
  385.     filesystem.rename(oldName,newName)
  386.     break
  387.   elseif filesystem.exists(getPath()..newName..'('..n..')') == false and filesystem.exists(getPath()..newName) == true then
  388.     filesystem.rename(oldName,newName..'('..n..')')
  389.     break
  390.   end
  391.   n=n+1
  392.   end
  393. end
  394. end
  395.  
  396. function copyFile()
  397.   print(copyFilePath,copyF)
  398.   if arr[clickFile] ~= nil then
  399.     copyFilePath=getPath()
  400.   copyF=tostring(arr[clickFile])
  401.   end
  402. end
  403.  
  404. function pasteFile()
  405.   pasteFilePath=getPath()
  406.   n=1
  407.   if copyFilePath ~= pasteFilePatha and copyF ~= nil then
  408.   while true do
  409.     if filesystem.exists(pasteFilePath..copyF) == false then
  410.     shell.execute('cp '..copyFilePath..copyF..' '..pasteFilePath..copyF)
  411.     break
  412.   elseif filesystem.exists(getPath()..copyF..'('..n..')') == false and filesystem.exists(pasteFilePath..copyF) == true then
  413.     shell.execute('cp '..copyFilePath..copyF..' '..pasteFilePath..copyF..'('..n..')')
  414.     break
  415.   end
  416.   n=n+1
  417.   end
  418. end
  419. end
  420.  
  421. function addFile()
  422.   newFileName=drawFrame('[Создание файла/директории]')
  423.   if newFileName ~= nil then
  424.     if unicode.sub(newFileName,unicode.len(newFileName),unicode.len(newFileName)) == '/' then
  425.     shell.execute('mkdir '..newFileName)
  426.   else
  427.       nf=io.open(newFileName,'w')
  428.     nf:close()
  429.   end
  430.   end
  431. end
  432.  
  433. function menu()
  434.   drawMenu()
  435.   drawDisks()
  436.   drawFiles(1,72,getPath())
  437.   while true do
  438.     key , x , y , clickMouse = checkEvent()
  439.     if key == 'touch' then
  440.       if x > 18 and x < 39 then
  441.         if arr[y-1] ~= nil then    
  442.           clickFile=y-1
  443.           N=1
  444.         end
  445.        elseif x > 39 and x < 60 then
  446.         if arr[(y-1)+24] ~= nil then    
  447.          clickFile=(y-1)+24
  448.          N=1
  449.         end
  450.       elseif x > 60 and x < 81 then
  451.         if arr[(y-1)+48] ~= nil then
  452.           clickFile=(y-1)+48
  453.            N=1
  454.         end
  455.       elseif x > 2 and x < 7 and y == 1 then
  456.         if #pathArr > 1 then
  457.           pathArr2={}
  458.           for i=1,#pathArr-1 do
  459.             pathArr2[#pathArr2+1]=pathArr[i]
  460.           end
  461.           pathArr=pathArr2
  462.           sel=0
  463.           oldClickFile=nil
  464.           clickFile=0
  465.           drawFiles(1,72,getPath())
  466.           N=1
  467.         end
  468.       elseif x>1 and x < 18 and y > 2 and y < 7 then
  469.         ldr=getMyDisks(2)
  470.     if ldr[y-2+pageDisk] ~= nil then
  471.       posDisk=y-2+pageDisk
  472.         pathArr={}
  473.           pathArr[1]=tostring('mnt/'..ldr[posDisk][2]:sub(1,3)..'/')
  474.           sel=0
  475.           oldClickFile=nil
  476.           clickFile=0
  477.           drawFiles(1,72,getPath())
  478.           drawDisks()
  479.     end
  480.       elseif x==1 and y==1 then
  481.       exit()    
  482.       end
  483.       if oldClickFile ~= clickFile and N ~= nil then
  484.         sel=clickFile
  485.         if sel < 25 then  
  486.           if arr[sel] ~= nil then
  487.             drawSelect(19,arr,sel,0)
  488.           end    
  489.         elseif sel >24 and sel < 49 then
  490.           if arr[sel] ~= nil then
  491.             drawSelect(40,arr,sel,24)
  492.           end
  493.         elseif sel > 48 and sel < 73 then  
  494.           if arr[sel] ~= nil then
  495.             drawSelect(61,arr,sel,48)
  496.           end
  497.         end
  498.       elseif oldClickFile == clickFile and N~= nil then
  499.         if arr[clickFile] ~= nil and unicode.sub(arr[clickFile],unicode.len(arr[clickFile]),unicode.len(arr[clickFile])) == '/' then
  500.           pathArr[#pathArr+1]=tostring(arr[clickFile])
  501.           sel=0
  502.           oldClickFile=nil
  503.           clickFile=0
  504.           drawFiles(1,72,getPath())
  505.         else
  506.          if clickMouse == 0 and N~= nil then
  507.            if arr[clickFile] ~= nil then
  508.              gpu.setBackground(oldBack)
  509.              gpu.setForeground(oldFore)
  510.              term.clear()
  511.              run(getPath()..arr[clickFile])
  512.            end
  513.            sel=0
  514.            oldClickFile=nil
  515.            clickFile=0
  516.            event.pull('touch')
  517.            drawMenu()
  518.            drawPath()
  519.            drawDisks()
  520.            drawFiles(1,72,getPath())
  521.         elseif clickMouse == 1 and arr[clickFile] ~= nil and N~= nil  then
  522.           gpu.setBackground(oldBack)
  523.           gpu.setForeground(oldFore)
  524.           term.clear()
  525.           shell.execute('edit '..getPath()..arr[clickFile])
  526.           sel=0
  527.           oldClickFile=nil
  528.           clickFile=0
  529.           drawMenu()
  530.           drawPath()
  531.           drawDisks()
  532.          drawFiles(1,72,getPath())
  533.         end  
  534.       end
  535.     end
  536.     oldClickFile=clickFile
  537.   elseif key == 'key_up' then
  538.     if y == 211 and clickFile ~= nil and clickFile ~= 0 then
  539.       delete()
  540.     elseif y == 19 and clickFile ~= nil and clickFile ~= 0 then
  541.     renameFile()  
  542.      sel=0
  543.           oldClickFile=nil
  544.           clickFile=0
  545.           drawMenu()
  546.           drawPath()
  547.           drawDisks()
  548.          drawFiles(1,72,getPath())    
  549.     elseif y == 46 and clickFile ~= nil and clickFile ~= 0 then
  550.       copyFile()
  551.     elseif y == 47 then
  552.     pasteFile()
  553.         sel=0
  554.           oldClickFile=nil
  555.           clickFile=0
  556.           drawMenu()
  557.           drawPath()
  558.           drawDisks()
  559.           drawFiles(1,72,getPath())
  560.     elseif y == 49 then
  561.     addFile()
  562.           drawMenu()
  563.           drawPath()
  564.           drawDisks()
  565.           drawFiles(1,72,getPath())
  566.   end
  567.   elseif key == 'scroll' then
  568.     if x>1 and x < 18 and y > 2 and y < 7 then
  569.     pageDisk=pageDisk-(clickMouse)
  570.     if pageDisk < 0 then pageDisk=0 end
  571.     gmd=getMyDisks(1)
  572.       if pageDisk > math.ceil(#gmd/4) then pageDisk = math.ceil(#gmd/4) end
  573.     drawDisks()
  574.   end
  575.   end
  576.   os.sleep(0.0001)
  577.   N=nil
  578.   end
  579. end
  580. --
  581. local pa,posDisk= startDisk()
  582. pathArr={tostring(pa)}
  583. --
  584. thread.init()
  585. thread.create(ob)
  586. menu()
  587. thread.waitForAll()
  588. --drawFrame('test')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement