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

Attempting to center ASCII Art

Started by PieCrafted, 04 August 2015 - 12:03 AM
PieCrafted #1
Posted 04 August 2015 - 02:03 AM
I have this code but it prints the art in the top left of the screen:


local file = fs.open("piecinema/logo", "r")
if file then
  local text = file.readAll()
  file.close()
  print(text)
else
  print("PieCinema Logo Doesn't Exist! Downloading!")
  shell.run("pastebin get x0yd4h9D piecinema/logo")
end

What I am trying to do is center the ASCII Art found at pastebin.com/x0yd4h9D in the direct center x and center y without it getting distorted. When I have attempted to use over code, it puts the top line in the y center and it messes the spaces up on the x axis.

I am trying to have the middle line be in y center and have it so x won't distort the text because of the spaces.

Can someone help please?
Bomb Bloke #2
Posted 04 August 2015 - 03:14 AM
Well, you'd need to consider the number of lines, and their width. Figuring out those two values is done by inspecting the string to see where the newline characters (\n) are.

Eg, using string.find() and a loop, we can shove each line into a table:

local pos, lines, width = 1, {}, 0
repeat
	local cur = text:find("\n", pos)
	
	if cur then  -- If we found a new break,
		lines[#lines + 1] = text:sub(pos, cur - 1)
		if #lines[#lines] > width then width = #lines[#lines] end
		pos = cur + 1
	elseif text:sub(#text) ~= "\n" then  -- or, if the last line also contains text,
		lines[#lines + 1] = text:sub(pos)
		if #lines[#lines] > width then width = #lines[#lines] end
	end
until not cur

Now we compare the size of the art to that of your display, and render accordingly:

local xBump, yBump = term.getSize()
xBump, yBump = math.floor((xBump - width) / 2) + 1, math.floor((yBump - #lines) / 2)

for i = 1, #lines do
	term.setCursorPos(xBump, yBump + i)
	term.write(lines[i])
end
PieCrafted #3
Posted 05 August 2015 - 05:09 AM
Well, you'd need to consider the number of lines, and their width. Figuring out those two values is done by inspecting the string to see where the newline characters (\n) are.

Eg, using string.find() and a loop, we can shove each line into a table:

local pos, lines, width = 1, {}, 0
repeat
	local cur = text:find("\n", pos)
	
	if cur then  -- If we found a new break,
		lines[#lines + 1] = text:sub(pos, cur - 1)
		if #lines[#lines] > width then width = #lines[#lines] end
		pos = cur + 1
	elseif text:sub(#text) ~= "\n" then  -- or, if the last line also contains text,
		lines[#lines + 1] = text:sub(pos)
		if #lines[#lines] > width then width = #lines[#lines] end
	end
until not cur

Now we compare the size of the art to that of your display, and render accordingly:

local xBump, yBump = term.getSize()
xBump, yBump = math.floor((xBump - width) / 2) + 1, math.floor((yBump - #lines) / 2)

for i = 1, #lines do
	term.setCursorPos(xBump, yBump + i)
	term.write(lines[i])
end

Thank you a ton.

You are a true genius!