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:
Spoiler
This 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!