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

Replace term so top row is not used

Started by Selim, 14 October 2015 - 08:15 PM
Selim #1
Posted 14 October 2015 - 10:15 PM
I am trying to replace parts of the term API so that nothing can use the top row of the window. So, this is what I have now for new functions:
function _G.term.setCursorPos(x, y)
oldtermsetcursorpos(x, y+1)
end

function _G.term.getSize()
local x, y = oldtermgetsize()
return x, (y-1)
end

function _G.term.getCursorPos()
local x, y = oldtermgetcursorpos()
return x, (y+1)
end
But, then it results in this:
[attachment=2417:screenshot (2).png]
It normally looks like this:
[attachment=2418:screenshot (3).png]

Am I missing a function that would need to be replaced as well?

NOTE: I am showing this with the Northbridge Terminal OS rather than CraftOS because CraftOS with these modifications doesn't work properly in Mimic at least. It doesn't print anything then. I am currently using Mimic for testing as I am on my Chromebook at this moment.
Edited on 14 October 2015 - 08:31 PM
Bomb Bloke #2
Posted 14 October 2015 - 10:30 PM
The simple way is to define a window that covers the area of the screen you wish to use, then just term.redirect() to that.
Selim #3
Posted 14 October 2015 - 10:33 PM
I will look into that.
Edited on 14 October 2015 - 08:36 PM
TYKUHN2 #4
Posted 14 October 2015 - 11:30 PM
I cannot test because CraftOS as you stated hates this change, but what I suspect is when your printing it is printing to y+1 which sets the pos to y+1 which then next run adds another 1 effectively y+2

Basically detect if y = old 0 then return y+1 else return Y position.
Selim #5
Posted 15 October 2015 - 02:37 PM
I figured it out, I needed to y-1 in term.getCursorPos() instead of y+1.
Konlab #6
Posted 17 October 2015 - 07:10 PM
I figured it out, I needed to y-1 in term.getCursorPos() instead of y+1.
If you try to run programs you need to overwrite os.pullEvent, too. (If you want correct events).
Dragon53535 #7
Posted 18 October 2015 - 07:07 AM
I figured it out, I needed to y-1 in term.getCursorPos() instead of y+1.
Perhaps you could just simply check one thing

function _G.term.setCursorPos(x, y)
  if y == 1 then
    y = 2
  end
  oldtermsetcursorpos(x, y+1)
end
valithor #8
Posted 18 October 2015 - 07:12 AM
I figured it out, I needed to y-1 in term.getCursorPos() instead of y+1.
Perhaps you could just simply check one thing

function _G.term.setCursorPos(x, y)
  if y == 1 then
	y = 2
  end
  oldtermsetcursorpos(x, y+1)
end

The only problem with doing the if statement would be the fact you wouldn't be able to print to the second line. If you put in 1, then it would end up being 3, 2 would end be 3 as well. Either way creating a window is the smarter thing to do as bomb suggested, since simple overwrites like these could lead to unexpected results, things not behaving how they would expected to, or bypasses (for his solution simply passing 0 will write to the first line).

edit:

Just putting the addition in a else would fix the problem, but it would still have the problem of passing 0.


function _G.term.setCursorPos(x, y)
  y = y <= 1 and 1 or y
  oldtermsetcursorpos(x, y+1)
end
Edited on 18 October 2015 - 05:18 AM
Bomb Bloke #9
Posted 18 October 2015 - 07:37 AM
(for his solution simply passing 0 will write to the first line).

Eh?
H4X0RZ #10
Posted 18 October 2015 - 10:39 AM
(for his solution simply passing 0 will write to the first line).

Eh?
Valithor was referring to Dragons code. If you enter 0 for the Y coord it would increase it by 1 (so it becomes 1).
Edited on 18 October 2015 - 08:40 AM
TheOddByte #11
Posted 18 October 2015 - 01:53 PM
I'd go with BombBloke's suggestion for this, just create a new window object and redirect to that

--# Get the size of the screen
local w, h = term.getSize()

--# Create the window object
local win = window.create( term.current(), 1, 2, w, h - 1, true )

--# Redirect to the window object
term.redirect( win )

--# Test it
term.setBackgroundColor( colors.white )
term.clear()
Dragon53535 #12
Posted 19 October 2015 - 03:20 PM
Derpity Derp, forgot to actually edit that bottom line, as well as check for zero and less.


function _G.term.setCursorPos(x, y)
  y = y < 2 and 2 or y
  oldtermsetcursorpos(x, y)
end
I don't know if i'm right in assuming that 1.92 will write on the first line due to truncation, or if the Java side actually rounds it.
Edited on 19 October 2015 - 01:23 PM
Konlab #13
Posted 19 October 2015 - 03:24 PM
Derpity Derp, forgot to actually edit that bottom line, as well as check for zero and less.


function _G.term.setCursorPos(x, y)
  y = y < 2 and 2 or y
  oldtermsetcursorpos(x, y)
end
I don't know if i'm right in assuming that 1.92 will write on the first line due to truncation, or if the Java side actually rounds it.
Throw errors instead of doing this if y is less than 2 (or set cursor pos to 0). But using the window API is better
Edited on 19 October 2015 - 01:25 PM
Dragon53535 #14
Posted 19 October 2015 - 11:32 PM
Throw errors instead of doing this if y is less than 2 (or set cursor pos to 0). But using the window API is better
For something simplistic, i would agree with you there, however my solution works on normal computers as well as if someone were to use the TLCO it would still work.
valithor #15
Posted 20 October 2015 - 12:19 AM
Throw errors instead of doing this if y is less than 2 (or set cursor pos to 0). But using the window API is better
For something simplistic, i would agree with you there, however my solution works on normal computers as well as if someone were to use the TLCO it would still work.

Throwing errors would be a horrible solution. The entire point of this is to just remove the ability to write to the first line, without changing any funcitonality, which throwing errors would.

The only problem with only changing term.setCursorPos is then anything that uses term.getCursorPos would be 1 line off (in setCursorPos line 2 would be treated as line 1, while in getCursorPos line 2 is treated as line 2).

Either way it doesn't matter. The windows solution has been mentioned numerous times as the best solution, so there really shouldn't be any reason to continue to discuss another one.