DabDaddy6223

jukebox_frontend_songlist

Jun 19th, 2026 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.64 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. local jukebox_id, jukebox_backend_id = ...
  55.  
  56. JUKEBOX_ID = tonumber(jukebox_id)
  57. JUKEBOX_BACKEND_ID = tonumber(jukebox_backend_id)
  58.  
  59. local minecraft_songs = {}
  60. local doctor_who_songs = {}
  61. local other_songs = {}
  62.  
  63. local menu_option = 1
  64.  
  65. local function drawMenu()
  66.     term.clear()
  67.     term.setCursorPos(1, 1)
  68.     term.write("Jukebox OS Alpha //")
  69.  
  70.     term.setCursorPos(w - 16, 1)
  71.     if menu_option == 1 then
  72.         term.write("Minecraft Songs")
  73.     elseif menu_option == 2 then
  74.         term.write("Doctor Who Songs")
  75.     elseif menu_option == 3 then
  76.         term.write("Other Songs")
  77.     elseif menu_option == 4 then
  78.         term.write("Exit")
  79.     end
  80. end
  81.  
  82. local function drawFrontend()
  83.     printCentered(math.floor(h/2) - 3, "")
  84.     printCentered(math.floor(h/2) - 2, "Song List" )
  85.     printCentered(math.floor(h/2) - 1, "")
  86.     printCentered(math.floor(h/2) + 0, ((menu_option == 1) and "[ Minecraft Songs  ]") or "Minecraft Songs ")
  87.     printCentered(math.floor(h/2) + 1, ((menu_option == 2) and "[ Doctor Who Songs ]") or "Doctor Who Songs")
  88.     printCentered(math.floor(h/2) + 2, ((menu_option == 3) and "[ Other Songs      ]") or "Other Songs     ")
  89.     printCentered(math.floor(h/2) + 3, ((menu_option == 4) and "[ Exit             ]") or "Exit            ")
  90.     printCentered(math.floor(h/2) + 4, "")
  91. end
  92.  
  93. local function printSongCenteredOnDash(y, song)
  94.     local title, artist = song:match("^(.-)%s%-%s(.+)$")
  95.  
  96.     if not title then
  97.         printCentered(y, song)
  98.         return
  99.     end
  100.  
  101.     local centre_x = math.floor(w / 2)
  102.  
  103.     -- Print title so it ends just before the dash
  104.     term.setCursorPos(centre_x - #title - 1, y)
  105.     term.write(title)
  106.  
  107.     -- Print dash
  108.     term.setCursorPos(centre_x, y)
  109.     term.write("-")
  110.  
  111.     -- Print artist
  112.     term.setCursorPos(centre_x + 2, y)
  113.     term.write(artist)
  114. end
  115.  
  116. local function drawMinecraftSongsList()
  117.     drawMenu()
  118.     printCentered(3, "Minecraft Songs")
  119.  
  120.     -- Display song names
  121.     for i, song in ipairs(minecraft_songs) do
  122.         printSongCenteredOnDash(3 + i, song)
  123.     end
  124.  
  125.     -- Display exit button
  126.     printCentered(h - 1, "[ Exit ]")
  127. end
  128.  
  129. local function drawDoctorWhoSongsList()
  130.     drawMenu()
  131.     printCentered(3, "Doctor Who Songs")
  132.  
  133.     -- Display song names
  134.     for i, song in ipairs(doctor_who_songs) do
  135.         printSongCenteredOnDash(3 + i, song)
  136.     end
  137.  
  138.     -- Display exit button
  139.     printCentered(h - 1, "[ Exit ]")
  140. end
  141.  
  142. local function drawOtherSongsList()
  143.     drawMenu()
  144.     printCentered(3, "Other Songs")
  145.  
  146.     -- Display song names
  147.     for i, song in ipairs(other_songs) do
  148.         printSongCenteredOnDash(3 + i, song)
  149.     end
  150.  
  151.     -- Display exit button
  152.     printCentered(h - 1, "[ Exit ]")
  153. end
  154.  
  155. local function gatherSongData()
  156.     local song_list = fs.find("songs/*/*.json")
  157.     for _, song_path in ipairs(song_list) do
  158.         -- Decode data
  159.         local song_file = fs.open(song_path, "r")
  160.         local song_contents = song_file.readAll()
  161.         song_file.close()
  162.  
  163.         local song_data = textutils.unserializeJSON(song_contents)
  164.         local song_name = song_data["display_name"] .. " - " .. song_data["artist"]
  165.        
  166.         -- Add to appropriate songs list
  167.         if fs.getDir(song_path) == "songs/minecraft" then
  168.             minecraft_songs[#minecraft_songs + 1] = song_name
  169.         elseif fs.getDir(song_path) == "songs/doctor_who" then
  170.             doctor_who_songs[#doctor_who_songs + 1] = song_name
  171.         elseif fs.getDir(song_path) == "songs/other" then
  172.             other_songs[#other_songs + 1] = song_name
  173.         end
  174.     end
  175. end
  176.  
  177. local function displaySongsListArea()
  178.     drawMenu()
  179.     drawFrontend()
  180.  
  181.     while true do
  182.         local e, p = os.pullEvent()
  183.         if e == "key" then
  184.             local key = p
  185.             if key == keys.w or key == keys.up then
  186.                 if menu_option > 1 then
  187.                     menu_option = menu_option - 1
  188.                     drawMenu()
  189.                     drawFrontend()
  190.                 end
  191.             elseif key == keys.s or key == keys.down then
  192.                 if menu_option < 4 then
  193.                     menu_option = menu_option + 1
  194.                     drawMenu()
  195.                     drawFrontend()
  196.                 end
  197.             elseif key == keys.enter then
  198.                 break
  199.             end
  200.         end
  201.     end
  202.  
  203.     term.clear()
  204.  
  205.     if menu_option == 1 then
  206.         drawMinecraftSongsList()
  207.         while true do
  208.             local e, p = os.pullEvent()
  209.             if e == "key" then
  210.                 local key = p
  211.                 if key == keys.enter then
  212.                     break
  213.                 end
  214.             end
  215.         end
  216.         displaySongsListArea()
  217.     elseif menu_option == 2 then
  218.         drawDoctorWhoSongsList()
  219.         while true do
  220.             local e, p = os.pullEvent()
  221.             if e == "key" then
  222.                 local key = p
  223.                 if key == keys.enter then
  224.                     break
  225.                 end
  226.             end
  227.         end
  228.         displaySongsListArea()
  229.     elseif menu_option == 3 then
  230.         drawOtherSongsList()
  231.         while true do
  232.             local e, p = os.pullEvent()
  233.             if e == "key" then
  234.                 local key = p
  235.                 if key == keys.enter then
  236.                     break
  237.                 end
  238.             end
  239.         end
  240.         displaySongsListArea()
  241.     elseif menu_option == 4 then
  242.         shell.run("jukebox_frontend", JUKEBOX_ID, JUKEBOX_BACKEND_ID)
  243.     end
  244. end
  245.  
  246. function main()
  247.     gatherSongData()
  248.     displaySongsListArea()
  249. end
  250.  
  251. main()
Advertisement
Add Comment
Please, Sign In to add comment