Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- ---Desc: A utility program which lists the peripherals
- ---Which the computer can detect
- ---via wired modems
- ---
- --By: Spank
- -- Get the peripheral name from the command line argument
- local args = {...}
- if #args < 1 then
- print("Usage: script <peripheral_name>")
- return
- end
- local chestName = args[1]
- -- Wrap the specified peripheral
- local chest = peripheral.wrap(chestName)
- if not chest then
- print("Error: Peripheral not found.")
- return
- end
- -- Function to print output with pagination
- local function printWithPagination(output)
- local lines = {}
- for line in output:gmatch("[^\n]+") do
- table.insert(lines, line)
- end
- local currentPage = 1
- local pageSize = 5
- local totalPages = math.ceil(#lines / pageSize)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- for i = 1, pageSize do
- local lineIndex = (currentPage - 1) * pageSize + i
- if lineIndex <= #lines then
- print(lines[lineIndex])
- else
- break
- end
- end
- print(("Page %d/%d. Page Up/Down, Home/End to navigate. Press 'x' to exit."):format(currentPage, totalPages))
- local event, key = os.pullEvent("key")
- if key == keys.pageUp and currentPage > 1 then
- currentPage = currentPage - 1
- elseif key == keys.pageDown and currentPage < totalPages then
- currentPage = currentPage + 1
- elseif key == keys.home then
- currentPage = 1
- elseif key == keys["end"] then
- currentPage = totalPages
- elseif key == keys.x then
- break
- end
- end
- end
- -- Get the items from the chest and format them
- local output = {}
- for slot, item in pairs(chest.list()) do
- table.insert(output, ("%d x %s in slot %d"):format(item.count, item.name, slot))
- end
- -- Print output with pagination
- printWithPagination(table.concat(output, "\n"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement