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

Text Wrapping

Started by JerryWester, 31 October 2016 - 02:11 PM
JerryWester #1
Posted 31 October 2016 - 03:11 PM
Hello! I am developing a scrolling text program in ComputerCraft, but my problem is that the text wraps around the screen and words end up on the next line. I want to prevent the text from wrapping around the screen. Any and all help is appreciated, and thank you in advance.

My code:
Spoiler

local messages = { -- Edit this to choose which messages show up
	"This is the first line",
	"You can redirect this to a monitor by typing \"scroll [side]\".",
	"Press \"Q\" to exit the program",
	"This is the fifth line",
	"Wait, I made a mistake. That was the fourth line, this is the fifth",
}

local speed = 0.1 -- Speed at which the messages scroll in seconds

-- Do not edit anything below this unless you understand what youre doing

local tArgs = {...}
local sides = {"front", "back", "left", "right", "top", "bottom"}
local monSide
local xSize, ySize
local runProg = true
local s = messages[1] .. " - "
local f = 2
local scroll = 1
local center

if #tArgs > 0 then
	for i = 1, #sides do
		if tArgs[1] == sides[i] then
			monSide = sides[i]
		end
	end
	if peripheral.getType(monSide) == "monitor" then
		local mon = peripheral.wrap(monSide)
		term.redirect(mon)
	else
		print("No monitor was found on that side!!")
		runProg = false
	end
end

if runProg then
	shell.run("clear")
		xSize, ySize = term.getSize()

		center = math.ceil(ySize/2)

		while runProg do
			os.startTimer(speed)
			e, p1, p2, p3 = os.pullEvent()
			if e == "timer" then
				while not (#s >= xSize - scroll) do
				s = s .. messages[f] .. " - "
				if f ~= #messages then
					f = f + 1
				else
					f = 1
				end
			end

			term.setCursorPos(scroll, center)
			write(s)
			for i = 1, ySize - center do
				term.setCursorPos(scroll, center + i)
				term.clearLine()
			end
			scroll = scroll - 1
		elseif e == "key" then
			if p1 == keys.q then
				shell.run("clear")
				sleep(0.05)
				break
			end
		end
	end
end

EDIT: My apologies, I figured it out. I now feel like an idiot for not seeing it before, but I needed to use term.write() instead of just write(). Any moderator is welcome to delete this post. For those of you wanting to know the difference between the printing methods, go to http://www.computercraft.info/wiki/Term.write
Edited on 31 October 2016 - 02:43 PM
Dog #2
Posted 31 October 2016 - 03:39 PM
write automatically line wraps. Since you've redirected, try using term.write - that should do what you want.