Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function menu(opts,x1,y1,x2,y2,offset)
- local x,y=term.getSize()
- x1=x1 or 1
- y1=y1 or 1
- x2=x2 or x-1
- y2=y2 or y-1
- offset=offset or 1
- s=1
- sc=0
- local function maxlen(t)
- local len=0
- for num,ref in ipairs(opts) do
- local item=(type(ref)=='table' and ref[1] or ref)
- if len<#item then len=#item end
- end
- return len
- end
- local function render()
- local max=maxlen(opts)+2
- local columns=math.floor(x2/(max+offset))
- local curcol=0
- local line=0
- for num,ref in ipairs(opts) do
- curcol=(curcol+1>columns and (function() line=line+1 return 1 end)() or curcol+1)
- if line>=sc and y2+sc>=line+1 then
- term.setCursorPos(x1+(curcol-1)*(max+offset),y1+line-sc)
- local item=(type(ref)=='table' and ref[1] or ref)
- write((num==s and '[' or ' ')..item..(num==s and ']' or ' ')..(#item+2<max and string.rep(' ',max-#(item+2)) or ''))
- end
- end
- return columns
- end
- while true do
- local cols=render()
- local rows=math.ceil(s/cols)
- local evts={os.pullEvent()}
- if evts[1]=='key' and evts[2]==208 then
- s=(s+cols<=#opts and s+cols or #opts)
- elseif evts[1]=='key' and evts[2]==200 then
- s=(s-cols>=1 and s-cols or 1)
- elseif evts[1]=='key' and evts[2]==203 and s>1 then
- s=s-1
- elseif evts[1]=='key' and evts[2]==205 and s<#opts then
- s=s+1
- elseif evts[1]=='key' and evts[2]==28 then
- return (type(opts[s])=='table' and opts[s][2](opts[s][1]) or opts[s])
- end
- if math.ceil(s/cols)>sc+y2 then sc=math.ceil(s/cols)-y2 end
- if math.ceil(s/cols)<=sc then sc=math.ceil(s/cols)-1 end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- menu({'hello1','hello2','hello3','hello4','hello5','hello6'},2,2,20,2)
Advertisement
Add Comment
Please, Sign In to add comment