Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tFiles,tFilesSorted = {},{}
- local tStrings = {}
- local tLines = {}
- local tD,tF,tDr,tFr={},{},{},{}
- local w,h = term.getSize()
- local pos,scroll,virtualPos=1,1,1
- local nEntries = 0
- local bRunning = true
- local sDir=shell.dir()
- local bScreen,bMenu,bDir,bTxt,bDel,bRun,bName,bPar=true,false,false,false,false,false,false
- local nMenuPos,nDelPos,nRunPos=1,2,1
- local sMenu=''
- local sNewItem=''
- local currentFile=''
- function close()
- bRunning = false
- shell.setDir(sDir)
- term.scroll(h)
- term.setCursorPos(1,1)
- end
- function clearFiles()
- tFiles,tFilesSorted = {},{}
- tD,tF = {},{}
- tDr,tFr={},{}
- tStrings = {}
- tLines = {}
- end
- function getExtension(sPar)
- local sFile=sPar
- local len = string.len(sFile)
- local sExt=''
- local sSub=''
- while true do
- sSub = string.sub(sFile,len,len)
- if sSub=='.' then
- break
- else
- sExt=sSub..sExt
- len=len-1
- if len==0 then
- sExt=''
- break
- end
- end
- end
- return sExt
- end
- function convertDir()
- tFiles = fs.list(sDir)
- if sDir~='' then
- tD[1]='DIR ..'
- end
- for n, sF in pairs(tFiles) do
- local sP = fs.combine(sDir,sF)
- if fs.isDir(sP) then
- table.insert(tD,'DIR '..sF)
- table.insert(tDr,sF)
- elseif getExtension(sF)~='' then
- table.insert(tF,getExtension(sF)..' '..string.sub(sF,1,string.len(sF)-string.len(getExtension(sF))-1))
- table.insert(tFr,sF)
- else
- table.insert(tF,'txt '..sF)
- table.insert(tFr,sF)
- end
- end
- for n,sF in pairs(tD) do
- table.insert(tStrings,sF)
- end
- for n,sF in pairs(tF) do
- table.insert(tStrings,sF)
- end
- if sDir~='' then
- tFilesSorted[1]='..'
- end
- for n,sF in pairs(tDr) do
- table.insert(tFilesSorted,sF)
- end
- for n,sF in pairs(tFr) do
- table.insert(tFilesSorted,sF)
- end
- nEntries = #tStrings
- end
- function buildMenu()
- if bScreen then
- sMenu= ' NewDir NewTxt Del Name Exit'
- elseif bMenu then
- if nMenuPos==1 then
- sMenu='[NewDir] NewTxt Del Name Exit'
- elseif nMenuPos==2 then
- sMenu=' NewDir [NewTxt] Del Name Exit'
- elseif nMenuPos==3 then
- sMenu=' NewDir NewTxt [Del] Name Exit'
- elseif nMenuPos==4 then
- sMenu=' NewDir NewTxt Del [Name] Exit'
- elseif nMenuPos==5 then
- sMenu=' NewDir NewTxt Del Name [Exit]'
- end
- elseif bDir then
- sMenu='New DIR> '..sNewItem
- elseif bTxt then
- sMenu='New TXT> '..sNewItem
- elseif bName then
- sMenu='Change Name> '..sNewItem
- elseif bPar then
- sMenu='Parameters> '..sNewItem
- elseif bDel then
- if nDelPos==1 then
- sMenu='Delete? [ Yes ] No '
- elseif nDelPos==2 then
- sMenu='Delete? Yes [ No ]'
- end
- elseif bRun then
- if nRunPos==1 then
- sMenu='[Run] Edit '
- elseif nRunPos==2 then
- sMenu=' Run [Edit]'
- end
- end
- end
- function drawScreen()
- clearFiles()
- convertDir()
- term.clear()
- term.setCursorPos(1,1)
- for n=1,h-1 do
- tLines[n]=tStrings[scroll+n-1]
- end
- buildMenu()
- for n,Item in pairs(tLines) do
- if n==pos-scroll+1 then
- tLines[pos-scroll+1]='[ '..tLines[pos-scroll+1]..' ]'
- else
- tLines[n]=' '..tLines[n]
- end
- end
- for n=1,h-2 do
- term.setCursorPos(1,n)
- term.write(tLines[n])
- end
- term.setCursorPos(1,h)
- term.write(sMenu)
- end
- term.clear()
- clearFiles()
- term.setCursorPos(1,1)
- drawScreen()
- shell.setDir('')
- while bRunning do
- local ev,par=os.pullEvent()
- if ev=='key' then
- if par==200 then --Up
- if (not bScreen) and (not bDel) then
- bMenu,bDir,bTxt,bRun,bName=false,false,false,false,false
- sNewItem=''
- bScreen=true
- pos=virtualPos
- elseif bDel then
- else
- if pos>1 then
- pos = pos-1
- elseif pos==1 then
- pos=nEntries
- if nEntries>h-2 then
- scroll=nEntries-h+3
- end
- end
- if scroll>1 and pos==scroll then
- scroll = scroll-1
- end
- end
- elseif par==208 then --Down
- if (not bScreen) and (not bDel) then
- bMenu,bDir,bTxt,bRun,bName=false,false,false,false,false
- sNewItem=''
- bScreen=true
- pos=virtualPos
- elseif bDel then
- else
- if pos < nEntries then
- pos=pos+1
- elseif pos==nEntries then
- pos=1
- scroll=1
- end
- if scroll<(nEntries-h+3) and pos == (scroll+h-3) then
- scroll=scroll+1
- end
- end
- elseif par==54 then --rshift
- elseif par==203 then --left
- if bScreen then
- virtualPos=pos
- bScreen=false
- bMenu=true
- elseif bMenu then
- if nMenuPos>1 then
- nMenuPos=nMenuPos-1
- elseif nMenuPos==1 then
- nMenuPos=5
- end
- elseif bDel then
- if nDelPos>1 then
- nDelPos=nDelPos-1
- elseif nDelPos==1 then
- nDelPos=2
- end
- elseif bRun then
- if nRunPos>1 then
- nRunPos=nRunPos-1
- elseif nRunPos==1 then
- nRunPos=2
- end
- end
- elseif par==205 then --right
- if bScreen then
- virtualPos=pos
- bScreen=false
- bMenu=true
- elseif bMenu then
- if nMenuPos<5 then
- nMenuPos=nMenuPos+1
- elseif nMenuPos==5 then
- nMenuPos=1
- end
- elseif bDel then
- if nDelPos<2 then
- nDelPos=nDelPos+1
- elseif nDelPos==2 then
- nDelPos=1
- end
- elseif bRun then
- if nRunPos<2 then
- nRunPos=nRunPos+1
- elseif nRunPos==2 then
- nRunPos=1
- end
- end
- elseif par==29 or par==157 then --strg
- if bScreen then
- virtualPos=pos
- bScreen=false
- bMenu=true
- elseif bMenu then
- pos=virtualPos
- bScreen=true
- bMenu=false
- end
- elseif par==28 then --enter
- if bScreen then
- if tFilesSorted[pos]=='..' then
- if sDir == '' then
- break
- else
- sDir=fs.combine(sDir,tFilesSorted[pos])
- if sDir~='' then
- pos=2
- else
- pos=1
- end
- end
- elseif fs.isDir(fs.combine(sDir,tFilesSorted[pos])) then
- sDir=fs.combine(sDir,tFilesSorted[pos])
- pos=2
- else
- virtualPos=pos
- bScreen=false
- bRun=true
- end
- elseif bMenu then
- if nMenuPos==1 then
- bMenu=false
- bDir=true
- elseif nMenuPos==2 then
- bMenu=false
- bTxt=true
- elseif nMenuPos==3 then
- pos=virtualPos
- nDelPos=2
- bMenu=false
- bDel=true
- elseif nMenuPos==4 then
- pos=virtualPos
- bMenu=false
- bName=true
- elseif nMenuPos==5 then
- break
- end
- elseif bRun then
- if nRunPos==1 then
- bRun=false
- bPar=true
- elseif nRunPos==2 then
- shell.run('rom/programs/edit',fs.combine(sDir,tFilesSorted[pos]))
- bRun=false
- bScreen=true
- end
- elseif bPar then
- term.clear()
- term.setCursorPos(1,1)
- shell.run(fs.combine(sDir,tFilesSorted[pos]),sNewItem)
- sleep(2)
- bPar=false
- bScreen=true
- sNewItem=''
- elseif bDir then
- fs.makeDir(fs.combine(sDir,sNewItem))
- bDir=false
- bScreen=true
- sNewItem=''
- elseif bTxt then
- if not fs.exists(fs.combine(sDir,sNewItem)) then
- local file=io.open(fs.combine(sDir,sNewItem),'w')
- file:write('')
- file:close()
- end
- bTxt=false
- bScreen=true
- sNewItem=''
- elseif bDel then
- if nDelPos==1 then
- if not fs.isReadOnly(fs.combine(sDir,tFilesSorted[pos])) then
- fs.delete(fs.combine(sDir,tFilesSorted[pos]))
- end
- end
- if pos > 1 then
- pos=pos-1
- end
- bDel=false
- bScreen=true
- elseif bName then
- if not fs.isReadOnly(fs.combine(sDir,tFilesSorted[pos])) then
- fs.move(fs.combine(sDir,tFilesSorted[pos]),fs.combine(sDir,sNewItem))
- end
- bName=false
- bScreen=true
- sNewItem=''
- end
- elseif par==14 then --back
- if bDir or bTxt or bName or bPar then
- if string.len(sNewItem)>1 then
- sNewItem=string.sub(sNewItem,1,string.len(sNewItem)-1)
- elseif string.len(sNewItem)==1 then
- sNewItem=''
- end
- elseif bScreen then
- if sDir ~= '' then
- sDir=fs.combine(sDir,'..')
- if sDir=='' then
- pos=1
- else
- pos=2
- end
- end
- end
- end
- elseif ev=='char' then
- if bDir or bTxt or bName or bPar then
- sNewItem=sNewItem..par
- end
- end
- drawScreen()
- end
- close()
- shell.setDir("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement