Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------
- -- Download APIs script --
- --------------------------
- if not http then
- print("This is a script that allows the download of APIs.")
- return
- end
- local tAPIs = {}
- local topString = "-- This portion of the startup script was automatically added by the Download APIs script. --"
- local startupString = [[
- -- Load APIs --
- local apis = "apis"
- if fs.isDir(apis) then
- local folder = fs.list(apis)
- if #folder > 0 then
- print("Loading APIs...")
- for k, v in ipairs(folder) do
- os.loadAPI(apis.."/"..v)
- end
- end
- end
- -- Set program aliases --
- local programs = "programs"
- if fs.isDir(programs) then
- local folder = fs.list(programs)
- if #folder > 0 then
- print("Aliasing programs...")
- for k, v in ipairs(folder) do
- shell.setAlias(apis.."/"..v)
- end
- end
- end
- -- And now back to your startup script. --
- ]]
- -- Set Up Variables --
- local scrX, scrY = term.getSize()
- local menuTitleColor = colors.lightBlue
- local menuBGColor = colors.gray
- local menuBGSelectionColor = colors.blue
- local menuScrollColor = colors.white
- local menuBGScrollColor = colors.lightGray
- function drawMenuFront(Title, ControlBarText)
- term.setCursorPos(1, 1)
- term.setBackgroundColor(menuTitleColor)
- term.clearLine()
- term.write(Title)
- ControlBarText = ControlBarText or "Select an option then press Enter."
- term.setCursorPos(1, scrY + 1 - math.ceil(#ControlBarText/scrX))
- term.setBackgroundColor(menuTitleColor)
- term.clearLine()
- term.write(ControlBarText)
- end
- -- Draws a menu prompt.
- function drawMenu(Title, Options, Scroll, Selected)
- local y = 1
- for k=math.max(Scroll + 1, 1), math.min(Scroll + 1 + scrY - 2, #Options) do
- y = y + 1
- term.setCursorPos(1, y)
- if Selected == k then
- term.setBackgroundColor(menuBGSelectionColor)
- else
- term.setBackgroundColor(menuBGColor)
- end
- term.clearLine()
- if type(Options[k]) == "table" then
- term.write(Options[k]["Name"] or Options[k][1])
- else
- term.write(Options[k])
- end
- end
- if #Options > scrY - 2 then -- Draw a scroll bar
- term.setBackgroundColor(menuBGScrollColor)
- for k=2, scrY - 1 do
- term.setCursorPos(scrX, k)
- term.write(" ")
- end
- term.setBackgroundColor(menuScrollColor)
- local barHeight = (scrY - 2)/(#Options - (scrY - 2))
- local barPos = math.max((Scroll/(#Options - (scrY - 2)))*(scrY - 1 - barHeight), 1) + 1
- for k=barPos, barPos + barHeight do
- term.setCursorPos(scrX, k)
- term.write(" ")
- end
- end
- drawMenuFront(Title, ControlBar)
- end
- -- Shows a menu prompt.
- function menuPrompt(Title, Options)
- term.setBackgroundColor(menuBGColor)
- term.clear()
- if not SuppressCancel then
- if tostring(Options[#Options]) ~= "Cancel" then
- Options[#Options + 1] = "Cancel"
- end
- end
- local Scroll, Selected = 0, 1
- while true do
- if #Options > scrY - 2 then
- Scroll = math.min(math.max(Selected - math.ceil((scrY - 2)*.5), 0), #Options - (scrY - 2))
- else
- Scroll = 0
- end
- drawMenu(Title, Options, Scroll, Selected)
- local event, key, clx, cly = os.pullEvent()
- if event == "key" then
- if key == keys.up then
- Selected = math.max(Selected - 1, 1)
- elseif key == keys.down then
- Selected = math.min(Selected + 1, #Options)
- elseif key == keys.pageUp then
- Selected = math.max(Selected - (height - 1), 1)
- elseif key == keys.pageDown then
- Selected = math.min(Selected + (height - 1), #Options)
- elseif key == keys.home then
- Selected = 1
- elseif key == keys["end"] then
- Selected = #Options
- elseif key == keys.f5 then
- -- Refresh
- elseif(key == keys.tab) then
- term.setCursorBlink(false)
- return false
- elseif key == (keys.enter or 28) then
- if Selected > #Options - 1 and not SuppressCancel then
- return false
- else
- return Selected
- end
- end
- elseif event == "mouse_click" then
- if cly > 1 and cly < scrY then
- local Clicked = Scroll + cly - 1
- if Clicked <= #Options then
- if Clicked == Selected then
- if Selected > #Options - 1 and not SuppressCancel then
- return false
- else
- return Selected
- end
- else
- Selected = Clicked
- end
- end
- end
- end
- end
- end
- function testIfMainElement(str)
- if string.lower(str) == "description" then
- return true
- elseif string.lower(str) == "body" then
- return true
- elseif string.lower(str) == "message" then
- return true
- elseif string.lower(str) == "details" then
- return true
- elseif string.lower(str) == "code" then
- return true
- end
- return false
- end
- function testIfVisibleElement(str)
- if string.lower(str) == "name" then
- return false
- elseif string.lower(str) == "title" then
- return false
- elseif string.lower(str) == "id" then
- return false
- elseif string.lower(str) == "turtle api" then
- return false
- elseif string.lower(str) == "command api" then
- return false
- end
- return true
- end
- function drawDescriptionMenu(Title, ControlBarText, TableElements, Layout, MainElement)
- term.setBackgroundColor(menuBGColor)
- term.clear()
- local currentRow = 0
- for k, v in ipairs(Layout) do
- if not v["MainElement"] and v["Visible"] then -- If it is not the MainElement and it is visible
- currentRow = currentRow + 1
- term.setCursorPos(1, currentRow + 1)
- term.write((v["Name"] or v[1])..": ")
- term.setCursorPos(#(v[1]..": "), currentRow + 1)
- term.write(tostring(TableElements[v[1]]))
- end
- end
- currentRow = currentRow + 1
- term.setCursorPos(1, currentRow + 1)
- term.write(Layout[MainElement]["Name"] or Layout[MainElement][1])
- term.setCursorPos(1, currentRow + 2)
- print(tostring(TableElements[Layout[MainElement][1]]))
- drawMenuFront(Title, "Press Enter to confirm or Tab to return.")
- end
- -- Draws a frame of a description menu.
- function descriptionMenuWindow(Title, ControlBarText, TableElements, Layout)
- local MainElement = 0
- if type(Layout) ~= "table" then
- Layout = {}
- for k, v in pairs(TableElements) do
- Layout[#Layout + 1] = {k}
- if testIfMainElement(k) then
- MainElement = #Layout
- Layout[#Layout]["MainElement"] = true
- else
- Layout[#Layout]["MainElement"] = false
- end
- Layout[#Layout]["Visible"] = testIfVisibleElement(Layout[#Layout][1])
- end
- if not MainElement then
- MainElement = #Layout
- Layout[#Layout]["MainElement"] = true
- end
- else
- for k, v in pairs(Layout) do
- if v[2] then
- MainElement = #Layout
- break
- end
- end
- end
- drawDescriptionMenu(Title, ControlBarText, TableElements, Layout, MainElement)
- end
- function showAPIMenu(selectedAPI)
- descriptionMenuWindow(tAPIs[selectedAPI]["Name"], selectedAPI, tAPIs[selectedAPI], Layout)
- while true do
- local event, key = os.pullEvent("key")
- if(key == keys.tab) then
- term.setCursorBlink(false)
- return false
- elseif key == (keys.enter or 28) then
- return true
- end
- end
- end
- function overwriteFileRaw(contents, destination)
- local file, err = fs.open(destination, "w")
- if file then
- file.write(contents)
- file.close()
- return true
- end
- return false, err
- end
- function overwriteFile(contents, destination)
- local pcallSuccess, returnCondition, errorString = pcall(overwriteFileRaw, contents, destination)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- function pullFileRaw(url, header, post)
- if http then
- local handle, str
- if not post then
- handle, str = http.get(url, header), ""
- else
- handle, str = http.post(url, post, header), ""
- end
- if handle then
- str = handle.readAll()
- handle.close()
- return str
- end
- return false, str
- end
- return false, "Http API is not enabled"
- end
- function pullFile(url, header, post)
- local pcallSuccess, returnCondition, errorString = pcall(pullFileRaw, url, header, post)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- for k=1, 4 do
- tAPIs = textutils.unserialize(pullFile("https://pastebin.com/raw/Eare8WYE"))
- if tAPIs then
- break
- end
- end
- if not tAPIs then
- error("There was a problem while loading the API list.")
- end
- function downloadFileRaw(url, destination, header, post)
- if http then
- local handle, str
- if not post then
- handle, str = http.get(url, header), ""
- else
- handle, str = http.post(url, post, header), ""
- end
- if handle then
- str = handle.readAll()
- handle.close()
- return overwriteFile(str, destination)
- end
- return str
- end
- return false, "Http API is not enabled"
- end
- function downloadFile(url, destination, header, post)
- local pcallSuccess, returnCondition, errorString = pcall(downloadFileRaw, url, destination, header, post)
- if pcallSuccess then
- return returnCondition, errorString
- end
- return false, returnCondition
- end
- function setUpStartup()
- local file, str = fs.open("startup", "r"), ""
- if file then
- str = file.readAll()
- file.close()
- end
- if not string.find(str, topString) then
- file = fs.open("startup", "w")
- if file then
- file.writeLine(topString.."\n")
- file.writeLine(startupString.."\n")
- file.writeLine(str)
- file.close()
- end
- print("Your startup script has been automatically changed.")
- end
- end
- while true do
- local selectedAPI = menuPrompt("API list", tAPIs)
- if not selectedAPI then term.setBackgroundColor(colors.black) term.clear() return term.setCursorPos(1, 1) end
- if showAPIMenu(selectedAPI) then
- term.clear()
- term.setCursorPos(1, 1)
- --print("Save to where? To automatically load this API on startup leave this space empty.")
- local name = ""
- term.write("Downloading File...")
- local response, err
- if #name > 0 then
- response, err = downloadFile("http://pastebin.com/raw.php?i="..tAPIs[selectedAPI]["ID"], name)
- else
- fs.makeDir("apis")
- response, err = downloadFile("http://pastebin.com/raw.php?i="..tAPIs[selectedAPI]["ID"], "apis/"..(tAPIs[selectedAPI]["DefaultName"] or tAPIs[selectedAPI]["Name"]))
- setUpStartup()
- if tAPIs[selectedAPI]["SampleApplications"] then
- fs.makeDir("programs")
- for k, v in ipairs(tAPIs[selectedAPI]["SampleApplications"]) do
- downloadFile("http://pastebin.com/raw.php?i="..v[1], "programs/"..(tAPIs[selectedAPI]["DefaultName"] or tAPIs[selectedAPI]["Name"]))
- end
- end
- end
- if not response then
- print(err)
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement