Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function newmenu(tList,x,y,height)
- local function maxlen(t)
- local len=0
- for i=1,#t do
- local curlen=string.len(type(t[i])=='table' and t[i][1] or t[i])
- if curlen>len then len=curlen end
- end
- return len
- end
- local max=maxlen(tList)
- x=x or 1
- y=y or 1
- y=y-1
- height=height or #tList
- height=height+1
- local selected=1
- local scrolled=0
- local function render()
- for num,item in ipairs(tList) do
- if num>scrolled and num<scrolled+height then
- term.setCursorPos(x,y+num-scrolled)
- local current=(type(item)=='table' and item[1] or item)
- write((num==selected and '[' or ' ')..current..(num==selected and ']' or ' ')..(max-#current>0 and string.rep(' ',max-#current) or ''))
- end
- end
- end
- while true do
- render()
- local evts={os.pullEvent('key')}
- if evts[1]=="key" and evts[2]==200 and selected>1 then
- if selected-1<=scrolled then scrolled=scrolled-1 end
- selected=selected-1
- elseif evts[1]=="key" and evts[2]==208 and selected<#tList then
- selected=selected+1
- if selected>=height+scrolled then scrolled=scrolled+1 end
- elseif evts[1]=="key" and evts[2]==28 or evts[2]==156 then
- return (type(tList[selected])=='table' and tList[selected][2](tList[selected][1]) or tList[selected])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment