Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Copyright 2014 Jackson McNeill
- --You may edit this program without my permission, as long as copyright info is not altered in any way, but you must ask for and receive Jackson McNeill's permission to distrubute ANY version(s) of this program.
- if OneOS then
- if shell.getRunningProgram() == "startup" then
- --to make sure they're not running this in shell
- --because if they are it'll crash.
- OneOS.ToolBarColour = colours.lightGrey
- end
- end
- --load redirect
- if not redirect then
- os.loadAPI(shell.resolve(string.sub(shell.getRunningProgram(),1,#shell.getRunningProgram()-8)).."/redirect")
- end
- if not sapphUtils then
- os.loadAPI(shell.resolve(string.sub(shell.getRunningProgram(),1,#shell.getRunningProgram()-8)).."/sapphUtils")
- end
- --Credits:
- --Developers:
- --Hithere
- --APIs
- --Redirect by GopherAtl
- --Special thanks to:
- --GopheralAtl For making Redirect, and being a very nice person. He's made stuff
- --for me personally before!
- --Sublime Text 2(yes I edit outside of the game)
- --For being the best text editor yet
- --------------------------------------------------
- sapphariVersionString = "1.0"
- sapphariVersionNumber = 1
- sapphariSites = {}
- sapphariBlacklist = {}
- --Note that this will be okay if the user
- --has a script called pastebin
- --because it runs it directly from rom
- shell.run("rom/programs/http/pastebin run Jg8D7mLi")
- --setting a new homepage:
- --I would REALLY prefer you keep the default one so you can get
- --updates on news and all that stuff and see cool new stuff
- --and sapphari updates, but I'm not going to try to stop you from
- --changing it. Uncomment out the line below and set it to the PasteID
- --sapphariSites["home"] = "insertpagehere"
- local sapphInt = {}
- --for some reason there's a ton of debug sleeps in this version. Since the program is sandboxed, this is an okay solution(there's no sleeps in Sapphari)
- sSleep=sleep
- function sleep() end
- function sapphInt.redrawUI()
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.clear()
- --it's funny how often when GUI coding,
- --ever since advanced computers you
- --don't need to set the cursor pos as much.
- --Kinda nice to clear in one line.
- --But you do have to set background and
- --text colors. Sadface.
- local termX, termY = term.getSize()
- for i=1,2 do
- paintutils.drawLine(1,i,termX,i,colors.lightGray)
- end
- paintutils.drawLine(2,1,termX/3,1,colors.white)
- term.setBackgroundColor(colors.green)
- term.setCursorPos(termX/3+2,1)
- term.write("Refresh")
- --now it's the right length.
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(2,2)
- term.write"Bookmarks | Tabs | Remove"
- end
- --this function will get the data for a site,
- --however, will not run it.
- function sapphInt.getSiteStr(pasteID)
- if type(pasteID) =="string" then
- local responce = http.get("http://pastebin.com/raw.php?i="..pasteID)
- if responce then
- return responce
- end
- end
- end
- function sapphInt.getSite(paste)
- responce = sapphInt.getSiteStr(paste)
- if responce then
- return loadstring(responce.readAll())
- else
- return false
- end
- end
- function sapphInt.handleMenu(typeClick, clickX, clickY,tabs,tabNames,bookmarks)
- --handle the user clicking the menu
- local termX,termY=term.getSize()
- if typeClick == 1 then
- --okay, it's a left click, passes first one'
- if clickX>1 then --okay, in somewhere special
- if clickX<termX/3+1 and clickY==1 then
- --okay, the user wants to change the url.
- term.setCursorPos(2,1)
- term.setBackgroundColor(colors.white)
- return "newsite",sapphInt.read(2,1,termX/3)
- --we will return the input
- elseif clickX<13 and clickY==2 then --bookmarks button
- local otherReturn = sapphInt.dropDownMenu(bookmarks,2,2)
- if otherReturn ==1 then --new bookmark
- return "bookmark",otherReturn
- else
- local newPlace = bookmarks[otherReturn]
- return "newsite",newPlace --this will go to the bookmark!
- end
- elseif clickX>13 and clickX<18 and clickY==2 then -- tab button
- --tabs button
- --print"Tabz"
- --insert the add a tab option
- local otherReturn = sapphInt.dropDownMenu(tabNames,12,2)
- return "tab",otherReturn
- elseif clickX>20 and clickX<27 and clickY==2 then -- remove button
- local stuffRemovable = {}
- stuffRemovable[2] = "Bookmark" stuffRemovable[1] = "Tab" --cool people do it backwards
- local result = sapphInt.dropDownMenu(stuffRemovable, 21,2)
- if result==1 then --if it's a tab we know it's this one
- return "removeTab"
- elseif result ==0 then
- return"nothing"
- else
- local result = sapphInt.dropDownMenu(bookmarks,21,2) --select the book mark removed
- if result ==1 then
- return "nothing"
- elseif result==0 then
- return"nothing"
- else
- table.remove(bookmarks,result)
- local bookMarksWrite = fs.open(shell.resolve(string.sub(shell.getRunningProgram(),1,#shell.getRunningProgram()-8)).."/sapphBookmarks","w")
- bookMarksWrite.writeLine(textutils.serialize(bookmarks))
- bookMarksWrite.close()
- return "rBook", bookmarks
- end
- end
- end
- end
- if clickX>termX/3+1 then
- if clickX<termX/3+8 then
- --need to refresh page nao
- return "newsite",currentSite
- end
- end
- end
- end
- function sapphInt.decodeHumanReadableURL(HRURL)
- --this is where we attempt to decode a
- --human readable url. If fails, will tell
- --calling function to attempt to use the
- --plain url instead.
- if sapphariSites[HRURL] then
- --this must be an HRURL, so we'll give them'
- --the pasteID
- currentSite = sapphariSites[HRURL]
- return sapphariSites[HRURL]
- else
- currentSite = HRURL
- return HRURL
- --this must be a PasteID then
- end
- end
- --this is a really crappy function we don;t
- --want sites using. It's only for very
- --very basic textboxes and you can;t
- --use the arrow keys nor can you make
- --it multiline. Maybe someday I'll fix it up.'
- function sapphInt.read(startx, starty, endx)
- term.setCursorBlink(true)
- local textThing = ""
- local textThingPrint=""
- while true do
- evt, text = os.pullEvent()
- if evt == "key" then
- if text==14 then
- textThingPrint=string.sub(textThingPrint,1,#textThingPrint-1)
- term.setCursorPos(7,7)
- textThing=string.sub(textThing,1,#textThing-1)
- local textThingAdd=string.sub(textThing,#textThing-#textThingPrint,#textThing-#textThingPrint)
- textThingPrint = textThingAdd..textThingPrint
- elseif text==28 then
- term.setCursorBlink(false)
- return textThing
- end
- elseif evt == "char" then
- textThing=textThing..text
- textThingPrint=textThingPrint..text
- end
- term.setCursorPos(startx,starty)
- --format the string to fit the box
- --patch for deleting under max text box
- --length:
- if #textThingPrint ==(endx-startx) then
- textThingPrint = string.sub(textThingPrint,2,#textThingPrint)
- end
- local whitespacez = ""
- for i=1,endx-startx do
- --change that string VV for cool custom stuffz. Only one character, though!
- whitespacez = whitespacez.." "
- end
- term.setCursorPos(startx,starty)
- term.write(whitespacez)
- --reset cursor
- term.setCursorPos(startx,starty)
- term.write(textThingPrint)
- term.setCursorBlink(true)
- end
- end
- --this must get the pasteid, not the human
- --redable url
- function sapphInt.handleWebsite(url)
- local rurl = sapphInt.decodeHumanReadableURL(url)
- local site = sapphInt.getSite(rurl)
- --we make a timer to refresh it
- os.startTimer(0)
- if site then
- return coroutine.create(site)
- else
- return nil
- end
- end
- function sapphInt.dropDownMenu(contents, x, y)
- term.setTextColor(colors.black)
- --YAY! GUI CODING! - Said Nobody, Ever
- --find the longest variable in the table
- local termX,termY=term.getSize()
- term.setBackgroundColor(colors.white)
- --to refresh the menu for the first time
- os.startTimer(0)
- scrollDown = 0
- longestvar = #contents[1]
- for i=1,#contents do
- if #contents[i] > longestvar then
- longestvar = #contents[i]
- end
- end
- while true do
- local evt,keypress,mousex,mousey=os.pullEvent()
- if evt == "key" and termY-y<#contents then
- if keypress == 200 and scrollDown<0 then
- scrollDown=scrollDown+1
- elseif keypress == 208 and (scrollDown*-1)+termY-y<#contents then
- scrollDown=scrollDown-1
- end
- elseif evt=="mouse_click" then
- if mousex>x-1 and mousex<x+longestvar+1 then
- --okay, the xpos is right. What about the
- --ypos?
- if mousey>y-1 and mousey<y+#contents+1 then
- --we need to find the one clicked and minus
- --it by the scroll variable.
- return (mousey-y)-scrollDown
- end
- end
- end
- term.setBackgroundColor(colors.white)
- if #contents<termY-y then
- whiteSpaces = #contents
- else
- whiteSpaces=termY-y
- end
- local whitespace=""
- for i=1,longestvar do
- whitespace=whitespace.." "
- end
- for i=1,whiteSpaces do
- term.setCursorPos(x,i+y)
- print(whitespace)
- end
- term.setCursorPos(x-1,y)
- contents[0] = ""
- for i=1,whiteSpaces do
- setX,setY=term.getCursorPos()
- term.setCursorPos(x,setY+1)
- term.write(contents[i-scrollDown])
- end
- end
- whiteSpaces = nil
- end
- function sapphInt.handleSapphari()
- --fix os.pullEvent
- sapphInt["timesHomed"]=19
- oldPullEvent=os.pullEventRaw
- os.pullEvent=os.pullEventRaw
- local openSites = {}
- local runningSite = 1
- local siteNames = {}
- local redirectBuffers = {}
- local bookMarksRead = fs.open(shell.resolve(string.sub(shell.getRunningProgram(),1,#shell.getRunningProgram()-8)).."/sapphBookmarks","r")
- local bookMarkers = bookMarksRead.readAll()
- bookMarksRead.close()
- local bookMarkers = textutils.unserialize(bookMarkers)
- termX,termY=term.getSize()
- siteNames[1] = "home"
- --a table for open sites.
- --Only the foucused site is ran
- openSites[1] = sapphInt.handleWebsite("home")
- termX,termY=term.getSize()
- redirectBuffers[1] = redirect.createRedirectBuffer(termX,termY-2,colors.black,colors.white,true)
- local normalTerm = term.current()
- sapphInt.redrawUI()
- while true do -- so that they can get to a new site on the event the site dies
- local events = {os.pullEventRaw()}
- if events[1] == "mouse_click" and events[4] <3 then
- local sitesPassed = siteNames
- table.insert(sitesPassed,1,"Add Tab")
- local what, returned = sapphInt.handleMenu(events[2],events[3],events[4],openSites,sitesPassed,bookMarkers)
- table.remove(sitesPassed,1)
- sitesPassed=nil
- if what == "newsite" and returned then
- sapphInt["timesHomed"]=0
- local tempSite = sapphInt.handleWebsite(returned)
- if tempSite then
- openSites[runningSite] = tempSite -- kill the current site and replace it
- siteNames[runningSite] = returned
- end
- elseif what =="tab" then
- sapphInt["timesHomed"]=0
- if returned == 1 then
- --make a new tab
- runningSite = #openSites+1
- openSites[#openSites+1] = sapphInt.handleWebsite("home")
- siteNames[runningSite] = "home"
- redirectBuffers[runningSite] = redirect.createRedirectBuffer(termX,termY-2,colors.black,colors.white,true)
- elseif returned >1 then-- they're switching tabs. the greater than thing
- --ensures it doesn't do this when they're just closing the menu.
- runningSite = returned-1
- end
- elseif what == "bookmark" then
- if returned ==1 then -- safety feature, though already done for us
- bookMarkers[#bookMarkers+1] = siteNames[runningSite]
- local bookMarksWrite = fs.open(shell.resolve(string.sub(shell.getRunningProgram(),1,#shell.getRunningProgram()-8)).."/sapphBookmarks","w")
- bookMarksWrite.writeLine(textutils.serialize(bookMarkers))
- bookMarksWrite.close()
- end
- elseif what == "rBook" then
- bookMarkers = returned
- elseif what =="removeTab" then--it wants us to remove the current tab.
- if #openSites>1 then
- table.remove(openSites,runningSite) -- we'll just remove the tab and it'll be fine
- table.remove(siteNames,runningSite)
- else--they probably want to exit
- break--kills the loop, the function ends, cleanup is ran, then sapphari's coroutine dies and shell is resumed
- end
- end
- elseif events[1] == "terminate" then
- term.setCursorPos(5,5) --good spot, won't get in the way.
- print"Are you trying to kill Sapphari?(\"yes\" to kill, anything else to go to homepage)"
- if read() == "yes" then
- error"User opted to kill Sapphari."
- end
- openSites[runningSite] = sapphInt.handleWebsite("home")
- end
- if openSites[runningSite] then
- if coroutine.status(openSites[runningSite]) == "suspended" then else
- sapphInt["timesHomed"]=sapphInt["timesHomed"]+1
- sapphInt.redrawUI()
- if runningSite ==1 then
- openSites[runningSite] = sapphInt.handleWebsite("home")
- else
- openSites[runningSite] = nil --let this site die
- runningSite = 1 --switch their tab
- end
- end
- if okay ==false then
- sapphInt["timesHomed"]=sapphInt["timesHomed"]+1
- okay=nil
- theError=secondThing --so they can go into lua and see it
- secondThing=nil
- openSites[runningSite] = sapphInt.handleWebsite("home")
- siteNames[runningSite] = "home"
- sapphInt.redrawUI()
- end
- else
- sapphInt.redrawUI()
- if runningSite ==1 then
- sapphInt["timesHomed"]=sapphInt["timesHomed"]+1
- openSites[runningSite] = sapphInt.handleWebsite("home")
- else
- openSites[runningSite] = nil --let this site die
- runningSite = 1 --switch their tab
- end
- end
- if openSites[runningSite] then
- if coroutine.status(openSites[runningSite]) == "suspended" and events[1] == secondThing or coroutine.status(openSites[runningSite]) == "suspended" and not secondThing or coroutine.status(openSites[runningSite]) == "suspended" and secondThing=="download" then
- term.redirect(redirectBuffers[runningSite])
- --os.pullEvent = sapphInt.newPullEvent
- if events[1] == "mouse_click" then
- events[4] = events[4]-2
- end
- --sapphInt.fsSecurity(true)--make sure they don't get into fs
- term.redirect(redirectBuffers[runningSite])
- okay, secondThing, downloadID,downloadSpot = coroutine.resume(openSites[runningSite], unpack(events))
- --sapphInt.fsSecurity()--no vars means it undoes the fs stuff
- term.redirect(normalTerm)
- --now we want to print the buffer on the screen
- --This means that Sapphari is double-buffered.
- local termX,termY=term.getSize()
- redirectBuffers[runningSite].blit(1,1,1,3,termX,termY)
- end
- end
- if secondThing == "download" then--they wanted to download something.
- local dll = sapphInt.getSiteStr(downloadID)
- dl=dll.readAll()
- dll.close()
- if dl and fs.exists(downloadSpot)==false then
- local writeIt = sapphInt.fsStuff.open(downloadSpot,"w")
- writeIt.write(dl)
- writeIt.close()
- end
- secondThing=nil
- end
- if sapphInt["timesHomed"]>19 then--if going to home has happened 20 times or above
- error"Sapphari went to home 20 times in arrow, assuming something is wrong."
- end
- end
- end
- ---------------------------------------------------
- function sapphInt.cleanup()
- --so we don't leave crap floating arround.
- --sometimes this gets left for some reason
- --also we must reset pullEvent
- openSites =nil --global variable cleanup
- sapphUtils = nil
- runningSite = nil
- currentSite = nil
- whiteSpaces = nil
- whitespacez = nil
- whitespace = nil
- os.pullEvent=oldPullEvent
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- term.clear()
- print(os.version())
- end
- sapphInt.handleSapphari()
- sapphInt.cleanup()
- sleep=sSleep
Advertisement
Add Comment
Please, Sign In to add comment