Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
willowyknight76

need help with a menu

Question

hey I'm trying to make a menu based os that you navigate with the arrow keys and enter button

it should look like this

                        command

                        programs

                       [shutdown]

                        reinstall

it's bringing up this error

too long without yielding:

stack traceback:

                   machine:809: in function 'xpcall'

                   /lib/process.lua:63: in function </lib/process.lua:59>

here's the code I can't for the the life of me figure out what's wrong

local kb = require("keyboard")
local event = require("event")
local component = require("component")
local gpu = component.gpu
local term = require("term")
local w,h = gpu.getResolution()

function printCentered (y,s)
    local x = math.floor((w - string.len(s)) / 2)
    term.setCursor(x,y)
    term.clearLine()
    term.write(s)
end

--menu up
local function up()
  keyboard.keys.up = up()
end
--menu down
local function down()
  keyboard.keys.down = down()
end
--menu enter
local function enter() 
  keyboard.key.enter = enter()
end
--draw menu

local nOption = 1

local function drawMenu()
    term.clear()
    term.setCursor(1,1)
    term.write("perdOS")
    
    term.setCursor(w-11,1)
    if nOption == 1 then
        term.write("Command")
    elseif nOption == 2 then
        term.write("programs")
    elseif nOption == 3 then
        term.write("shutdown")
    elseif nOption == 4 then
        term.write("uninstall")
    else
        end
end

--gui
term.clear()
local function drawFE()
    printCentered(math.floor(h/2) - 3, "")
    printCentered(math.floor(h/2) - 2, "start menu")
    printCentered(math.floor(h/2) - 1, "")
    printCentered(math.floor(h/2) + 0, ((nOption == 1) and "[command  ]") or "command")
    printCentered(math.floor(h/2) + 1, ((nOption == 2) and "[programs ]") or "programs")
    printCentered(math.floor(h/2) + 2, ((nOption == 3) and "[shutdown ]") or "shutdown")
    printCentered(math.floor(h/2) + 3, ((nOption == 4) and "[uninstall]") or "uninstall")
end

--display

drawMenu()
drawFE()

while true do
    local e,p = kb.isKeyDown("")
        if e == "key" then
            local key = p
            if key == down() then
                if nOption > 1 then
                    nOption = nOption - 1
                    drawMenu()
                    drawFE()
                end
            elseif key == up() then
                if nOption < 4 then
                    nOption = nOption + 1
                    drawMenu()
                    drawFE()
                end
            
        elseif key == enter() then
            break
        end
    end
end

--conditions
if nOption == 1 then
shell.run("os/.command")
elseif nOption == 2 then
    shell.run("os/.programs")
elseif nOption == 3 then
    os.shutdown()
else
    shell.run("os/.uninstall")
end

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

You are creating a loop without any yielding, so, the OpenOS create a exemption to break the loop and close you program.

To solve this, just use os.sleep(time), where time is the time that the program will wait in the while loop.

Other way is use the event api, using local event = require"event", and using event.pull("key_down"), see the event api documentation to understand tha api, the event.pull function block the loop until the "key_down" event happen.

The 2 ways do the same thing, create a yielding in program thread, so the OpenOS khows that the program isn't in a no end loop.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.