DabDaddy6223

jukebox_frontend_main

Jun 19th, 2026 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. -- API --------------------------------------------------
  2.  
  3. os.pullEvent = os.pullEventRaw
  4.  
  5. local w, h = term.getSize()
  6.  
  7. function printCentered(y, str)
  8.    local x = math.floor((w - string.len(str)) / 2)
  9.    term.setCursorPos(x, y)
  10.    term.clearLine()
  11.    term.write(str)
  12. end
  13.  
  14. function printThird(y, third, str)
  15.     local third_width = w / 3
  16.  
  17.     -- Centre position of the requested third
  18.     local centre_x = (third - 0.5) * third_width
  19.  
  20.     local x = math.floor(centre_x - (#str / 2))
  21.  
  22.     term.setCursorPos(x, y)
  23.     term.write(str)
  24. end
  25.  
  26. function hasValue (tab, val)
  27.     for index, value in ipairs(tab) do
  28.         if value == val then
  29.             return true
  30.         end
  31.     end
  32.  
  33.     return false
  34. end
  35.  
  36. function printKeys(tab)
  37.     -- Source - https://stackoverflow.com/a/12674376
  38.     -- Posted by lhf
  39.     -- Retrieved 2026-06-11, License - CC BY-SA 3.0
  40.  
  41.     local keyset={}
  42.     local n=0
  43.  
  44.     for k,v in pairs(tab) do
  45.         n=n+1
  46.         keyset[n]=k
  47.     end
  48.  
  49.     print(textutils.serialise(keyset))
  50. end
  51.  
  52. ---------------------------------------------------------
  53.  
  54. JUKEBOX_ID = -1
  55. JUKEBOX_BACKEND_ID = -1
  56.  
  57. local menu_option = 1
  58.  
  59. local function drawMenu()
  60.     term.clear()
  61.     term.setCursorPos(1, 1)
  62.     term.write("Jukebox OS Alpha //")
  63.  
  64.     term.setCursorPos(w - 10, 1)
  65.     if menu_option == 1 then
  66.         term.write("Song List")
  67.     elseif menu_option == 2 then
  68.         term.write("Queue")
  69.     elseif menu_option == 3 then
  70.         term.write("Maintenance")
  71.     elseif menu_option == 4 then
  72.         term.write("Shutdown")
  73.     end
  74. end
  75.  
  76. local function drawFrontend()
  77.     printCentered(math.floor(h/2) - 3, "")
  78.     printCentered(math.floor(h/2) - 2, "Start Menu" )
  79.     printCentered(math.floor(h/2) - 1, "")
  80.     printCentered(math.floor(h/2) + 0, ((menu_option == 1) and "[ Song List   ]") or "Song List  ")
  81.     printCentered(math.floor(h/2) + 1, ((menu_option == 2) and "[ Queue       ]") or "Queue      ")
  82.     printCentered(math.floor(h/2) + 2, ((menu_option == 3) and "[ Maintenance ]") or "Maintenance")
  83.     printCentered(math.floor(h/2) + 3, ((menu_option == 4) and "[ Shutdown    ]") or "Shutdown   ")
  84.     printCentered(math.floor(h/2) + 4, "")
  85. end
  86.  
  87. function main(...)
  88.     local jukebox_id, jukebox_backend_id = ...
  89.     JUKEBOX_ID = tonumber(jukebox_id)
  90.     JUKEBOX_BACKEND_ID = tonumber(jukebox_backend_id)
  91.  
  92.     drawMenu()
  93.     drawFrontend()
  94.  
  95.     while true do
  96.         local e, p = os.pullEvent()
  97.         if e == "key" then
  98.             local key = p
  99.             if key == keys.w or key == keys.up then
  100.                 if menu_option > 1 then
  101.                     menu_option = menu_option - 1
  102.                     drawMenu()
  103.                     drawFrontend()
  104.                 end
  105.             elseif key == keys.s or key == keys.down then
  106.                 if menu_option < 4 then
  107.                     menu_option = menu_option + 1
  108.                     drawMenu()
  109.                     drawFrontend()
  110.                 end
  111.             elseif key == keys.enter then
  112.                 break
  113.             end
  114.         end
  115.     end
  116.  
  117.     term.clear()
  118.  
  119.     if menu_option == 1 then
  120.         shell.run("jukebox_frontend_songlist", JUKEBOX_ID, JUKEBOX_BACKEND_ID)
  121.     elseif menu_option == 2 then
  122.         shell.run("jukebox_frontend_queue", JUKEBOX_ID, JUKEBOX_BACKEND_ID)
  123.     elseif menu_option == 3 then
  124.     elseif menu_option == 4 then
  125.         os.shutdown()
  126.     end
  127. end
  128.  
  129. main(...)
Advertisement
Add Comment
Please, Sign In to add comment