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

Problem with wrapping text in printers

Started by Guilty_Spark_117, 29 November 2013 - 01:00 PM
Guilty_Spark_117 #1
Posted 29 November 2013 - 02:00 PM
So, lua noob here (kinda). I am trying to make a contract system for my server. I have everything down and finished with except the printer, which I have never used. I have a problem with the text running off the page, and I am unsure how to wrap it around to another line to print the entire string.

I am not on the host computer at the moment, but if it is necessary I can post code.
Any help would be appreciated. Thanks in advance.

In the description line the text runs off the page.

Just noticed technologies is spelled wrong. oops :P/>

Edited on 29 November 2013 - 01:01 PM
Lyqyd #2
Posted 29 November 2013 - 02:48 PM
Take a look at how the write() function in bios.lua does it.
Guilty_Spark_117 #3
Posted 29 November 2013 - 03:41 PM
Take a look at how the write() function in bios.lua does it.
Okay, I did this and I am still rather confused, sorry.
Bomb Bloke #4
Posted 29 November 2013 - 04:04 PM
Something fromm this thread may be useful to you.
Guilty_Spark_117 #5
Posted 29 November 2013 - 05:21 PM
Something fromm this thread may be useful to you.
I tried everything in that thread. It prints correctly in a monitor, but on the printers it prints the first line and then a "?" and nothing more. I really know just about nothing about string editing, so reading the code for it is like reading a new language for me :(/>
Bomb Bloke #6
Posted 30 November 2013 - 02:28 AM
It should be easy enough to demonstrate how to alter your code, if you post what you've got thus far.
Guilty_Spark_117 #7
Posted 30 November 2013 - 04:13 PM
It should be easy enough to demonstrate how to alter your code, if you post what you've got thus far.

…. well you asked for it. Don't laugh. :unsure:/> The printing section is really messed up due to me trying out different codes that have been spread all over the interwebs.


local p=peripheral.wrap("right")

local function clear()
term.clear()
term.setCursorPos(1,1)
end

function wrapText(text, limit)
local lines = {}
local curLine = ''
for word in text:gmatch('%S+%s*') do
curLine = curLine .. word
if #curLine + #word >= limit then
lines[#lines + 1] = curLine
curLine = ''
end
end
return lines
end


while true do
clear()
io.write("Enter a name for the contract")
print("")
name = read()
print("——————————-")
io.write("Enter your name")
print()
yourname = read()
print("——————————-")
io.write("Enter the name of the contractor")
print()
contractor = read()
print("——————————-")
io.write("Enter a description for the contract")
print()
desc = read()
print("——————————-")
io.write("Enter the pay for the contract")
print()
pay = read()
print("——————————-")
clear()
d=0
while d <= 5 do
print("Processing")
sleep(.3)
clear()
print("Processing.")
sleep(.3)
clear()
print("Processing..")
sleep(.3)
clear()
print("Processing…")
sleep(.3)
clear()
d=d+1
end
repeat print("Printer needs more paper")
sleep(.5)
clear()
until p.getPaperLevel()>0
repeat print("Printer needs more ink")
sleep(.5)
clear()
until p.getInkLevel()>0
p.newPage()
p.setCursorPos(1,1)
p.write("CONTRACT PAPER")
p.setCursorPos(1,3)
wrapped = wrapText(name,19)
name = (table.concat(wrapped, '\n'))
p.write("Name: "..name)
wrapped = wrapText(contractor,21)
contractor = (table.concat(wrapped, '\n'))
p.setCursorPos(1,5)
p.write("To: "..contractor)
p.setCursorPos(1,7)
wrapped = wrapText(yourname,19)
yourname = (table.concat(wrapped, '\n'))
p.write("From: "..yourname)
p.setCursorPos(1,9)
wrapped = wrapText(desc,23)
p.write("Description: ")
desc = (table.concat(wrapped, '\n'))
p.setCursorPos(1,10)
p.write(desc)
p.setCursorPos(1,16)
wrapped = wrapText(pay,20)
print(table.concat(wrapped, '\n'))
p.write("Pay: "..pay)
p.setCursorPos(1,18)
if p.endPage() then print("Files have been sent to the printer")
else print("An error occurred while attempting to print your paper.")
end
sleep(5)
end



This is what is showing up.Where the "?" is where an indent should be. Thanks for helping this far.

Bomb Bloke #8
Posted 30 November 2013 - 05:13 PM
Hmm. Want that diamond block back, huh?

The idea is that the "wrapText" function returns a table filled with strings - a collection of variables, if you will. Instead of printing them in one go, you do them line by line instead.

Try replacing this:

p.setCursorPos(1,9)
wrapped = wrapText(desc,23)
p.write("Description: ")
desc = (table.concat(wrapped, '\n'))
p.setCursorPos(1,10)
p.write(desc)

With this:

p.setCursorPos(1,9)
wrapped = wrapText(desc,23)
p.write("Description: ")
for i=1,#wrapped do      -- A loop that'll repeat a number of times equal to the amount of variables in "wrapped".
  p.setCursorPos(1,9+i)  -- 'i' increments by one with each repetition of the loop.
  p.write(wrapped[i])    -- 'wrapped[1]' in the first string in the table, 'wrapped[2]' the second, and so on.
end