Advertisement
AKopyl

filEx

Dec 7th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 KB | None | 0 0
  1. W, H = term.getSize()
  2.  
  3. selected = 1
  4. scroll = 0
  5.  
  6. currentPath = ''
  7.  
  8. fileList = fs.list(currentPath)
  9.  
  10. while true do
  11.     term.clear()
  12.    
  13.     term.setCursorPos(3, 1)
  14.     term.write('name')
  15.     term.setCursorPos(W-21, 1)
  16.     term.write('type')
  17.     term.setCursorPos(W-15, 1)
  18.     term.write('dir')
  19.     term.setCursorPos(W-10, 1)
  20.     term.write('size')
  21.     term.setCursorPos(1, 2)
  22.     for n = 1, W do
  23.         term.write('-')
  24.     end
  25.    
  26.     if #fileList > H-2 then
  27.         term.setCursorPos(W, 2+(H-2)*selected/#fileList)
  28.         term.write('#')
  29.     end
  30.  
  31.     term.setCursorPos(1, H)
  32.     if selected ~= 0 then
  33.         term.write(currentPath..'/'..(fileList[selected] or ''))
  34.     end
  35.     term.setCursorPos(W-tostring(#fileList):len(), H)
  36.     term.write(#fileList)
  37.  
  38.     term.setCursorPos(1, selected+3-scroll)
  39.     for n = 1, W do
  40.         term.write('-')
  41.     end
  42.     term.setCursorPos(1, selected+3-scroll)
  43.     if selected == 0 or fs.isDir(currentPath..'/'..(fileList[selected] or '')) then
  44.         term.write('>>')
  45.     else
  46.         term.write('->')
  47.     end
  48.  
  49.     if 3-scroll < H and 3-scroll > 2 then
  50.         term.setCursorPos(3, 3-scroll)
  51.         term.write('..')
  52.     end
  53.    
  54.     for n, file in ipairs(fileList) do
  55.         if n+3-scroll < H and n+3-scroll > 2 then
  56.             term.setCursorPos(3, n+3-scroll)
  57.             term.write(file)
  58.  
  59.             if file:sub(#file-3) == '.nfp' then
  60.                 term.setCursorPos(W-21, n+3-scroll)
  61.                 term.write('IMG')
  62.             end
  63.  
  64.             if file:sub(#file-3) == '.lua' then
  65.                 term.setCursorPos(W-21, n+3-scroll)
  66.                 term.write('LUA')
  67.             end
  68.            
  69.             if fs.isDir(currentPath..'/'..fileList[n]) then
  70.                 term.setCursorPos(W-16, n+3-scroll)
  71.                 term.write('[\\_/]')
  72.             end
  73.            
  74.             term.setCursorPos(W-10, n+3-scroll)
  75.             term.write(fs.getSize(currentPath..'/'..file))
  76.         end
  77.     end
  78.  
  79.     local event = {os.pullEvent()}
  80.  
  81.     if event[1] == 'key' then
  82.         if event[2] == keys.up then
  83.             selected = selected - 1
  84.         elseif event[2] == keys.down then
  85.             selected = selected + 1
  86.         end
  87.         if selected > #fileList then
  88.             selected = 0
  89.             scroll = 0
  90.         end
  91.         if selected < 0 then
  92.             selected = #fileList
  93.             if H+3 > #fileList then
  94.                 scroll = 0
  95.             else
  96.                 scroll = #fileList-H+3
  97.             end
  98.         end
  99.  
  100.         if selected-scroll > H-4 then
  101.             scroll = scroll + 1
  102.         end
  103.  
  104.         if selected-scroll < 0 then
  105.             scroll = scroll - 1
  106.         end
  107.  
  108.         if event[2] == 28 then
  109.             if selected == 0 then
  110.                 if currentPath ~= '' then
  111.                     currentPath = fs.combine(currentPath, '..')
  112.                     fileList = fs.list(currentPath)
  113.                 end
  114.             else
  115.                 if fs.isDir(currentPath..'/'..fileList[selected]) then
  116.                     currentPath = currentPath..'/'..fileList[selected]
  117.                     fileList = fs.list(currentPath)
  118.                     selected = 1
  119.                     scroll = 0
  120.                 else
  121.                     shell.run(currentPath..'/'..fileList[selected])
  122.                 end
  123.             end
  124.         end
  125.  
  126.         if event[2] == 19 then
  127.             os.pullEvent('char')
  128.             if selected ~= 0 then
  129.                 term.clear()
  130.                 term.setCursorPos(1, 1)
  131.                 term.write('Are you sure you want to delete '..fileList[selected]..'?')
  132.                 term.write(' (y/n)')
  133.                 term.setCursorPos(1, 2)
  134.                 local input = read()
  135.                 if string.lower(input) == 'y' then
  136.                     shell.run('rm '..currentPath..'/'..fileList[selected])
  137.                     fileList = fs.list(currentPath)
  138.                     selected = 1
  139.                     scroll = 0
  140.                 elseif string.lower(input) ~= 'n' then
  141.                     term.setCursorPos(1, 3)
  142.                     term.write('Incorrect input!')
  143.                 end
  144.             end
  145.         end
  146.  
  147.         if event[2] == 18 then
  148.             os.pullEvent('char')
  149.             if selected ~= 0 and not fs.isDir(currentPath..'/'..fileList[selected]) then
  150.                 if fileList[selected]:sub(#fileList[selected]-3) == '.nfp' then
  151.                     shell.run('paint '..currentPath..'/'..fileList[selected])
  152.                 else
  153.                     shell.run('edit '..currentPath..'/'..fileList[selected])
  154.                 end
  155.             end
  156.         end
  157.  
  158.         if event[2] == 50 then
  159.             if selected ~= 0 and not fs.isDir(currentPath..'/'..fileList[selected]) then
  160.                 term.clear()
  161.                 term.setCursorPos(1, 1)
  162.                 shell.run('monitor monitor_0', currentPath..'/'..fileList[selected])
  163.             end
  164.         end
  165.  
  166.         if event[2] == 16 then
  167.             shell.run('.menu')
  168.         end
  169.     elseif event[1] == 'monitor_touch' or event[1] == 'mouse_click' then
  170.         if event[4] < H-1 and event[4] > 2 then
  171.             if selected == event[4] - 3 then
  172.                 if selected == 0 then
  173.                     if currentPath ~= '' then
  174.                         currentPath = fs.combine(currentPath, '..')
  175.                         fileList = fs.list(currentPath)
  176.                     end
  177.                 else
  178.                     if fs.isDir(currentPath..'/'..fileList[selected]) then
  179.                         currentPath = currentPath..'/'..fileList[selected]
  180.                         fileList = fs.list(currentPath)
  181.                         selected = 1
  182.                     else
  183.                         shell.run(currentPath..'/'..fileList[selected])
  184.                     end
  185.                 end
  186.             else
  187.                 selected = event[4] - 3
  188.             end
  189.         end
  190.     end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement