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

[Lua] [Question] Looking for a couple pointers (I/O)

Started by Justforlawls, 04 August 2012 - 06:36 AM
Justforlawls #1
Posted 04 August 2012 - 08:36 AM
General Backstory:
I've become known on a couple servers for making billboards and programs so that I/others can easily change the message. However, as I hope all of you know, there is a limit to the size of a monitor. I currently make programs where it prompts the user for line 1, line 2, etc. and assigns variables with io.read(), but I'm not satisfied.

First, I want a way to automatically split a single string into lines. I think the best way to do this would be to find the positions of all the spaces in the message so it doesn't split words, and then split the string at the last space before the width of a screen, and repeat that five or six times to make a separate string for each line on the monitor.

Second, I'd like a way to limit the size of the input string while letting the user know. An example would be like a form on a website that stops displaying the characters after you've entered a certain amount. This part sounds simple to me but I can't quite figure it out.

I'd like to clarify that I am NOT asking for actual code, but mainly the functions that I would need for some of the operations that I need. I have a general understanding of lua in computercraft but I'm just missing what seems like a few puzzle pieces.
Ponder #2
Posted 04 August 2012 - 10:23 AM
@First idea: Yeah, that would be what I also would do.
@Second idea: Well, I don't think you come around writing your own read function which pulls os events directly and counts them until a certain limit is reached.
Justforlawls #3
Posted 04 August 2012 - 11:22 AM
@Second idea: Well, I don't think you come around writing your own read function which pulls os events directly and counts them until a certain limit is reached.
Does that just mean you think it's too advanced for someone like me?
Ponder #4
Posted 04 August 2012 - 12:42 PM
Nah, not really. Have a look at this.
Pharap #5
Posted 04 August 2012 - 12:57 PM
First one sounds a little bit awkward, but it should be easy enough to do.

For the no overflow thing, set a variable to the screenwidth, go to that position in the string, check if it's a space, if it isn't, subtract 1 from the variable and rety until it is a space, then insert the newline character. Repeat until end of string.

For the typing thing, I think it would be easiest to alert the typer beforehand and then when they press enter, test if it's over, clear the console, alert them if it is, tell them how many characters they have to remove, then rewrite their message back to them (obviously stored in an input variable from earlier) and they'll have to retry.

That's the easy way, the hard way is handling the keydown event so that it checks how many characters have been written, clears the console, prints how many written, how many left to write and how many over, then reprints what the typer just put. Depends how confident you are with event handling. I haven't started learning event handling yet so I can't help you there, but I can help write the code for the other stuff.
Ponder #6
Posted 04 August 2012 - 02:07 PM
Well, I think the easy way would be pretty annoying, imagine you'd have to retype a message, just because you were a character over the limit. If you really want to limit what the user can type in (why, btw?) and really don't want to handle the events yourself, just cut the stuff which is over the limit off, like the Ingame Minecraft Chat does.