Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAX_OPTIONS = 13
- function promptForNumber()
- local value = nil
- while value == nil do
- io.write("> ")
- value = tonumber(io.read())
- if value == nil then
- print("ERROR: Invalid number. Please try again.")
- end
- end
- return value
- end
- function promptForNumberInRange(min, max)
- local value = nil
- while value == nil do
- local tValue = promptForNumber()
- if tValue < min then
- print("ERROR: please enter a number >= " .. min)
- elseif tValue > max then
- print("ERROR: please neter a number <= " .. max)
- else
- value = tValue
- end
- end
- return value
- end
- function printOptions(options, banner)
- local page = 1
- local N = table.getn(options)
- local maxPage = math.ceil(N / MAX_OPTIONS)
- local viewing = true
- while viewing do
- print(banner)
- if N > MAX_OPTIONS then
- term.clear()
- term.setCursorPos(1, 1)
- print(banner)
- print()
- print(string.format(
- "Viewing %d items (%d/%d)",
- N,
- page,
- maxPage
- ))
- print("------------------------------------")
- end
- local start = (((page - 1) * MAX_OPTIONS) + 1)
- for i = start, math.min(N, (start + MAX_OPTIONS - 1)), 1 do
- local option = options[i]
- if type(option) == "table" then
- option = option.name
- end
- print(string.format("%d. %s", i, option))
- end
- if N <= MAX_OPTIONS then
- break
- else
- print("------------------------------------")
- io.write("[j] previous, [k] next, [Enter] done")
- while true do
- local sEvent, key = os.pullEvent("key")
- if key == keys.k then
- page = math.min(maxPage, page+1)
- break
- elseif key == keys.j then
- page = math.max(1, page-1)
- break
- elseif key == keys.enter then
- viewing = false
- break
- end
- end
- end
- if not viewing then
- print()
- end
- end
- end
- function chooseOption(options)
- printOptions(options)
- local option = promptForNumberInRange(1, table.getn(options))
- return options[option]
- end
Add Comment
Please, Sign In to add comment