This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Geforce Fan's profile picture

Odd table behavior

Started by Geforce Fan, 11 June 2014 - 12:01 AM
Geforce Fan #1
Posted 11 June 2014 - 02:01 AM
Every time I click tab 2 times, it adds another "add tab". Why is this happening?
Here's the code. You're going to want to click on "tab" when you run it
By the way, you'll need the redirect API.
Here's the pastebin for redirect: pastebin get fU9Kj9zr redirect

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("redirect")
end
--Credits:
--Developers:
--Hithere

--APIs

--Special thanks to:
--oeed, for OneOS, the OS Sapphari was
--designed for

--DEVELOPER CONFIG
local waitForMessage =  false

--------------------------------------------------
  sapphariVersionString = "1.0"
  sapphariVersionNumber = 1

  sapphariSites = {}
  sapphariBlacklist = {}
--TESTS:
--might fix some crashes
--+function error()



	--The script sets the news, then sets the url vars
	--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 = {}
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"
end
--this function will get the data for a site,
--however, will not run it.
function sapphInt.getSite(pasteID)
  local responce = http.get("http://pastebin.com/raw.php?i="..pasteID)
  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
		--bookmarks here
		print"bok"
	  elseif clickX>13 and clickX<18 and clickY==2 then
		--tabs button
		--print"Tabz"
		--insert the add a tab option
		local otherReturn = sapphInt.dropDownMenu(tabNames,12,2)
		return "tab",otherReturn
	  end
	end
	if clickX>termX/3+1 then
	  if clickX<termX/3+8 then
		--need to refresh page nao
		return 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
--WEBSITE FUNCTON
--sapphutils, MUST be global!!!
sapphUtils = {}
--compatibility with Sapphari Beta:
function sapphUtils.pullEvent(filter)
  return {os.pullEventRaw(filter)}
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)
  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
		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
	--make some whitespace:
	local whitespace=""
	for i=1,endx-startx do
	  whitespace=whitespace.." "
	end
	term.write(whitespace)
	--reset cursor
	term.setCursorPos(startx,starty)
	term.write(textThingPrint)
  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 longestvar = #contents[1]
  for i=1,#contents do
	if #contents[i] > longestvar then
	  longestvar = #contents[i]
	end
  end
  --okay, now let's give us some whitespace
  local whitespace= ""
  for i=1,longestvar do
	whitespace = whitespace.." "
  end
  local termX,termY=term.getSize()
  term.setBackgroundColor(colors.white)
  --to refresh the menu for the first time
  os.startTimer(0)
   scrollDown = 0
  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
	for i=y+1,whiteSpaces do
	  term.setCursorPos(x,i)
	  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
end
function sapphInt.handleSapphari()
  --fix os.pullEvent
  oldPullEvent=os.pullEventRaw
  os.pullEvent=os.pullEventRaw
  local openSites = {}
  local runningSite = 1
  local siteNames = {}
  local sitesPassed
  termX,termY=term.getSize()
  siteNames[1] = "Homepage"
  --a table for open sites.
  --Only the foucused site is ran
  openSites[1] = sapphInt.handleWebsite("home")
  termX,termY=term.getSize()
  local siteBuffer = 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(eventFilter)}
	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)
	  sitesPassed=nil
	  if what == "newsite" and returned then
		local tempSite = sapphInt.handleWebsite(returned)
		if tempSite then
		  openSites[runningSite] = tempSite -- kill the current site and replace it
		end
	  elseif what =="tab" then
		if returned == 1 then
		--make a new tab
		runningSite = #openSites+1
		openSites[#openSites+1] = sapphInt.handleWebsite("home")
		end
	  end
	elseif events[1] == "terminate" then
	  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 coroutine.status(openSites[runningSite]) == "dead" then
	  print"ded"
	  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 coroutine.status(openSites[runningSite]) == "suspended" then
	term.redirect(siteBuffer)
	--os.pullEvent = sapphInt.newPullEvent
	if events[1] == "mouse_click" then
	  events[4] = events[4]-2
	end
	okay, eventFilter = coroutine.resume(openSites[runningSite], unpack(events))
	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()
	siteBuffer.blit(1,1,1,3,termX,termY)
	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
  sapphUtils = nil
  runningSite = nil
  os.pullEvent=oldPullEvent
end


sapphInt.handleSapphari()
sapphInt.cleanup()
Edited on 11 June 2014 - 12:23 AM
Yevano #2
Posted 11 June 2014 - 02:19 AM
sitePassed ~= sitesPassed
Geforce Fan #3
Posted 11 June 2014 - 02:20 AM
…rage.
When I do that and remove the table.remove, it makes another "Add Tab" every time I click it.
Will fix the code above, so the next problem can be solved
Edited on 11 June 2014 - 12:21 AM