Jump to content




Printer.write too long for the page!

lua

2 replies to this topic

#1 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 08 May 2014 - 11:17 PM

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?

#2 HometownPotato

  • Members
  • 62 posts

Posted 08 May 2014 - 11:47 PM

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 by HometownPotato, 08 May 2014 - 11:53 PM.


#3 cptdeath58

  • Members
  • 139 posts
  • LocationError 404: Could not find.

Posted 09 May 2014 - 12: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 by cptdeath58, 09 May 2014 - 12:28 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users