Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function drawButton(x, y, width, height, text)
- paintutils.drawFilledBox(x, y, x + width - 1, y + height - 1, colors.lightGray)
- local textX = x + math.floor((width - #text) / 2)
- local textY = y + math.floor(height / 2)
- term.setCursorPos(textX, textY)
- term.setTextColor(colors.black)
- term.write(text)
- end
- local function drawExitButton()
- local width, height = term.getSize()
- term.setCursorPos(width, 1)
- term.setTextColor(colors.red)
- term.write("X")
- end
- local function isInArea(x, y, xMin, yMin, xMax, yMax)
- return x >= xMin and x <= xMax and y >= yMin and y <= yMax
- end
- local function getDiskID()
- local drive = peripheral.wrap("bottom")
- if drive and peripheral.getType("bottom") == "drive" then
- if drive.isDiskPresent() then
- local diskID = drive.getDiskID()
- return diskID
- else
- return nil, "No disk present in the drive."
- end
- else
- return nil, "No drive found at the bottom."
- end
- end
- local function listCards()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- local cardDir = "/disk/Cards/"
- local files = fs.list(cardDir)
- if #files > 0 then
- print("Cards in /disk/Cards:")
- for _, file in ipairs(files) do
- print("- " .. file)
- end
- else
- print("No cards found in /disk/Cards.")
- end
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, width, 1, width, 1) then
- return
- end
- end
- end
- local function linkCard()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- print("Link Card:")
- drawButton(2, 2, 12, 2, "Insert Card")
- drawButton(2, 5, 12, 2, "Enter ID")
- drawButton(2, 8, 12, 2, "Back")
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- print("Insert a disk to link...")
- local diskID, errorMsg = getDiskID()
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- else
- print(errorMsg)
- end
- elseif isInArea(x, y, 2, 5, 13, 6) then
- term.setCursorPos(1, 10)
- term.clearLine()
- print("Enter the card ID:")
- term.setCursorPos(1, 11)
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- local file = fs.open(cardFileName, "w")
- file.close()
- print("Card linked successfully.")
- elseif isInArea(x, y, 2, 8, 13, 9) then
- return
- elseif isInArea(x, y, width, 1, width, 1) then
- return
- end
- end
- end
- local function unlinkCard()
- term.setCursorPos(1, 1)
- term.clear()
- drawExitButton()
- print("Unlink Card:")
- drawButton(2, 2, 12, 2, "Insert Card")
- drawButton(2, 5, 12, 2, "Enter ID")
- drawButton(2, 8, 12, 2, "Back")
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- print("Insert a disk to unlink...")
- local diskID, errorMsg = getDiskID()
- if diskID then
- local cardFileName = "/disk/Cards/" .. diskID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- else
- print(errorMsg)
- end
- elseif isInArea(x, y, 2, 5, 13, 6) then
- term.setCursorPos(1, 10)
- term.clearLine()
- print("Enter the card ID:")
- term.setCursorPos(1, 11)
- local cardID = read()
- local cardFileName = "/disk/Cards/" .. cardID .. ".ID"
- if fs.exists(cardFileName) then
- fs.delete(cardFileName)
- print("Card unlinked successfully.")
- else
- print("No matching card found to unlink.")
- end
- elseif isInArea(x, y, 2, 8, 13, 9) then
- return
- elseif isInArea(x, y, width, 1, width, 1) then
- return
- end
- end
- end
- local function mainMenu()
- term.clear()
- drawExitButton()
- drawButton(2, 2, 12, 2, "List Cards")
- drawButton(2, 5, 12, 2, "Link Card")
- drawButton(2, 8, 12, 2, "Unlink Card")
- drawButton(2, 11, 12, 2, "Exit")
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- local width, _ = term.getSize()
- if isInArea(x, y, 2, 2, 13, 3) then
- listCards()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 5, 13, 6) then
- linkCard()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 8, 13, 9) then
- unlinkCard()
- term.clear()
- mainMenu()
- elseif isInArea(x, y, 2, 11, 13, 12) then
- print("Exiting...")
- break
- elseif isInArea(x, y, width, 1, width, 1) then
- term.clear()
- mainMenu()
- end
- end
- end
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement