Advertisement
PrinceOfCookies

Untitled

Aug 1st, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. function cosUtils.centerPrintTable(y, w, items, numOp, slow, col, margins, win)
  2. win = win and win or term
  3. local ts = tostring
  4. cosUtils.logToOS("Color: " .. ts(col) .. " | Slow: " .. ts(slow) .. " | NumOp: " .. ts(numOp) .. " | Margins: " .. ts(margins))
  5. col = col and col or colors.black
  6. slow = slow == nil and false or slow
  7. numOp = numOp and numOp or 0
  8. margins = margins == nil and true or margins
  9. cosUtils.logToOS("Color: " .. ts(col) .. " | Slow: " .. ts(slow) .. " | NumOp: " .. ts(numOp) .. " | Margins: " .. ts(margins))
  10. -- Calculate the longest line
  11. local longestLine = 0
  12.  
  13. for _, item in ipairs(items) do
  14. if #item > longestLine then
  15. longestLine = #item
  16. end
  17. end
  18.  
  19. -- Set Box Dimensions
  20. local wMarg, hMarg = margins and 4 or 0, margins and 2 or 0
  21. local boxWidth = longestLine + wMarg -- 1 margin on each side + 2 for borders
  22. local boxHeight = #items + hMarg -- 1 margin on top and bottom + 2 for borders
  23. -- Calculate the starting position
  24. local x = math.floor((w - boxWidth) / 2)
  25. local yStart = y
  26. -- Draw the box around the menu
  27. cosUtils.drawBox(win, x, yStart, boxWidth, boxHeight)
  28.  
  29. -- Print the menu items inside the box
  30. for i, item in ipairs(items) do
  31. local text = item
  32. local xPos = math.floor((w - #text) / 2)
  33. yStart = margins and yStart or yStart - 1
  34. win.setCursorPos(xPos, yStart + i)
  35. win.setBackgroundColor(col)
  36.  
  37. if slow then
  38. textutils.slowWrite(text, 9)
  39. else
  40. win.write(text)
  41. end
  42. end
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement