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

Printer.write too long for the page!

Started by Agent Silence, 08 May 2014 - 10:17 PM
Agent Silence #1
Posted 09 May 2014 - 12:17 AM
Im trying to make a simple program for my server, for admin applications, but when someone types something too long, it goes off the page. Any fix?
HometownPotato #2
Posted 09 May 2014 - 12:47 AM
Since all the characters are the same length with the font CC uses (I believe), you can compare the amount of characters and divide that by the maximum that can fit per line. And you will get how many lines you will need (math.ceil it).
So something like:


local text = "blah";
local sX, sY = printer.getPageSize();
local lines = math.ceil(#text / sX)
for y = 1, lines do
   local start = ((y - 1) * sX) + 1;
   local finish = y * sX;
   printer.setCursorPos(1, y)
   printer.write(text:sub(start, finish));
end
The math seemed to have worked out in my head, though I'm not sure it's entirely correct.


Also, you can implement something fairly easy (when lines > paper size Y) to check if it reached the end of the page, then start a new page.
Edited on 08 May 2014 - 10:53 PM
cptdeath58 #3
Posted 09 May 2014 - 01:28 AM
you could just put:
printer = peripheral.wrap( "side" )
local px , py = printer.getPageSize()
if px > "however long a page is" then
-- cancel the input by using something.
but HometownPotato has a better code with that part. (IF it does work)
Edited on 08 May 2014 - 11:28 PM