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

Clear the line above

Started by xXSniper1313Xx, 05 July 2012 - 01:45 AM
xXSniper1313Xx #1
Posted 05 July 2012 - 03:45 AM
Hey Guys,

Basically i have a program with rednet and one computer is the sender and the other is the receiver. the sender is hooked up to redstone then a wireless receiver (technic pack) then it goes into the sky and connects to a light sensor. anyways my problem is once it sends the computer the Day/Night and repeats every second to check i want it to clear the line above it here is my code for clearing the line above it

while true do
term.clearLine()
id, message = rednet.receive()
print(message)
sleep(1)
end

Any help will be gladly appreciated
Thanks in advanced,
~Sniper
Lyqyd #2
Posted 05 July 2012 - 05:28 AM
Here's a function that should clear the previous line. You'd just replace the term.clearLine() line with a call to this function, and include it in your program.


function clearPreviousLine()
	local x, y = term.getCursorPos()
	local line = 1
	if x > 1 then line = x - 1 end
	term.setCursorPos(line, 1)
	term.clearLine()
	term.setCursorPos(x, y)
end
MysticT #3
Posted 05 July 2012 - 04:12 PM

term.setCursorPos(line, 1)
should be:

term.setCursorPos(1, line)
Lyqyd #4
Posted 06 July 2012 - 01:26 AM

term.setCursorPos(line, 1)
should be:

term.setCursorPos(1, line)

Is the term.setCursorPos documentation incorrect? I'll see about editing it if it is.
MysticT #5
Posted 06 July 2012 - 01:47 AM
Actually, I just noticed that what's wrong is that you need to move the cursor in the y direction, not the x.
So it should be like this:

function clearPreviousLine()
	local x, y = term.getCursorPos()
	term.setCursorPos(1, y - 1)
	term.clearLine()
	term.setCursorPos(x, y)
end
Lyqyd #6
Posted 06 July 2012 - 05:53 AM
Actually, I just noticed that what's wrong is that you need to move the cursor in the y direction, not the x.
So it should be like this:

function clearPreviousLine()
	local x, y = term.getCursorPos()
	term.setCursorPos(1, y - 1)
	term.clearLine()
	term.setCursorPos(x, y)
end

Yep, just looked through some old code I wrote that had a setCursorPos() call in it. I've updated the example usage in the wiki documentation (which is how I got x and y transposed in my original example). By the way, your code wouldn't catch the edge case of y = 1, which I will admit is unlikely to happen.