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

Some questions about problems with my Scroll and Print event.

Started by jasperdekiller, 04 February 2015 - 09:24 PM
jasperdekiller #1
Posted 04 February 2015 - 10:24 PM
Hello,

I'm working on a Database and I got some problems with it. So I thought lets ask it here
maybe someone here can help me out.


function logview.printHeader()
term.setCursorPos(1,1)
  term.setBackgroundColor(colors.gray)
  term.clearLine()
  term.setCursorPos(1,2)
  term.clearLine()
  term.setCursorPos(1,3)
  term.clearLine()
  term.setCursorPos(2,2)
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.gray)
if offline == 0 then
  term.write(" x ")
elseif offline == 1 then
  term.write(" > ")
end
  term.setCursorPos(6,2)
  term.setBackgroundColor(colors.gray)
  term.setTextColor(colors.white)
  term.write(Log_Running_Status)
	  
-- Buttons --
	  
  term.setBackgroundColor(colors.cyan)
  term.setTextColor(colors.white)
  term.setCursorPos(18,2)
  term.write(" Options ")
  term.setBackgroundColor(colors.blue)
  term.setTextColor(colors.white)
  term.setCursorPos(28,2)
  term.write(" Log ")
  term.setCursorPos(34,2)
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.gray)
  write(" Accounts ")
  term.setTextColor(colors.gray)
  term.setBackgroundColor(colors.white)
  term.setCursorPos(45,2)
  write(" Quit ")
end



function logview.printLogs(offset)
lineNumber = 3
-- Print the layout --

  term.setCursorPos(34,2)
  term.setTextColor(colors.gray)
  term.setBackgroundColor(colors.white)
  term.write(" Accounts ")
  term.setBackgroundColor(colors.white)
  term.clear()
logview.printHeader()
if #log > 0 then
for i = offset, offset + 16 do
if i > #log then break end
  
  if log[i][2] == "Info" then
   term.setCursorPos(1,lineNumber+1)
   term.setTextColor(colors.blue)
   write("["..log[i][2].."] ")
   term.setTextColor(colors.lightGray)
   x, y = term.getCursorPos()
   term.setCursorPos(x,y)
   write(log[i][1])
   xP, lineNumber = term.getCursorPos()
  elseif log[i][2] == "Success" then
   term.setCursorPos(1,lineNumber+1)
   term.setTextColor(colors.green)
   write("["..log[i][2].."] ")
   term.setTextColor(colors.lightGray)
   x, y = term.getCursorPos()
   term.setCursorPos(x,y)
   write(log[i][1])
   xP, lineNumber = term.getCursorPos()
  elseif log[i][2] == "Warning" then
   term.setCursorPos(1,lineNumber+1)
   term.setTextColor(colors.red)
   write("["..log[i][2].."] ")
   term.setTextColor(colors.lightGray)
   x, y = term.getCursorPos()
   term.setCursorPos(x,y)
   write(log[i][1])
   xP, lineNumber = term.getCursorPos()
  elseif log[i][2] == "Error" then	
   term.setCursorPos(1,lineNumber+1)
   term.setTextColor(colors.red)
   write("["..log[i][2].."] ")
   term.setTextColor(colors.lightGray)
   x, y = term.getCursorPos()
   term.setCursorPos(x,y)
   write(log[i][1])
   xP, lineNumber = term.getCursorPos()
  elseif log[i][2] == "Alert" then	
   term.setCursorPos(1,lineNumber+1)
   term.setTextColor(colors.red)
   write("["..log[i][2].."] ")
   term.setTextColor(colors.lightGray)
   x, y = term.getCursorPos()
   term.setCursorPos(x,y)
   write(log[i][1])
   xP, lineNumber = term.getCursorPos()
   end
	logview.printHeader()
  end

end
end


This two functions up here will print all logs and will print the Header. But
when a Log Message the bottom of the screen the thing will mess up and some things will just print
into other messages. Thats that,

Then we have another problem that with my scrolling event of the Logs.


function logview.userScrolls()
	while true do
		event, amount = os.pullEvent()
		if event == "mouse_scroll" then
		  logstartTime = 0
			local loglastOffset = logCurrentOffset
			if logCurrentOffset + amount <= 1 then
				logCurrentOffset = 1
			elseif (amount + logCurrentOffset + 15 >= #log) then
				logCurrentOffset = #log - 15
				if logCurrentOffset <= 1 then
				  logCurrentOffset = 1
				end
			else
				logCurrentOffset = logCurrentOffset + amount
			end
			if logCurrentOffset ~= loglastOffset then
				logview.printLogs(logCurrentOffset)
			end
		end
	end
end

That is not working correctly at all, maybe someone can see a litle problem.

Then the page where the users will be printed, I want to use key arrows to scroll down too.
The problem is that when I reached the top I will be able to go down but it wont scroll down.

Scroll system:

function userScrolls()
	while true do
		event, amount = os.pullEvent()
		if event == "mouse_scroll" then
		  startTime = 1
			local lastOffset = currentOffset
			if currentOffset + amount <= 1 then
				currentOffset = 1
			elseif (amount + currentOffset + 11 >= #currentUsers) then
				currentOffset = #currentUsers - 11
				if currentOffset <= 1 then
				  currentOffset = 1
				end
			else
				currentOffset = currentOffset + amount
			end
			if currentOffset ~= lastOffset then
				printUsers(currentOffset)
			end
		end
	end
end

Key press:

   elseif s == 200 then
	keyAmount = 1
	keyAm = keyAm - 1
   if selectedRow then
	if selectedRow == 1 then
	else
	 selectedRow = selectedRow - 1
	 selectedAccount = Users[selectedRow]
	 printUsers(currentOffset)  
	end
   end
   elseif s == 208 then
	keyAmount = 1
	keyAm = keyAm + 1
   if selectedRow then
	if selectedRow == 12 then
	else
	 selectedRow = selectedRow + 1
	 selectedAccount = Users[selectedRow]
	 printUsers(currentOffset)
	end	
   end



I guess you will think, what alot of problems. I know
I'm still learning and I just want to know how to fix this such
things. I dont suspect someone can answer all problems, but I hope someone can just
answer some. Like the printing logs thing is the biggest problem.

This picture will show you what I meant with printing in other logs.



Thankyou,
Jasper
Bomb Bloke #2
Posted 05 February 2015 - 11:41 PM
This two functions up here will print all logs and will print the Header. But
when a Log Message the bottom of the screen the thing will mess up and some things will just print
into other messages. Thats that,

Going by your screenshot, this looks to be related to how "write" handles word-wrap. You're writing more log-messages then can fit on the screen, and whenever the word-wrap functionality kicks in, they get forced back on - overwriting whatever was at the bottom.

Because your messages are longer than a line each, it's difficult to know exactly how many you should render per "page". Probably the "simplest" way to do it is to give up on "write" and use "term.write" instead (which doesn't use word-wrap), but then you won't be seeing your full message content on-screen…

I suggest re-organising your "log" table so that no one line is longer than the width of the screen. When adding a new index, "x", to "log", then if #("["..log[x][2].."] ") + #log[x][1] > 51, use string.sub() to cut the end off log[x][1] and place it into log[x + 1] to be rendered on its own (without a log[x + 1][2]), and so on.

Then we have another problem that with my scrolling event of the Logs.



That is not working correctly at all, maybe someone can see a litle problem.

What happens instead? Where are you calling this function from?

Then the page where the users will be printed, I want to use key arrows to scroll down too.
The problem is that when I reached the top I will be able to go down but it wont scroll down.

If "selectedRow" would go off-screen, instead of changing it, change currentOffset.

Instead of using the likes of 200 and 208, consider using keys.up and keys.down. Makes for much more readable code.

Also, consider using a function like this:

local function writeAt(text, x, y, textCol, backCol)
	local curX, curY = term.getCursorPos()
	term.setCursorPos(x or curX, y or curY)
	if textCol then term.setTextColour(textCol) end
	if backCol then term.setBackgroundColour(backCol) end
	term.write(text)
end

It should allow you to shrink your script dramatically, by turning this sort of thing:

  term.setBackgroundColor(colors.cyan)
  term.setTextColor(colors.white)
  term.setCursorPos(18,2)
  term.write(" Options ")

… into this sort of thing:

  writeAt(" Options ", 18, 2, colors.white, colors.cyan)
Edited on 05 February 2015 - 10:43 PM