Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- API --------------------------------------------------
- os.pullEvent = os.pullEventRaw
- local w, h = term.getSize()
- function printCentered(y, str)
- local x = math.floor((w - string.len(str)) / 2)
- term.setCursorPos(x, y)
- term.clearLine()
- term.write(str)
- end
- function printThird(y, third, str)
- local third_width = w / 3
- -- Centre position of the requested third
- local centre_x = (third - 0.5) * third_width
- local x = math.floor(centre_x - (#str / 2))
- term.setCursorPos(x, y)
- term.write(str)
- end
- function hasValue (tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- function printKeys(tab)
- -- Source - https://stackoverflow.com/a/12674376
- -- Posted by lhf
- -- Retrieved 2026-06-11, License - CC BY-SA 3.0
- local keyset={}
- local n=0
- for k,v in pairs(tab) do
- n=n+1
- keyset[n]=k
- end
- print(textutils.serialise(keyset))
- end
- ---------------------------------------------------------
- local jukebox_id, jukebox_backend_id = ...
- JUKEBOX_ID = tonumber(jukebox_id)
- JUKEBOX_BACKEND_ID = tonumber(jukebox_backend_id)
- local minecraft_songs = {}
- local doctor_who_songs = {}
- local other_songs = {}
- local menu_option = 1
- local function drawMenu()
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Jukebox OS Alpha //")
- term.setCursorPos(w - 16, 1)
- if menu_option == 1 then
- term.write("Minecraft Songs")
- elseif menu_option == 2 then
- term.write("Doctor Who Songs")
- elseif menu_option == 3 then
- term.write("Other Songs")
- elseif menu_option == 4 then
- term.write("Exit")
- end
- end
- local function drawFrontend()
- printCentered(math.floor(h/2) - 3, "")
- printCentered(math.floor(h/2) - 2, "Song List" )
- printCentered(math.floor(h/2) - 1, "")
- printCentered(math.floor(h/2) + 0, ((menu_option == 1) and "[ Minecraft Songs ]") or "Minecraft Songs ")
- printCentered(math.floor(h/2) + 1, ((menu_option == 2) and "[ Doctor Who Songs ]") or "Doctor Who Songs")
- printCentered(math.floor(h/2) + 2, ((menu_option == 3) and "[ Other Songs ]") or "Other Songs ")
- printCentered(math.floor(h/2) + 3, ((menu_option == 4) and "[ Exit ]") or "Exit ")
- printCentered(math.floor(h/2) + 4, "")
- end
- local function printSongCenteredOnDash(y, song)
- local title, artist = song:match("^(.-)%s%-%s(.+)$")
- if not title then
- printCentered(y, song)
- return
- end
- local centre_x = math.floor(w / 2)
- -- Print title so it ends just before the dash
- term.setCursorPos(centre_x - #title - 1, y)
- term.write(title)
- -- Print dash
- term.setCursorPos(centre_x, y)
- term.write("-")
- -- Print artist
- term.setCursorPos(centre_x + 2, y)
- term.write(artist)
- end
- local function drawMinecraftSongsList()
- drawMenu()
- printCentered(3, "Minecraft Songs")
- -- Display song names
- for i, song in ipairs(minecraft_songs) do
- printSongCenteredOnDash(3 + i, song)
- end
- -- Display exit button
- printCentered(h - 1, "[ Exit ]")
- end
- local function drawDoctorWhoSongsList()
- drawMenu()
- printCentered(3, "Doctor Who Songs")
- -- Display song names
- for i, song in ipairs(doctor_who_songs) do
- printSongCenteredOnDash(3 + i, song)
- end
- -- Display exit button
- printCentered(h - 1, "[ Exit ]")
- end
- local function drawOtherSongsList()
- drawMenu()
- printCentered(3, "Other Songs")
- -- Display song names
- for i, song in ipairs(other_songs) do
- printSongCenteredOnDash(3 + i, song)
- end
- -- Display exit button
- printCentered(h - 1, "[ Exit ]")
- end
- local function gatherSongData()
- local song_list = fs.find("songs/*/*.json")
- for _, song_path in ipairs(song_list) do
- -- Decode data
- local song_file = fs.open(song_path, "r")
- local song_contents = song_file.readAll()
- song_file.close()
- local song_data = textutils.unserializeJSON(song_contents)
- local song_name = song_data["display_name"] .. " - " .. song_data["artist"]
- -- Add to appropriate songs list
- if fs.getDir(song_path) == "songs/minecraft" then
- minecraft_songs[#minecraft_songs + 1] = song_name
- elseif fs.getDir(song_path) == "songs/doctor_who" then
- doctor_who_songs[#doctor_who_songs + 1] = song_name
- elseif fs.getDir(song_path) == "songs/other" then
- other_songs[#other_songs + 1] = song_name
- end
- end
- end
- local function displaySongsListArea()
- drawMenu()
- drawFrontend()
- while true do
- local e, p = os.pullEvent()
- if e == "key" then
- local key = p
- if key == keys.w or key == keys.up then
- if menu_option > 1 then
- menu_option = menu_option - 1
- drawMenu()
- drawFrontend()
- end
- elseif key == keys.s or key == keys.down then
- if menu_option < 4 then
- menu_option = menu_option + 1
- drawMenu()
- drawFrontend()
- end
- elseif key == keys.enter then
- break
- end
- end
- end
- term.clear()
- if menu_option == 1 then
- drawMinecraftSongsList()
- while true do
- local e, p = os.pullEvent()
- if e == "key" then
- local key = p
- if key == keys.enter then
- break
- end
- end
- end
- displaySongsListArea()
- elseif menu_option == 2 then
- drawDoctorWhoSongsList()
- while true do
- local e, p = os.pullEvent()
- if e == "key" then
- local key = p
- if key == keys.enter then
- break
- end
- end
- end
- displaySongsListArea()
- elseif menu_option == 3 then
- drawOtherSongsList()
- while true do
- local e, p = os.pullEvent()
- if e == "key" then
- local key = p
- if key == keys.enter then
- break
- end
- end
- end
- displaySongsListArea()
- elseif menu_option == 4 then
- shell.run("jukebox_frontend", JUKEBOX_ID, JUKEBOX_BACKEND_ID)
- end
- end
- function main()
- gatherSongData()
- displaySongsListArea()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment