Posted 12 February 2014 - 05:35 PM
I'm writing a function to allow selection from a list of options, returning the index of the selected choice. At first, it only worked if the length of the items fit onto the page, then I modified it, so now it can do a list if the items fit on a second page, at the cost of losing the header. However, anything larger than two pages just does weird things. Anyone know how I can fix it?
Here's the code:
Help is appreciated.
Here's the code:
Spoiler
local function header(banner)
term.clear()
term.setCursorPos(1,1)
print("HimCo Industries' Experimental OS")
--print(loadFromFile('session','./config'),' logged into Computer ID #',os.getComputerID())
print()
printCentered(banner)
print()
end
local function lineClear()
local _,y=term.getCursorPos()
term.clearLine()
term.setCursorPos(1,y)
end
function printCentered(str)
local termX, _termY=term.getSize()
local _cursX, cursY=term.getCursorPos()
local newX = math.ceil(termX/2)-math.ceil(string.len(str)/2)
term.setCursorPos(newX,cursY)
print(str)
end
local function multipleChoice(choices, sel,escape,delete)
local shiftList=false
local escape=escape or 'startup'
local size = #choices
local sel = sel or 1 --initial selection
local xsize,ysize=term.getSize()
local xcoord,ycoord=term.getCursorPos()
local diff=ysize-ycoord
if diff>#choices then
shiftList=true
end
while true do
if sel<=diff then
term.setCursorPos(xcoord,ycoord)
for i,v in ipairs(choices) do
lineClear()
if i == sel then
printCentered('[ '..v..' ]')
elseif i <= diff then
printCentered(' '..v..' ')
end
end
else
for i,v in ipairs(choices) do
printCentered('')
end
local over=diff-sel
term.setCursorPos(xcoord,ycoord)
for i,v in ipairs(choices) do
lineClear()
if i == sel then
printCentered('[ '..v..' ]')
elseif i >= sel+over then
printCentered(' '..v..' ')
end
end
end
--user input
local event,key=os.pullEvent('key')
if key == 208 --down arrow
or key == 31 then -- S
sel = sel+1
if sel > size then
sel = 1 --bottom reached, moving back to top
end
elseif key == 200 -- up arrow
or key == 17 then -- W
sel = sel-1
if sel < 1 then
sel = size -- top reached, moving to bottom
end
elseif key == 28 -- enter
or key == 57 then --space
return sel --input complete
elseif key == 14 then -- backspace
shell.run(escape)
shell.exit()
--os.reboot()
elseif key == 211 and delete then -- Returns true as well as selection when delete is pressed
return sel,true
end
end
end
header('What?')
choices={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','aa','ab','ac','ad','ae','af'}
choice=multipleChoice(choices)