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

Scroll table up/down

Started by Mirodin, 06 July 2013 - 04:41 PM
Mirodin #1
Posted 06 July 2013 - 06:41 PM
Hey guys,

maybe I do not see the forest for the trees but I want single values from a table printed on the screen like this

[1] - "blabla"
[2] - "blublu"
	 [1] - 17
	 [2] - "haha"
My code works quiet good and all the indentations are ok, but I do not get it to get scrolled up/down properly. So when I use term.scroll() with 1 or -1 params I have the problem that the bottommost/topmost value gets deleted at each step which is not what I want^^.

I hope you can follow me bad english explanations (sorry :)/> ) and point me a way to solve this really annoying thing.

Cheers Mirodin
Engineer #2
Posted 06 July 2013 - 07:03 PM
Can you show us the code you currently have? I know its not necessary for this one, but I would like to know how far you came
Lyqyd #3
Posted 06 July 2013 - 07:34 PM
You'll have to replace the data as you scroll it back onto the screen–the terminal does not keep track of lines scrolled off-screen.
Mirodin #4
Posted 07 July 2013 - 02:17 PM
So here is my code. I know, it is quiet messy because of my minor Lua skills and the experiments to write it down in two columns.
I hope you will understand it despite of german ;)/>

P.S.: sauber() is a function which clears my terminal


function anzeigen(db, von, bis, mitk)
	local _, tast, x, y, maxY
	local maxY = false
	local maxX = 0
	local einrueck = 0
	local count = 1
	local w, h = term.getSize()
	local tmp = {}
	local inter = {
		["TOP"] = "Verwendung: mit Pfeil hoch/runter navigieren, mit \"Esc\" verlassen.",
		["TRENN"] = "-",
	}
  
	local function show()
  
		if count > bis then
	  
			count = von
		  
		elseif count < von then
	  
			count = bis
		  
		end
	  
		sauber()
		print(inter["TOP"].."\n"..count.."/"..#tmp.."\n")
	  
		local function aufloesung(tab)
	  
			einrueck = einrueck + 1
			local einrueck = einrueck
		  
			for k, v in pairs(tab) do
		  
				x, y = term.getCursorPos()
			  
				if x > maxX then maxX = x end
			  
				if y == h - 2 then
			  
					y = 5
					maxY = true
				  
				end
			  
				if maxy == true then einrueck = x + 10 end
			  
				if type(v) == "table" then
			  
					if mitk then
				  
						term.setCursorPos(einrueck,y + 1)
						term.write(k)
					  
					end
				  
					aufloesung(v)
				  
				else
			  
					term.setCursorPos(einrueck,y + 1)
					term.write(tostring(k).." - "..tostring(v))
				  
				end
			  
			end
		  
		end
	  
		aufloesung(tmp[count])
	  
		for i = 1, w do
	  
			if maxY then
		  
				term.setCursorPos(i, h)
			  
			else
		  
				term.setCursorPos(i, y + 2)
			  
			end
		  
			term.write(inter["TRENN"])
		  
		end
	  
	end
  
	for k, v in pairs(db) do
  
		tmp[count] = {[k] = v}
		count = count + 1
	  
	end
  
	if type(von) == "boolean" then
  
		mitk = von
		von = 1
	  
	elseif type(bis) == "boolean" then
  
		mitk = bis
		bis = #tmp
	  
	end
  
	von = von or 1
	bis = bis or #tmp
  
	if von > bis then
  
		print("Der Startwert muss kleiner sein, als der Endwert!")
		print("Die Datenbank wird vom ersten Wert ausgehend angezeigt...")
	  
		von = 1
	  
	elseif von > #tmp then
  
		print("Die Datenbank endet vor dem angegebenen Startwert!")
		print("Die Datenbank wird vom ersten Wert ausgehend angezeigt...")
	  
		von = 1
	  
	end

	if bis > #tmp then

		print("Die Datenbank endet vor dem angegebenen Endwert!")
		print("Die Datenbank wird bis zum Ende angezeigt...")
	  
		bis = #tmp
	  
	end

	show()
  
	while true do
  
		_, tast = os.pullEvent("key")
	  
		if tast == 200 then
	  
			count = count - 1
		  
		elseif tast == 208 then
	  
			count = count + 1
		  
		elseif tast == 1 then
	  
			sauber()
		  
			return
		  
		end
	  
		einrueck = 0
		maxX = 0
		maxY = false
	  
		show()
	  
	end
end