Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tab = {
- "Hi",
- "Goodbye",
- "Hello",
- "Exit",
- 'lololololololol',
- 'last',
- }
- local sX, sY = term.getSize()
- local function getLongest( _table )
- local longest = 0
- for index, text in pairs( _table ) do
- longest = #text > longest and #text or longest
- end
- return longest + 1
- end
- local function menu( _table, startX, startY )
- local function printMenu( _table, long, sel, yPos )
- --for index, text in pairs( _table ) do
- -- term.setCursorPos( startX, startY + index - 1 )
- -- write( ( index == sel and '[ ' or ' ' ) .. text .. string.rep(' ', long - #text) .. ( index == sel and ']' or ' ' ) )
- --end
- for i = 1, sY do
- if _table[i + yPos - 1] then
- local tmp = _table[i + yPos - 1]
- term.setCursorPos( startX, startY + i - 1 )
- write( ( i + yPos - 1 == sel and '[ ' or ' ' ) .. tmp .. string.rep(' ', long - #tmp) .. ( i + yPos - 1 == sel and ']' or ' ' ) )
- else break end
- end
- end
- local longest = getLongest( _table )
- local sel = 1
- local yPos = 1
- while true do
- printMenu( _table, longest, sel, yPos )
- local e, key = os.pullEvent()
- if e == "key" or e == "mouse_scroll" then
- if key == keys.up or key == -1 then
- -- up
- if yPos > 1 then
- yPos = yPos - 1
- end
- if sel > 1 then
- sel = sel - 1
- end
- elseif key == keys.down or key == 1 then
- -- down
- if sel < #_table then
- sel = sel + 1
- end
- if yPos < #_table - 18 and sel >= 20 then
- yPos = yPos + 1
- end
- elseif key == keys.enter then
- return _table[sel], sel
- end
- end
- end
- end
- term.clear()
- while true do
- local sOption, sNumb = menu( tab, (sX-getLongest(tab))/2, 1 )
- term.clear() term.setCursorPos( 1, 1 )
- if sOption == "Hi" then
- print( "Hello there! :D" )
- elseif sOption == "Goodbye" then
- print( "Why are you going? :(" )
- elseif sOption == "Hello" then
- print( "Oh hello again :>" )
- elseif sOption == "Exit" then
- print( "Cheers :<" )
- break
- end
- sleep(2)
- term.clear() term.setCursorPos( 1, 1 )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement