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

Custom writing position functions, how can I implement multi-line support?

Started by augustas656, 08 May 2014 - 07:19 PM
augustas656 #1
Posted 08 May 2014 - 09:19 PM
I have an API which contains features such as write left, write centre and write right, these features allow writting FROM an area, not the read function, but instead when you print or write. Centre is obviously centered text, right is when it's written not reverse but the text field's right side is touching the right screen border. These functions I have also allow a bit more customisation, which is optional in the parameters. Allowing you to perhaps centre it in between a certain area rather than from the left to the right border of the screen and the right write function allows you writting not exactly next to the right border but futher away if you want.

If I use the center function and write text more than 51 characters long, that text will just fill the screen and the next line if below also being 51 characters long won't be centered anymore, it will be at cursor x pos at 1 and will write normally. I'd like to implement multi-line support. Now my API has an easy way of getting the amount of lines by getLines(value) the value can be anything that can be tostringed. I could perhaps do this in some time but my head isn't working, so maybe someone already has such a code written. That'd be nice, otherwise if you can help me, thanks! :)/>

P.S. If you really need my code I can give you some of the functions in my api that are relevant to the multi-line support I really want.

Regards,
Augustas
CometWolf #2
Posted 08 May 2014 - 09:30 PM
How would you off-center a 51 character long line? 51 Characters would cover the entire line…
Anyways, if you know the amount of lines you need, it's pretty simple

local lineLength = termX-startX+1
for i=1,getLines(line) do
  local curLine = line:sub(1,lineLength)
  line = line:sub(lineLength+1)
  term.setCursorPos(startX,i)
  term.write(curLine)
end
Obvoiusly if you want each line centered seperately, you'd just put your calculations into the loop, based on how long the line is.
Edited on 08 May 2014 - 08:57 PM
augustas656 #3
Posted 08 May 2014 - 10:24 PM
I was thinking perhaps, if there are more than two lines, this is an if check, then you split the string into the string's length divided by screen's width, and the answer is done math.ceil to, then each string is centered seperately. How can I do something like this, a string of 186 characters length is split into the first 51 characters, the second 51 characters, the third 51 characters and the last being 33 characters. So like keep splitting a string at the position 51 (screen's width) until it's smaller than 51 (screen's width), then you centre all the split strings and the last 33 characters-length string.
KingofGamesYami #4
Posted 08 May 2014 - 10:34 PM
I was thinking perhaps, if there are more than two lines, this is an if check, then you split the string into the string's length divided by screen's width, and the answer is done math.ceil to, then each string is centered seperately. How can I do something like this, a string of 186 characters length is split into the first 51 characters, the second 51 characters, the third 51 characters and the last being 33 characters. So like keep splitting a string at the position 51 (screen's width) until it's smaller than 51 (screen's width), then you centre all the split strings and the last 33 characters-length string.

--#x = screen width

local text = "yourtexthere"
local tText

for i = 1, #text, x do
 tText[i] = text:sub(i, i + x)
end
Would something like this work? It makes a table of the strings *note: I wrote this in 5 seconds, I don't expect it to work correctly the first time
Edited on 08 May 2014 - 08:34 PM
CometWolf #5
Posted 08 May 2014 - 10:56 PM
I was thinking perhaps, if there are more than two lines, this is an if check, then you split the string into the string's length divided by screen's width, and the answer is done math.ceil to, then each string is centered seperately. How can I do something like this, a string of 186 characters length is split into the first 51 characters, the second 51 characters, the third 51 characters and the last being 33 characters. So like keep splitting a string at the position 51 (screen's width) until it's smaller than 51 (screen's width), then you centre all the split strings and the last 33 characters-length string.
The if check isn't really needed, since just one line would work fine aswell. As i said, the code i posted above can easily do what you want, if you add the calculations for each line.