327 posts
Location
Julfander Squad Studio
Posted 21 October 2017 - 08:09 AM
It annoys me everytime i use it; the term.write, term.blit… functions don't have x and y arguments. You always have to set the cursor position to where you want to write, and then use the function. So many times i find myself with a code looking like this:
term.setCursorPos(1, 1)
term.write("text")
term.setCursorPos(1, 2)
term.write("another text")
term.setCursorPos(5, 1)
term.write("another another text")
So it would be cool to be able to use this code instead:
term.write("text", 1, 1)
term.write("another text", 1, 1)
term.write("another another text", 5, 1)
194 posts
Posted 21 October 2017 - 12:01 PM
You could totally rewrite the term api and make this change yourself
function _G.term.write(string,x,y)
term.setCursorPos(x,y)
term.write(string)
end
2427 posts
Location
UK
Posted 21 October 2017 - 12:13 PM
function _G.term.write(string,x,y)
if type(x) == "number" and type(y) == "number" then
term.setCursorPos(x,y)
elseif x ~= nil and y ~= nil then
error("x and y must be numbers or nil", 2)
end
term.write(string)
end
Improvement, make x and y optional
477 posts
Location
Germany
Posted 21 October 2017 - 06:45 PM
If I'm not completely wrong neither of those will work because they will just call themself forever, solution:
local oldWrite = term.write
function _G.term.write(string,x,y)
if type(x) == "number" and type(y) == "number" then
term.setCursorPos(x,y)
elseif x ~= nil and y ~= nil then
error("x and y must be numbers or nil", 2)
end
oldWrite(string)
end
327 posts
Location
Julfander Squad Studio
Posted 22 October 2017 - 07:02 AM
I want to be able to use the function like it was before, without having to specify the x and y.
So my code looks like this:
local oldWrite = term.write
function _G.term.write(string,x,y)
if not x or not y then
local cur_x, cur_y = term.getCursorPos()
x = x or cur_x
y = y or cur_y
end
term.setCursorPos(x,y)
oldWrite(string)
end
Edited on 23 October 2017 - 03:37 PM
2427 posts
Location
UK
Posted 22 October 2017 - 04:04 PM
I want to be able to use the function like it was before, without having to specify the x and y.
So my code looks like this:
local oldWrite = term.write
function _G.term.write(string,x,y)
local cur_x, cur_y = term.getCursorPos()
x = x or cur_x
y = y or cur_y
term.setCursorPos(x,y)
oldWrite(string)
end
The code in the post written by Luca_S is more efficient as it reduces function calls. term.write or oldWrite already uses the current cursor position is none is provided, therefore you don't need to set it in your replacement function if no x or y are provided. Additionally it will handle incorrect values for x and y correctly.