Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pastebin Developer Utility App (apps/pastebin.lua)
- return {
- name = "Pastebin",
- color = colors.orange,
- symbol = "P",
- page = 1,
- run = function(w, h, drawBottomBar)
- local tab = "GET" -- "GET" or "PUT"
- local statusMsg = ""
- local statusColor = colors.white
- -- Prompt Input Helper
- local function promptInput(title, defaultVal)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(2, 17)
- term.write(string.rep(" ", w - 2))
- term.setCursorPos(2, 17)
- term.write(title .. ": ")
- term.setTextColor(colors.yellow)
- local input = read()
- if input and #input > 0 then
- return input
- end
- return defaultVal or ""
- end
- local getCode = ""
- local getFilename = "newapp.lua"
- local putFilename = ""
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- -- 1. Top Header
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write(" Pastebin Dev Tool " .. string.rep(" ", w - 19))
- -- 2. Mode Selector Tabs (Replaced darkGray with gray)
- term.setCursorPos(2, 3)
- term.setBackgroundColor(tab == "GET" and colors.blue or colors.gray)
- term.setTextColor(colors.white)
- term.write(" [ GET Code ] ")
- term.setCursorPos(15, 3)
- term.setBackgroundColor(tab == "PUT" and colors.green or colors.gray)
- term.setTextColor(colors.white)
- term.write(" [ UPLOAD File ] ")
- -- 3. Render GET Interface
- if tab == "GET" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.setCursorPos(2, 5)
- term.write("Pastebin Code / Key:")
- term.setCursorPos(2, 7)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write(string.format(" Key: %-16s ", getCode == "" and "Tap to set" or getCode))
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.setCursorPos(2, 9)
- term.write("Save File Name (in /apps):")
- term.setCursorPos(2, 11)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write(string.format(" File: %-15s ", getFilename))
- -- Execute Download Button
- term.setCursorPos(2, 14)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.write(" [ Download File ] ")
- -- 4. Render UPLOAD Interface
- elseif tab == "PUT" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.setCursorPos(2, 5)
- term.write("Local File to Upload:")
- term.setCursorPos(2, 7)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.write(string.format(" Path: %-15s ", putFilename == "" and "Tap to set" or putFilename))
- -- Execute Upload Button
- term.setCursorPos(2, 10)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.white)
- term.write(" [ Upload to Pastebin ] ")
- end
- -- 5. Status Output Area
- if statusMsg ~= "" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(statusColor)
- term.setCursorPos(2, 16)
- term.write(statusMsg)
- end
- -- 6. Bottom Navigation Bar
- drawBottomBar()
- -- 7. Event Loop
- local event, button, clickX, clickY = os.pullEvent("mouse_click")
- if button == 1 then
- -- Intercept Bottom Navigation Bar Click
- if clickY == h then
- return "bottom_bar", clickX
- end
- -- Switch Tabs
- if clickY == 3 then
- if clickX >= 2 and clickX <= 13 then
- tab = "GET"
- statusMsg = ""
- elseif clickX >= 15 and clickX <= 26 then
- tab = "PUT"
- statusMsg = ""
- end
- end
- -- GET Mode Actions
- if tab == "GET" then
- -- Set Pastebin Key
- if clickY == 7 and clickX >= 2 and clickX <= 24 then
- getCode = promptInput("Pastebin Key", getCode)
- if getCode:find("pastebin.com/") then
- getCode = getCode:match("pastebin.com/([%a%d]+)") or getCode
- end
- end
- -- Set Output File Name
- if clickY == 11 and clickX >= 2 and clickX <= 24 then
- getFilename = promptInput("Filename", getFilename)
- end
- -- Trigger Download
- if clickY == 14 and clickX >= 2 and clickX <= 18 then
- if getCode == "" then
- statusMsg = "Error: Key empty!"
- statusColor = colors.red
- else
- statusMsg = "Downloading..."
- statusColor = colors.yellow
- term.setCursorPos(2, 16)
- term.setTextColor(statusColor)
- term.write(statusMsg)
- local response = http.get("https://pastebin.com/raw/" .. textutils.urlEncode(getCode))
- if response then
- local content = response.readAll()
- response.close()
- -- Ensure target path is inside /apps/
- local savePath = getFilename
- if not savePath:find("^apps/") and not savePath:find("^/apps/") then
- savePath = "apps/" .. savePath
- end
- local file = fs.open(savePath, "w")
- file.write(content)
- file.close()
- statusMsg = "Saved: " .. savePath
- statusColor = colors.lime
- else
- statusMsg = "Fetch failed! Bad key."
- statusColor = colors.red
- end
- end
- end
- -- UPLOAD Mode Actions
- elseif tab == "PUT" then
- -- Set Local File Path
- if clickY == 7 and clickX >= 2 and clickX <= 24 then
- putFilename = promptInput("File Path", putFilename)
- end
- -- Trigger Upload
- if clickY == 10 and clickX >= 2 and clickX <= 22 then
- if putFilename == "" or not fs.exists(putFilename) then
- statusMsg = "Error: File not found!"
- statusColor = colors.red
- elseif fs.isDir(putFilename) then
- statusMsg = "Error: Is a directory!"
- statusColor = colors.red
- else
- statusMsg = "Uploading..."
- statusColor = colors.yellow
- term.setCursorPos(2, 16)
- term.setTextColor(statusColor)
- term.write(statusMsg)
- local file = fs.open(putFilename, "r")
- local content = file.readAll()
- file.close()
- local response = http.post(
- "https://pastebin.com/api/api_post.php",
- "api_option=paste&api_dev_key=1d205e012b189d288d448378f844a476&api_paste_code=" ..
- textutils.urlEncode(content)
- )
- if response then
- local key = response.readAll()
- response.close()
- if key:find("http") then
- key = key:match("pastebin.com/([%a%d]+)") or key
- end
- statusMsg = "Code: " .. key
- statusColor = colors.lime
- else
- statusMsg = "Upload failed!"
- statusColor = colors.red
- end
- end
- end
- end
- end
- end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment