Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function menu(tOpts,x,y,wd,ht,tInfo,col1,col2)
- term.setCursorBlink(false)
- local selected=1
- local scrolled=0
- local scrolledx=0
- tInfo=tInfo or {}
- local max=0
- for k,v in ipairs(tOpts) do
- tOpts[k]=tostring(v) or ""
- tInfo[k]=tInfo[k] or {}
- max=math.max(max,#v)
- end
- local tColMax={}
- for i=1,#tInfo do
- for k,v in pairs(tInfo[i]) do
- tInfo[i][k]=tostring(v) or ""
- tColMax[k]=math.max(tColMax[k] or 0,#v)
- end
- end
- local totMax=max+2
- for k,v in pairs(tColMax) do
- totMax=totMax+v
- end
- while true do
- term.setTextColor(colors.black)
- for cy=1,ht do
- term.setBackgroundColor(selected-scrolled==cy and (col1 or colors.magenta) or cy==ht and colors.gray or (col2 or colors.purple))
- term.setCursorPos(x,y+cy-1)
- write(string.rep(" ",wd-1))
- term.setBackgroundColor(colors.gray)
- write(" ")
- end
- for k,v in ipairs(tOpts) do
- if k>scrolled and k-scrolled<=ht-2 then
- term.setCursorPos(x,y+k-1-scrolled)
- term.setBackgroundColor(selected==k and (col1 or colors.magenta) or (col2 or colors.purple))
- local line=(k==selected and "[" or " ")..v..(k==selected and "]" or " ")..string.rep(" ",max-#v)
- for col,sInfo in pairs(tInfo[k]) do
- line=line..sInfo..string.rep(" ",tColMax[col]-#sInfo+1)
- end
- write(string.sub(line,1+scrolledx,wd+scrolledx-2))
- end
- end
- vL=math.ceil(ht*ht/#tOpts)
- hL=math.ceil(wd*wd/totMax)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(math.floor(scrolledx*(totMax-wd)/(totMax-hL)+x),y+ht-1)
- write(string.rep(" ",math.min(hL,wd-math.floor(scrolledx*(totMax-wd)/(totMax-hL)+x)+1)))
- for cy=1,math.min(vL,ht-math.floor(scrolled*(ht-vL-1)/(#tOpts-ht)+y)+1) do
- term.setCursorPos(x+wd-1,math.floor(scrolled*(ht-vL-1)/(#tOpts-ht)+y)+cy-1)
- write(" ")
- end
- term.setCursorPos(x+wd-1,y+ht-1)
- write(" ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local evts={os.pullEvent()}
- if ((evts[1]=="mouse_scroll" and evts[2]==-1) or (evts[1]=="key" and evts[2]==200)) and selected>1 then
- selected=selected-1
- elseif ((evts[1]=="mouse_scroll" and evts[2]==1) or (evts[1]=="key" and evts[2]==208)) and selected<#tOpts then
- selected=selected+1
- elseif evts[1]=="key" and evts[2]==203 and scrolledx>0 then
- scrolledx=scrolledx-1
- elseif evts[1]=="key" and evts[2]==205 and scrolledx<totMax-wd+2 then
- scrolledx=scrolledx+1
- elseif evts[1]=="key" and evts[2]==28 then
- return tOpts[selected]
- elseif evts[1]=="mouse_click" and evts[2]==1 and evts[3]>=x and evts[3]<x+wd and tOpts[evts[4]-y+scrolled+1] then
- return tOpts[evts[4]-y+scrolled+1]
- end
- if selected<=scrolled then
- scrolled=selected-1
- elseif selected>scrolled+ht-2 then
- scrolled=selected-ht+2
- end
- end
- end
- local tOpts,tInfo={},{}
- while true do
- local curDir=shell.dir()
- local dirList=fs.list(shell.dir())
- if shell.dir()~="" then table.insert(dirList,1,"..\\") end
- for num,sName in pairs(dirList) do
- table.insert(tOpts,sName)
- table.insert(tInfo,{tostring(fs.isDir(fs.combine(curDir,sName))),tostring(fs.getSize(fs.combine(curDir,sName)))})
- end
- local size={term.getSize()}
- local resp=fs.combine(curDir,menu(tOpts,2,2,size[1]-2,size[2]-2,tInfo))
- if fs.isDir(resp) then
- shell.setDir(resp)
- tOpts={}
- else
- tOpts={}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment