Posted 09 November 2016 - 07:11 PM
I got challenged to make a file managment system. And i need to know how to make the text centered. So i need to know how to do this Thanks
local function centerWrite(text)
local x, y = term.getCursorPos()
term.setCursorPos(math.floor((x - #text) / 2) + 1, y)
term.write(text)
end
The general math is simple: take the length of the text from the width of the screen, halve then floor the result, and finally, as Lua is 1-indexed, add one.local function centerWrite(text) local x, y = term.getCursorPos() term.setCursorPos(math.floor((x - #text) / 2) + 1, y) term.write(text) end
local function centerWrite(text)
local x, y = term.getCursorPos()
local w, h = term.getSize() --# get the width/height of the screen
term.setCursorPos(math.floor((w - #text) / 2) + 1, y) --# subtract x from the width, not the current cursor x
term.write(text)
end
local function center(str,ln)
local maxX,maxY = term.getSize()
term.setCursorPos((maxX/2)-(#str/2),ln)
print(str)
end
center("Testing",2)
With that, you should just be able to specify the line to print to, and then you are set