New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add textutils.prompt() #470
base: master
Are you sure you want to change the base?
Conversation
textutils.prompt is the brainchild of numerous people that have attempted to contribute to ComputerCraft. It is the read function on steroids, if you will. Features: - All arguments are put into a table, so that they don't have to be put in any particular order, like read(). - Type error protection has been condensed into a more understandable form. - Adds a few arguments, including those from read(), and others that would never make it into read(): - replaceChar (_sReplaceChar), history (_tHistory), complete (_fnComplete), prefix (_sDefault); all arguments from read - limit (number): As defined in dan200#387 - newline (boolean): As defined in dan200#386 - completeBGColor (number): Changes the Background color of the suggested completion values - completeTextColor (number): Changes the Text color of the suggested completion values What's really great about this function being used over read is that it allows for both command lines, and applications with a GUI alike to have greater customizability, over read(). Hopefully in the future this may be used in replacement of read(). Along with this, my hope with prompt is to have people that have wanted a specific feature implemented into read(), be put here. As there is no particular order to the arguments, it's all subject to the users need of it.
| term.setCursorPos( sx, cy ) | ||
| local sReplace = (_bClear and " ") or _tOptions.replaceChar | ||
| if sReplace then | ||
| term.write( string.sub( string.rep( sReplace, math.max( string.len(sLine) + nScroll, 0 ) ), nScroll + 1, nScroll + w ) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be shortened to:
term.write( sReplace:rep(math.max( #sLine + nScroll, 0 ):sub( nScroll + 1, nScroll + w ) )
| if sReplace then | ||
| term.write( string.sub( string.rep( sReplace, math.max( string.len(sLine) + nScroll, 0 ) ), nScroll + 1, nScroll + w ) ) | ||
| else | ||
| term.write( string.sub( sLine, nScroll + 1, nScroll + w ) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be shortened to
term.write( sLine:sub( nScroll + 1, nScroll + w) )
| -- Find the common prefix of all the other suggestions which start with the same letter as the current one | ||
| local sCompletion = tCompletions[ nCompletion ] | ||
| sLine = sLine .. sCompletion | ||
| nPos = string.len( sLine ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be shortened to
nPos = #sLine
|
|
||
| elseif param == keys.right then | ||
| -- Right | ||
| if nPos < string.len(sLine) then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if nPos < #sLine then
| -- Delete | ||
| if nPos < string.len(sLine) then | ||
| clear() | ||
| sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sLine = sLine:sub(1, nPos) .. sLine:sub( nPos, 2 )
| end | ||
| if nhistoryPos then | ||
| sLine = _tOptions.history[nhistoryPos] | ||
| nPos = string.len( sLine ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nPos = #sLine
| elseif sEvent == "paste" then | ||
| -- Pasted text | ||
| clear() | ||
| sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sLine = sLine:sub( 1, nPos ) .. param .. sLine:sub( nPos + 1 )
| if sEvent == "char" then | ||
| -- Typed key | ||
| clear() | ||
| sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sLine = sLine:sub( 1, nPos ) .. param .. sLine:sub( nPos + 1 )
| end | ||
| end | ||
| if sReplace then | ||
| term.write( string.rep( sReplace, string.len( sCompletion ) ) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
term.write( sReplace:rep( #sCompletion ) )
| -- Pasted text | ||
| clear() | ||
| sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 ) | ||
| nPos = nPos + string.len( param ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nPos = nPos + #param
|
@lurr Most everything you nitpicked was stuff that is in the original read function. I tried to keep everything as close as possible to the style that read() was written. While your proposed changes may be nice, the goal of this PR is not to redefine the style of ComputerCraft scripts. |
|
@hugeblank I'd argue that keeping the exact code style in line with CC's original style isn't a good reason to avoid this kind of thing. Using colon notation and the |
|
Also, I have a few suggestions:
|
|
And I'd argue that "keeping things in line with CC's style" would be more of a reason to ditch the "arguments via a table" thing, if you really want to play that card. |
|
>.< conflicting opinions, If the general consensus is to go through with lurs changes then I will... unless by divine intervention (Dan), things go the other way. @apemanzilla I'm going to need more explanation on what you mean by points 2 and 3... even though point 3 is supposedly self explanatory, |
|
Re the filter function, the idea is that the user presses a key, the char / paste gets sent to the function, and its return value is then treated as the actual input. For example, for pincode entry, you might write a filter function that returns "" for anything non-numeric. |
|
@hugeblank BB pretty much got it right with regards to the filter function. A function that's called for all input, and can change or negate it accordingly. For example, if I wanted to ignore the letter By configurable keys, I meant adding options to change which keys (or key combos) did what - e.g. change the history forward/backward keys from up/down to page up/page down. |
|
Okay, I understand the idea of the filter function, and I think I could make it happen. |
This commit adds the 2 primary requested features to textutils.prompt()
1) filter (function): add a function that filters out characters/strings from the user input, EX. `textutils.prompt({filter = function( sText ) return string.gsub(sText, "%D", "") end})`; filters out everything that isn't a digit.
2) customkeys (table): a table consisting of all the "special" keys in the function, things like "keys.up" or "keys.down" can now be redirected to any other key. Ex. `textutils.prompt({customkeys = {enter = keys.space}})`; sets the typical "enter" key to the spacebar.
whoops...
|
@apemanzilla @BombBloke Added both the filter function and the custom characters, killing and reviving slowWrite in the process. ...and I forgot the freaking help file, let me implement that |
|
I never liked slowWrite anyways. |
|
Edit: ^ Is that what you meant by adding it to help Unfortunately, it must live on... |
|
Well, it's better than nothing, but given how complex the function is it would be a good idea to expand on it some - maybe change it to |
|
My concern is that it isn't a big enough implementation to include an entire help article about it. I'll let this sit for a while and see what Bomb and others think. |
|
I mean, there's a lot of options already - enough to easily fill a screen. I don't think it would be a good idea to try and fit it all into the textutils page. |
|
I'm saying to include everything it implements at all, not adding it to the textutils help page. I think it would be better suited for when it gets put into release form, that way it can have it's own wiki page. |
|
this is what I would want the prompt.txt file to look something like if this it may be generally accepted. |
Mutilated bug that, when pasting something that a filter function saw as invalid, nPos went below 0. nPos is too hot to go sub zero.
1) Debugged customkeys so that everything now works as intended 2) The filter function now directly manipulates the output string, meaning you can now have single character and multi-character manipulation. (meaning profanity can now be watched ;D).
|
Can one of the admins verify this patch? |
textutils.prompt is the brainchild of numerous people that have attempted to contribute to ComputerCraft. SquidDev has mentioned this in various PR's as "super-duper READ-O-MATIC 4000 with turbo text entry", and to be quite honest, that's what it is. It is the read function on steroids, if you will.
Features:
What's really great about this function being used over read is that it allows for both command lines, and applications with a GUI alike to have greater customizability, over read(). Hopefully in the future this may be used in replacement of read().
Along with this, my hope with prompt is to have people that have wanted a specific feature implemented into read(), be put here. As there is no particular order to the arguments, it's all subject to the user's need of it.