require("keybinds") ccstrings = require("cc.strings") local row_heights = { 5 } local col_widths = { 5 } local layout = {} local sel = { row = 1, col = 1, } function writeAt(text, x, y) term.setCursorPos(x, y) write(text) end function draw(x_offset, y_offset, draw_cell) local y = 1 + (y_offset or 0) for row = 1, #layout do local height = row_heights[row] local x = 1 + (x_offset or 0) for col = 1, #layout[row] do local width = col_widths[col] writeAt("+", x, y) writeAt("+", x + width, y) writeAt("+", x + width, y + height) writeAt("+", x, y + height) if sel.col == col and sel.row == row then writeAt(string.rep("-", width - 1), x + 1, y) writeAt(string.rep("-", width - 1), x + 1, y + height) for r = y + 1, y + height - 1 do writeAt("|", x, r) writeAt("|", x + width, r) end end menu_item = layout[row][col] if draw_cell then draw_cell(menu_item, x, y, width, height) elseif type(menu_item) == "string" then writeAt(menu_item, x + 1, y + 1) end x = x + width end y = y + height end end function handleInput(confirm_fn) local _event, key = os.pullEvent("key") if nav.down[key] then sel.row = math.min(sel.row + 1, #layout) elseif nav.up[key] then sel.row = math.max(sel.row - 1, 1) elseif nav.right[key] then sel.col = sel.col + 1 elseif nav.left[key] then sel.col = math.max(sel.col - 1, 1) elseif nav.confirm[key] then button = layout[sel.row][sel.col] confirm_fn(button) end sel.col = math.min(sel.col, #layout[sel.row]) end return { handleInput = handleInput, draw = draw, setLayout = function (grid, widths, heights) layout = grid col_widths = widths row_heights = heights end, }