Advertisement
MeXaN1cK

g

Feb 19th, 2022
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.85 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 keyboard = require("keyboard")
  10. local shell = require("shell")
  11. local object = require('object')
  12. ----
  13. local oldBack=gpu.getBackground()
  14. local oldFore=gpu.getForeground()
  15. local rX , rY = gpu.getResolution()
  16. ----
  17. function setOld()
  18.   gpu.setBackground(oldBack)
  19.   gpu.setForeground(oldFore)
  20. end
  21.  
  22. local function getEvent(...)
  23.   args = {...}
  24.   evnt = nil
  25.   if type(args[1]) == 'number' then
  26.     time = args[1]
  27.   end  
  28.   evtrue = true
  29.   local function getRawEvent()
  30.     return {event.pull()}
  31.   end
  32.   while evtrue do
  33.    -- s , evnt = pcall(getRawEvent)
  34.   evnt = {event.pull(time)}
  35.   --  if s ~= false then
  36.     for i=1,#args do
  37.       if evnt[1] == args[i] and time == nil then
  38.         evtrue = false
  39.         break
  40.     elseif time ~= nil then  
  41.       evtrue = false
  42.         break
  43.       end
  44.     end
  45.   --end
  46.   end
  47.   return evnt
  48. end
  49. ----
  50. function getPath(arr)
  51.   path = ''
  52.   for i=1,#arr do
  53.     path = path .. arr[i]
  54.   end
  55.   return path
  56. end
  57.  
  58. local function saveTbl(tbl, fl)
  59.   file = io.open(fl, 'w')
  60.   file:write(serialization.serialize(tbl))
  61.   file:close()
  62. end
  63.  
  64. local function loadTbl(fl)
  65.   file = io.open(fl, 'r')
  66.   if not file then
  67.     file = io.open(fl, 'w')
  68.     file:write('{}')
  69.     return {} else
  70.     return serialization.unserialize(file:read('*a'))
  71.   end
  72.   file:close()
  73. end
  74.  
  75. ex = loadTbl('.expansion')
  76.  
  77. function getFiles(path)
  78.   arr={} arr2={}
  79.   for val,val2 in filesystem.list(path) do if val~= nil then arr[#arr+1]=val end end
  80.   for i=1,#arr do if filesystem.isDirectory(arr[i]) then arr2[#arr2+1]=arr[i] end end
  81.   for i=1,#arr do if filesystem.isDirectory(arr[i]) == false then arr2[#arr2+1]=arr[i] end end
  82.   arr=arr2
  83.   return arr
  84. end
  85.  
  86. function rec(path)
  87.   local colors = {'0x0AA00','0xF06306','0xAA0000'}
  88.   if filesystem.isDirectory(path) then
  89.     str = {'',0xFFFFFF}
  90.   else
  91.     N = filesystem.size(path)
  92.     if unicode.len(tostring(N)) <= 3 then
  93.       str = {tostring(N)..'  B',colors[1]}
  94.     elseif unicode.len(tostring(N)) <= 5 then
  95.       str = {tostring(round(N / 1024 , 2)) .. ' KB',colors[2]}
  96.     else
  97.       str = {tostring(round(N / 1024 / 1024,2)) .. ' MB',colors[3]}
  98.     end
  99.   end
  100.   return str
  101. end
  102.  
  103. function round(num, decimalPlaces)
  104.     local mult = 10 ^ (decimalPlaces or 0)
  105.     return math.floor(num * mult + 0.5) / mult
  106. end
  107.  
  108. function getColorFile(str,n,o)
  109.   local out = o
  110.   for i=1,#ex do
  111.     if unicode.sub(str,unicode.len(str)-unicode.len(ex[i][1])+1,unicode.len(str)) == ex[i][1] then
  112.     out = ex[i][n]
  113.   end
  114.   end
  115.   return out
  116. end
  117.  
  118. function finder(arr,str)
  119.   local out = {false}
  120.   for i = 1,#arr do
  121.     if arr[i] == str then
  122.     out = {true,i}
  123.     break
  124.   end
  125.   end
  126.   return out
  127. end
  128.  
  129. function drawOneSegClick(arr,i,color,n)
  130.   cf = rec(getPath(paths)..arr[i])
  131.   l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
  132.   gpu.setBackground(color)
  133.   gpu.fill(k+1,1+n,l,1,' ')
  134.   gpu.setForeground(getColorFile(arr[i],2,0))
  135.   object.text(math.ceil(rX/4)+1,1+n,l,1,tostring(arr[i]),'first','')
  136.   gpu.setForeground(tonumber(cf[2]))
  137.   gpu.set(rX-2- unicode.len(tostring(cf[1])),1+n,tostring(cf[1]))
  138. end
  139.  
  140. function drawClick(arr)
  141.   if oldSelected == nil then oldSelected = selected end
  142.   for i=active_position,rY-2+active_position-1 do
  143.     for j=1,#oldSelected do
  144.       if oldSelected[j] == arr[i] and oldSelected[j] ~= selected[j] then
  145.       n = i - active_position + 1
  146.       drawOneSegClick(arr,i,0xFFFFFF,n)
  147.     break
  148.     end
  149.     end
  150.   end
  151.   for i=active_position,rY-2+active_position-1 do
  152.     for j=1,#selected do
  153.       if selected[j] == arr[i] then
  154.       n = i - active_position + 1
  155.     drawOneSegClick(arr,i,0x007DFF,n)
  156.     break
  157.     end
  158.     end    
  159.   end
  160.   oldSelected = selected
  161. end
  162.  
  163. function reDrawFiles(arr,p)
  164.   if lastP == nil then lastP = 1 end
  165.   if p == -1 and rY < #arr and lastP ~= rY-2+active_position-1 then
  166.     gpu.copy(k+1,3,rX-k-2,rY-2-1,0,-1)
  167.     cf = rec(getPath(paths)..arr[rY-2+active_position-1])
  168.     l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
  169.     fi = finder(selected,arr[rY-2+active_position-1])
  170.   if fi[1] then
  171.       gpu.setBackground(0x007DFF)
  172.       gpu.fill(k+1,rY-1,l,1,' ')
  173.     else
  174.     gpu.setBackground(0xFFFFFF)
  175.     gpu.fill(k+1,rY-1,l,1,' ')
  176.     end
  177.   gpu.setForeground(getColorFile(arr[rY-2+active_position-1],2,0))
  178.     object.text(k+1,rY-1,l,1,tostring(arr[rY-2+active_position-1]),'first','')
  179.     gpu.setForeground(tonumber(cf[2]))
  180.     gpu.set(rX-2- unicode.len(tostring(cf[1])),rY-1,tostring(cf[1]))
  181.   lastP = rY-2+active_position-1
  182.   elseif p == 1 and rY < #arr and lastP ~= active_position then
  183.     gpu.copy(k+1,2,rX-k-2,rY-2-1,0,1)
  184.     cf = rec(getPath(paths)..arr[active_position])
  185.     l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
  186.     fi = finder(selected,arr[active_position])
  187.   if fi[1] then
  188.       gpu.setBackground(0x007DFF)
  189.       gpu.fill(k+1,2,l,1,' ')
  190.     else
  191.     gpu.setBackground(0xFFFFFF)
  192.     gpu.fill(k+1,2,l,1,' ')
  193.     end
  194.   gpu.setForeground(getColorFile(arr[active_position],2,0))
  195.     object.text(k+1,2,l,1,tostring(arr[active_position]),'first','')
  196.     gpu.setForeground(tonumber(cf[2]))
  197.     gpu.set(rX-2- unicode.len(tostring(cf[1])),2,tostring(cf[1]))
  198.   lastP = active_position
  199.   end
  200. end
  201.  
  202. function drawFiles(arr)
  203.   object.square(k+1,2,rX-k-2,rY-2,' ',0xffffff,0xFFFFFF)
  204.   gpu.setBackground(0xffffff)
  205.   n = 0
  206.   for i=active_position,rY-2+active_position-1 do
  207.     if arr[i] ~= nil then
  208.     n = n+1
  209.   if filesystem.isDirectory(arr[i]) == false then
  210.       cf = rec(getPath(paths)..arr[i])
  211.   else
  212.     cf = {'',0xffffff}
  213.   end
  214.     l = rX - math.ceil(rX/4) - 2 - unicode.len(cf[1])
  215.     fi = finder(selected,arr[i])
  216.     if fi[1] then
  217.       gpu.setBackground(0x007DFF)
  218.       gpu.fill(k+1,1+n,l,1,' ')
  219.     else
  220.       gpu.setBackground(0xFFFFFF)
  221.     end
  222.     gpu.setForeground(getColorFile(arr[i],2,0))
  223.     object.text(math.ceil(rX/4)+1,1+n,l,1,tostring(arr[i]),'first','')
  224.     gpu.setForeground(tonumber(cf[2]))
  225.     gpu.set(rX-2- unicode.len(tostring(cf[1])),1+n,tostring(cf[1]))
  226.   end
  227.   end
  228. end
  229.  
  230. function mainDraw(path)
  231.   object.init()
  232.   rX , rY = gpu.getResolution()
  233.   active_position = 1
  234.   k = math.ceil(rX/4)
  235.   object.square(1,1,rX,1,' ',0xffffff,0x222222)
  236.   object.add('button',rX-3+1,1,3,1,'[X]',1,0xffffff,0xFF0000,'Выход')
  237.   object.add('button',2,1,3,1,' < ',1,0xffffff,0x0000FF,'Назад')
  238.   object.add('scroll',k+1,2,rX-k,rY-1,' ',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',rX-1,2,2,rY-2,'vertical')
  239.   object.drawAllObject()
  240.   object.square(1,2,k,rY-1,' ',0xffffff,0xAAAAAA)
  241.   object.square(k+1,2,rX-k,rY-1,' ',0xffffff,0xFFFFFF)
  242.   f = getFiles(path)
  243.   drawFiles(f)
  244.   object.drawScroll('FirstScroll',active_position,#f,rY-2)
  245.   object.square(1,rY,rX,1,' ',0xffffff,0x222222)
  246.   object.add('board',1,2,k-2,math.ceil(rY / 4),0xffffff,0x505050,0xffffff,123123,true,1,'disk')
  247.   object.add('scroll',1,2,k,math.ceil(rY / 4)-2,' ',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'disk_scroll',k-1,2,2,math.ceil(rY / 4),'vertical')
  248.   object.drawBoard('disk',disks,disk_position)
  249.   object.drawScroll('disk_scroll',disk_position,#disks,math.ceil(rY / 4)-2)
  250. end
  251.  
  252. function getMyDisks(mode)
  253. local disks={}
  254. local fromAddress = filesystem.get(os.getenv("_")).address
  255. local candidates = {}
  256. for address in cmp.list("filesystem", true) do
  257.   local dev = cmp.proxy(address)
  258.   if --[[not dev.isReadOnly() and]] dev.address ~= computer.tmpAddress() then
  259.     table.insert(candidates, dev)
  260.   end
  261. end
  262. if mode==2 then
  263. for i = 1, #candidates do
  264.   local label = candidates[i].getLabel()
  265.   if label and label ~= 'raid' then
  266.     label = label
  267.   else
  268.     label = candidates[i].address
  269.   end
  270.   disks[#disks+1]={label,candidates[i].address}
  271.   end
  272.   return disks
  273. elseif mode == 1 then
  274. for i = 1, #candidates do
  275.   local label = candidates[i].getLabel()
  276.   if label and label ~= 'raid' then
  277.     label = label
  278.   else
  279.     label = candidates[i].address
  280.   end
  281.   disks[#disks+1]=label
  282.   end
  283.   return disks
  284.   end
  285. end
  286. function startDisk()
  287.   local disks=getMyDisks(2)
  288.   arrMyDiskAddress=filesystem.get(os.getenv("_"))
  289.   for i=1,#disks do
  290.   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
  291.     posDisk=i
  292.     break
  293.   end
  294.   end
  295.   return 'mnt/'..tostring(arrMyDiskAddress.address:sub(1,3))..'/' , posDisk
  296. end
  297.  
  298. object.init()
  299. --
  300. lastFile = 1
  301. lastPath = '..'
  302. lastDisk = disk_position
  303. --
  304. pa , disk_position = startDisk()
  305. disks = getMyDisks(1)
  306. dsk = getMyDisks(2)
  307. paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
  308. path = getPath(paths)
  309. --
  310. selected = {}
  311. k = math.ceil(rX/4)
  312. mainDraw('..')
  313. --
  314. fileBuffer = {}
  315. function reDrawAllFiles()
  316.   active_position = 1
  317.   dsk = getMyDisks(2)
  318.   paths = {'mnt/',unicode.sub(dsk[disk_position][2],1,3)..'/'}
  319.   f = getFiles(getPath(paths))
  320.   selected = {}
  321.   drawFiles(f)
  322. end
  323.  
  324. function inBuffer()
  325.   local arr = {}
  326.   for i=1,#selected do
  327.     arr[#arr+1]=tostring(getPath(paths)..tostring(selected[i]))
  328.   end
  329.   return arr
  330. end
  331.  
  332. function paste()
  333. if fileBuffer[1] ~= nil and fileBuffer[1] == 'cut' then
  334.     for i=1,#fileBuffer[2] do
  335.       local p = filesystem.segments(fileBuffer[2][i])
  336.       shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
  337.     end
  338.   for i=1,#fileBuffer[2] do
  339.       local p = filesystem.segments(fileBuffer[2][i])
  340.       shell.execute('del '..fileBuffer[2][i])
  341.     end
  342.   fileBuffer = {}
  343.   elseif fileBuffer[1] ~= nil then  
  344.     for i=1,#fileBuffer[2] do
  345.       local p = filesystem.segments(fileBuffer[2][i])
  346.       shell.execute('cp '..fileBuffer[2][i]..' '..getPath(paths)..p[#p])
  347.     end
  348.   end
  349. end
  350.  
  351. function op()
  352.    paths[#paths+1] = selected[1]
  353.    f = getFiles(getPath(paths))
  354.    drawFiles(f)
  355.    active_position = 1
  356. end
  357. --
  358. while true do
  359.   e = getEvent('touch','scroll','component_added','component_removed','key_up')
  360.   if e[1] ~= 'component_added' and e[1] ~= 'component_removed' and e[1] ~= 'key_up' then
  361.     arr=object.getPressedObject(e[3],e[4])
  362.   end
  363.   if arr ~= false and arr[1] == 'button' then
  364.     if arr[2] == 'Выход' then
  365.       setOld()
  366.       term.clear()
  367.       os.exit()
  368.   elseif arr ~= false and arr[2] == 'Назад' then
  369.     if #paths > 1 then
  370.       table.remove(paths)
  371.     f = getFiles(getPath(paths))
  372.     active_position = 1
  373.     selected = {}
  374.     drawFiles(f)
  375.     end
  376.     end
  377.   elseif arr ~= false and arr[1] == 'scroll' and e[1] == 'scroll' then
  378.     if arr~= false and arr[2]=='FirstScroll' then
  379.       active_position = active_position -e[5]
  380.       if active_position > #f-rY+3 then active_position = #f-rY+3 end
  381.       if active_position < 1  then active_position = 1 end
  382.       object.drawScroll('FirstScroll',active_position,#f,rY-2)
  383.       reDrawFiles(f,e[5])
  384.     elseif arr ~=false and arr[2]=='disk_scroll' then
  385.       disk_position = disk_position -e[5]
  386.       if disk_position > #disks then disk_position = #disks end
  387.       if disk_position < 1  then disk_position = 1 end
  388.       object.drawBoard('disk',disks,disk_position)
  389.       object.drawScroll('disk_scroll',disk_position,#disks,math.ceil(rY / 4)-2)
  390.       reDrawAllFiles()
  391.     end
  392.   elseif e[1] == 'key_up' then
  393.     if e[4] == 46 and keyboard.isControlDown() then
  394.     fileBuffer = {'copy',inBuffer()}
  395.   elseif e[4] == 47 and keyboard.isControlDown() then
  396.     paste()
  397.     f = getFiles(getPath(paths))
  398.       selected = {}
  399.       drawFiles(f)
  400.   elseif e[4] == 45 and keyboard.isControlDown() then
  401.     fileBuffer = {'cut',inBuffer()}
  402.   elseif e[4] == 30 and keyboard.isControlDown() then
  403. selected=f
  404.     drawClick(f)
  405.   elseif e[4] == 211 then
  406.     for i=1,#selected do
  407.         shell.execute('rm '..getPath(paths)..selected[i])
  408.       end
  409.       selected = {}
  410.     active_position = 1
  411.     f = getFiles(getPath(paths))
  412.       drawFiles(f)
  413. end
  414. elseif e[1] == 'component_added' or e[1] == 'component_removed'  and e[3] == 'filesystem' then
  415.   dsks = getMyDisks(1)
  416.   if tostring(unicode.sub(e[2],1,3)..'/') == paths[2] and e[1] == 'component_removed' then
  417.       disk_position = 1
  418.     object.drawBoard('disk',dsks,disk_position)
  419.         object.drawScroll('disk_scroll',disk_position,#dsks,math.ceil(rY / 4)-2)
  420.         reDrawAllFiles()
  421.     elseif #disks > #dsks then
  422.     if disk_position > #dsks and disks[disk_position] == '' then  
  423.      
  424.     end
  425.   end
  426.   disks = getMyDisks(1)
  427.     object.drawBoard('disk',disks,disk_position)
  428.     object.drawScroll('disk_scroll',disk_position,#disks,math.ceil(rY / 4)-2)
  429.   elseif arr ~= false and e[1] == 'touch' and arr[2] == 'FirstScroll'  then
  430.     if keyboard.isControlDown() and e[5] == 0 then
  431.     fi = finder(selected,f[active_position+(e[4]-2)])
  432.     if fi[1] == false then
  433.       selected[#selected+1] = f[active_position+(e[4]-2)]
  434.       drawClick(f)
  435.     end
  436.   elseif keyboard.isControlDown() == false then
  437.     selected = {f[active_position+(e[4]-2)]}
  438.     drawClick(f)
  439.   end
  440.     fi = finder(selected,f[active_position+(e[4]-2)])
  441.   if fi[1] and #selected == 1 and selected[#selected] == lastClick then
  442.     if unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) == '/' then
  443.       op()
  444.     elseif e[5] == 0 and unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) ~= '/' then
  445.       setOld()
  446.       term.clear()      
  447.       shell.execute(getColorFile(selected[1],3,'')..' '..getPath(paths)..selected[1])
  448.     event.pull()
  449.       mainDraw(getPath(paths))
  450.     elseif e[5] == 1 and #selected == 1 and unicode.sub(selected[1],unicode.len(selected[1]),unicode.len(selected[1])) ~= '/' and lastClick == selected[#selected] then
  451.       setOld()
  452.       shell.execute('edit '..getPath(paths)..selected[1])
  453.       mainDraw(getPath(paths))
  454.     end
  455.   end
  456.   lastClick = f[active_position+(e[4]-2)]
  457.   end
  458. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement