Advertisement
Alex1979

file_browser

Sep 15th, 2013
1,827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1. local tFiles,tFilesSorted = {},{}
  2. local tStrings = {}
  3. local tLines = {}
  4. local tD,tF,tDr,tFr={},{},{},{}
  5. local w,h = term.getSize()
  6. local pos,scroll,virtualPos=1,1,1
  7. local nEntries = 0
  8. local bRunning = true
  9. local sDir=shell.dir()
  10. local bScreen,bMenu,bDir,bTxt,bDel,bRun,bName,bPar=true,false,false,false,false,false,false
  11. local nMenuPos,nDelPos,nRunPos=1,2,1
  12. local sMenu=''
  13. local sNewItem=''
  14. local currentFile=''
  15.  
  16. function close()
  17.   bRunning = false
  18.   shell.setDir(sDir)
  19.   term.scroll(h)
  20.   term.setCursorPos(1,1)
  21. end
  22.  
  23. function clearFiles()
  24.   tFiles,tFilesSorted = {},{}
  25.   tD,tF = {},{}
  26.   tDr,tFr={},{}
  27.   tStrings = {}
  28.   tLines = {}
  29. end
  30.  
  31.  
  32. function getExtension(sPar)
  33.   local sFile=sPar
  34.   local len = string.len(sFile)
  35.   local sExt=''
  36.   local sSub=''
  37.   while true do
  38.     sSub = string.sub(sFile,len,len)
  39.     if sSub=='.' then
  40.       break
  41.     else
  42.       sExt=sSub..sExt
  43.       len=len-1
  44.       if len==0 then
  45.         sExt=''
  46.         break
  47.       end
  48.     end
  49.   end
  50.   return sExt
  51. end
  52.      
  53.      
  54. function convertDir()
  55.   tFiles = fs.list(sDir)
  56.   if sDir~='' then
  57.     tD[1]='DIR  ..'
  58.   end
  59.   for n, sF in pairs(tFiles) do
  60.     local sP = fs.combine(sDir,sF)
  61.     if fs.isDir(sP) then
  62.       table.insert(tD,'DIR  '..sF)
  63.       table.insert(tDr,sF)
  64.     elseif getExtension(sF)~='' then
  65.       table.insert(tF,getExtension(sF)..'  '..string.sub(sF,1,string.len(sF)-string.len(getExtension(sF))-1))
  66.       table.insert(tFr,sF)
  67.     else
  68.       table.insert(tF,'txt  '..sF)
  69.       table.insert(tFr,sF)
  70.     end
  71.   end
  72.   for n,sF in pairs(tD) do
  73.     table.insert(tStrings,sF)
  74.   end
  75.   for n,sF in pairs(tF) do
  76.     table.insert(tStrings,sF)
  77.   end
  78.   if sDir~='' then
  79.     tFilesSorted[1]='..'
  80.   end
  81.   for n,sF in pairs(tDr) do
  82.     table.insert(tFilesSorted,sF)
  83.   end
  84.   for n,sF in pairs(tFr) do
  85.     table.insert(tFilesSorted,sF)
  86.   end
  87.   nEntries = #tStrings
  88. end
  89.  
  90.  
  91. function buildMenu()
  92.   if bScreen then
  93.     sMenu=  ' NewDir   NewTxt   Del   Name   Exit'
  94.   elseif bMenu then
  95.     if nMenuPos==1 then
  96.       sMenu='[NewDir]  NewTxt   Del   Name   Exit'
  97.     elseif nMenuPos==2 then
  98.       sMenu=' NewDir  [NewTxt]  Del   Name   Exit'
  99.     elseif nMenuPos==3 then
  100.       sMenu=' NewDir   NewTxt  [Del]  Name   Exit'
  101.     elseif nMenuPos==4 then
  102.       sMenu=' NewDir   NewTxt   Del  [Name]  Exit'
  103.     elseif nMenuPos==5 then
  104.       sMenu=' NewDir   NewTxt   Del   Name  [Exit]'
  105.     end
  106.   elseif bDir then
  107.     sMenu='New DIR> '..sNewItem
  108.   elseif bTxt then
  109.     sMenu='New TXT> '..sNewItem
  110.   elseif bName then
  111.     sMenu='Change Name> '..sNewItem
  112.   elseif bPar then
  113.     sMenu='Parameters> '..sNewItem
  114.   elseif bDel then
  115.     if nDelPos==1 then
  116.       sMenu='Delete? [ Yes ]   No  '
  117.     elseif nDelPos==2 then
  118.       sMenu='Delete?   Yes   [ No ]'
  119.     end
  120.   elseif bRun then
  121.     if nRunPos==1 then
  122.       sMenu='[Run] Edit '
  123.     elseif nRunPos==2 then
  124.       sMenu=' Run [Edit]'
  125.     end
  126.  
  127.   end
  128. end
  129.  
  130.  
  131. function drawScreen()
  132.   clearFiles()
  133.   convertDir()
  134.   term.clear()
  135.   term.setCursorPos(1,1)
  136.   for n=1,h-1 do
  137.     tLines[n]=tStrings[scroll+n-1]
  138.   end
  139.   buildMenu()
  140.   for n,Item in pairs(tLines) do
  141.     if n==pos-scroll+1 then
  142.       tLines[pos-scroll+1]='[ '..tLines[pos-scroll+1]..' ]'
  143.     else
  144.       tLines[n]='  '..tLines[n]
  145.     end
  146.   end
  147.   for n=1,h-2 do
  148.     term.setCursorPos(1,n)
  149.     term.write(tLines[n])
  150.   end
  151.   term.setCursorPos(1,h)
  152.   term.write(sMenu)
  153.  
  154.  
  155. end
  156.  
  157.  
  158.  
  159. term.clear()
  160. clearFiles()
  161. term.setCursorPos(1,1)
  162. drawScreen()
  163. shell.setDir('')
  164.  
  165.  
  166.  
  167. while bRunning do
  168.   local ev,par=os.pullEvent()
  169.   if ev=='key' then
  170.  
  171.     if par==200 then --Up
  172.       if (not bScreen) and (not bDel) then
  173.         bMenu,bDir,bTxt,bRun,bName=false,false,false,false,false
  174.         sNewItem=''
  175.         bScreen=true
  176.         pos=virtualPos
  177.       elseif bDel then
  178.       else
  179.         if pos>1 then
  180.           pos = pos-1
  181.         elseif pos==1 then
  182.           pos=nEntries
  183.           if nEntries>h-2 then
  184.             scroll=nEntries-h+3
  185.           end
  186.         end
  187.         if scroll>1 and pos==scroll then
  188.           scroll = scroll-1
  189.         end
  190.       end
  191.    
  192.     elseif par==208 then --Down
  193.       if (not bScreen) and (not bDel) then
  194.         bMenu,bDir,bTxt,bRun,bName=false,false,false,false,false
  195.         sNewItem=''
  196.         bScreen=true
  197.         pos=virtualPos
  198.       elseif bDel then
  199.       else
  200.         if pos < nEntries then
  201.           pos=pos+1
  202.         elseif pos==nEntries then
  203.           pos=1
  204.           scroll=1
  205.         end
  206.         if scroll<(nEntries-h+3) and pos == (scroll+h-3) then
  207.           scroll=scroll+1
  208.         end
  209.       end
  210.      
  211.     elseif par==54 then --rshift
  212.      
  213.      
  214.     elseif par==203 then --left
  215.       if bScreen then
  216.         virtualPos=pos
  217.         bScreen=false
  218.         bMenu=true
  219.       elseif bMenu then
  220.         if nMenuPos>1 then
  221.           nMenuPos=nMenuPos-1
  222.         elseif nMenuPos==1 then
  223.           nMenuPos=5
  224.         end
  225.       elseif bDel then
  226.         if nDelPos>1 then
  227.           nDelPos=nDelPos-1
  228.         elseif nDelPos==1 then
  229.           nDelPos=2
  230.         end
  231.       elseif bRun then
  232.         if nRunPos>1 then
  233.           nRunPos=nRunPos-1
  234.         elseif nRunPos==1 then
  235.           nRunPos=2
  236.         end
  237.       end
  238.    
  239.  
  240.     elseif par==205 then --right
  241.       if bScreen then
  242.         virtualPos=pos
  243.         bScreen=false
  244.         bMenu=true
  245.       elseif bMenu then
  246.         if nMenuPos<5 then
  247.           nMenuPos=nMenuPos+1
  248.         elseif nMenuPos==5 then
  249.           nMenuPos=1
  250.         end
  251.       elseif bDel then
  252.         if nDelPos<2 then
  253.           nDelPos=nDelPos+1
  254.         elseif nDelPos==2 then
  255.           nDelPos=1
  256.         end
  257.       elseif bRun then
  258.         if nRunPos<2 then
  259.           nRunPos=nRunPos+1
  260.         elseif nRunPos==2 then
  261.           nRunPos=1
  262.         end
  263.       end
  264.    
  265.     elseif par==29 or par==157 then --strg
  266.       if bScreen then
  267.         virtualPos=pos
  268.         bScreen=false
  269.         bMenu=true
  270.       elseif bMenu then
  271.         pos=virtualPos
  272.         bScreen=true
  273.         bMenu=false
  274.       end
  275.      
  276.     elseif par==28 then --enter
  277.       if bScreen then
  278.         if tFilesSorted[pos]=='..' then
  279.           if sDir == '' then
  280.             break
  281.           else
  282.             sDir=fs.combine(sDir,tFilesSorted[pos])
  283.             if sDir~='' then
  284.               pos=2
  285.             else
  286.               pos=1
  287.             end
  288.           end
  289.         elseif fs.isDir(fs.combine(sDir,tFilesSorted[pos])) then
  290.           sDir=fs.combine(sDir,tFilesSorted[pos])
  291.           pos=2
  292.         else
  293.           virtualPos=pos
  294.           bScreen=false
  295.           bRun=true
  296.         end
  297.       elseif bMenu then
  298.         if nMenuPos==1 then
  299.           bMenu=false
  300.           bDir=true
  301.         elseif nMenuPos==2 then
  302.           bMenu=false
  303.           bTxt=true
  304.         elseif nMenuPos==3 then
  305.           pos=virtualPos
  306.           nDelPos=2
  307.           bMenu=false
  308.           bDel=true
  309.         elseif nMenuPos==4 then
  310.           pos=virtualPos
  311.           bMenu=false
  312.           bName=true
  313.         elseif nMenuPos==5 then
  314.           break
  315.         end
  316.       elseif bRun then
  317.         if nRunPos==1 then
  318.           bRun=false
  319.           bPar=true
  320.         elseif nRunPos==2 then
  321.           shell.run('rom/programs/edit',fs.combine(sDir,tFilesSorted[pos]))
  322.           bRun=false
  323.           bScreen=true
  324.         end
  325.       elseif bPar then
  326.         term.clear()
  327.         term.setCursorPos(1,1)
  328.         shell.run(fs.combine(sDir,tFilesSorted[pos]),sNewItem)
  329.         sleep(2)
  330.         bPar=false
  331.         bScreen=true
  332.         sNewItem=''
  333.       elseif bDir then
  334.         fs.makeDir(fs.combine(sDir,sNewItem))
  335.         bDir=false
  336.         bScreen=true
  337.         sNewItem=''
  338.       elseif bTxt then
  339.         if not fs.exists(fs.combine(sDir,sNewItem)) then
  340.           local file=io.open(fs.combine(sDir,sNewItem),'w')
  341.           file:write('')
  342.           file:close()
  343.         end
  344.         bTxt=false
  345.         bScreen=true
  346.         sNewItem=''
  347.       elseif bDel then
  348.         if nDelPos==1 then
  349.           if not fs.isReadOnly(fs.combine(sDir,tFilesSorted[pos])) then
  350.             fs.delete(fs.combine(sDir,tFilesSorted[pos]))
  351.           end
  352.         end
  353.         if pos > 1 then
  354.           pos=pos-1
  355.         end
  356.         bDel=false
  357.         bScreen=true
  358.       elseif bName then
  359.         if not fs.isReadOnly(fs.combine(sDir,tFilesSorted[pos])) then
  360.           fs.move(fs.combine(sDir,tFilesSorted[pos]),fs.combine(sDir,sNewItem))
  361.         end
  362.         bName=false
  363.         bScreen=true
  364.         sNewItem=''
  365.       end
  366.      
  367.      
  368.     elseif par==14 then --back
  369.       if bDir or bTxt or bName or bPar then
  370.         if string.len(sNewItem)>1 then
  371.           sNewItem=string.sub(sNewItem,1,string.len(sNewItem)-1)
  372.         elseif string.len(sNewItem)==1 then
  373.           sNewItem=''
  374.         end
  375.       elseif bScreen then
  376.         if sDir ~= '' then
  377.           sDir=fs.combine(sDir,'..')
  378.           if sDir=='' then
  379.             pos=1
  380.           else
  381.             pos=2
  382.           end
  383.         end
  384.       end
  385.     end
  386.    
  387.    
  388.   elseif ev=='char' then
  389.     if bDir or bTxt or bName or bPar then
  390.       sNewItem=sNewItem..par
  391.     end
  392.   end
  393. drawScreen()
  394. end
  395. close()
  396. shell.setDir("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement