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

Modified Read Function

Started by GravityScore, 15 February 2013 - 10:03 PM
GravityScore #1
Posted 15 February 2013 - 11:03 PM
Hey all,

I've written a relatively simple modified read function with a bit of very useful functionality, because I always find myself needing to limit the amount of text you can type, or have the read scroll earlier than the edge of the screen (for neatness). I thought I'd release it in case anyone else needs a bit of increased functionality, and can't be bothered writing their own modified read.

Documentation:
SpoilerThis function takes 1 argument which is an array of properties that it will use to customise the functionality, or no arguments (or an empty array) if you want to use it like the normal read function.

In the properties array, you set certain keys to values to customise functionality. The keys you are able to set are:
- startingText, sets the text that will appear in the text fields before the user starts typing. This text is editable.
- replaceCharacter, which will replace any key typed with the first letter of this string (like in the normal read)
- displayLength, which dictates how long the typed text can become before it starts scrolling (like the normal read function would if the typed text hit the edge of the screen)
- maxLength, which determines how long the typed text can be in length before it does not let you type any more characters
- onEvent, which is a function that will be called when an event occurs (so you can implement buttons along side text boxes for example). The function takes the parameters (in order) of: currently typed text, event type, event parameter 1, event parameter 2, event parameter 3, …

Example usage:

local text = modifiedRead() -- works like the normal read function

local text = modifiedRead({replaceCharacter = "*"}) -- works like replace character in the normal read function

-- You can type 20 characters before it starts scrolling, and 30 before it doesn't let you type anymore
local text = modifiedRead({displayLength = 20, maxLength = 30})

-- Prints out the last event that occurred
local text = modifiedRead({onEvent = function(text, event, key, x, y, param4, param5)
  term.setCursorPos(1, 3)
  term.clearLine()
  write(event)
end})

Usage:
This is a separate function, so you can either use it by copy and pasting the whole thing into your code, or place it into a separate file, shell.run that file, then use the function (named modifiedRead).

Download:
Can be found on Pastebin with the ID: 5FpdD6L1
Or type into your computer:

pastebin get 5FpdD6L1 modread

Feel free to use it anywhere in your code. Credit isn't needed if you don't want to, but it is always appreciated!
Edited on 06 January 2014 - 11:14 AM
superaxander #2
Posted 16 February 2013 - 03:06 AM
That's handy for making locks more secure so the stack doesn't overflow!
And under what condition may you use it. Cause I want to use it for my upcoming project. Which will be full of security :)/>.
GravityScore #3
Posted 16 February 2013 - 03:21 AM
That's handy for making locks more secure so the stack doesn't overflow!
And under what condition may you use it. Cause I want to use it for my upcoming project. Which will be full of security :)/>.

I don't really care how you use it :P/> Credit's always nice, but if you don't want to I really don't mind :P/>
superaxander #4
Posted 16 February 2013 - 03:42 AM
Oh I am sure I will give credits :P/>
Cranium #5
Posted 16 February 2013 - 07:15 AM
Oh, I made something like this a long time ago. Nice to see these things getting around, so GUIs don't get all derped to hell.
TheOddByte #6
Posted 26 May 2013 - 02:21 PM
Can I just say something?
I freakin' love this! :)/>
And ofcourse I'll give you credits when using this ;)/>
GravityScore #7
Posted 06 January 2014 - 10:40 AM
Updated the function to be a lot shorter, but got rid of a lot of (for me at least) unneeded functionality. It doesn't currently support history, like the normal read function, but I haven't found a use for that yet personally. The displayLength property now works as expected :P/> and you can click anywhere along the typed text to move the cursor there.
Edited on 06 January 2014 - 10:47 AM
theoriginalbit #8
Posted 06 January 2014 - 09:54 PM
and you can click anywhere along the typed text to move the cursor there.
ooo, that's probably a good thing to add to my one :P/>