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

Input - basically read(), but way better

Started by LoganDark2, 30 January 2018 - 01:35 PM
LoganDark2 #1
Posted 30 January 2018 - 02:35 PM
Input is a library made in just a few hours out of boredom. It's intended to be a drop-in replacement for read(). And by drop-in, I mean you can do this:



Here's the fun part: Input supports a lot more than stock read() does. That includes everything read() can do, in addition to:
  • Max length (it goes to the right edge by default, just like regular read(), but you can finally change it!)
  • Option to disable print() (useful for reading at the bottom of the screen, where stock read()'s forced print() moves the entire screen down!)
  • Define where the cursor starts at as well as the default text
  • Colors! Input can do syntax highlighting (self promo, and example program here), or whatever colored text you like. There's an example program here that highlights pairs of parenthesis (or square brackets, or curly brackets)
  • Mouse support
  • Selections! Click and drag, or hold shift and move the cursor
The argument order is:
  1. Censor: a one-character long string specifying which character each character of the entered text will be replaced with. Default: nil
  2. History: a numerically indexed table specifying the past inputs, the last index being the most recent. Default: empty table
  3. Autocomplete: a function taking a single argument, the current line, that returns a numerically indexed table of all possible strings that could be used to complete the text, assuming the cursor is always at the end of the line. Default: empty function
  4. Default: text that will appear as if the user typed it. Default: empty string
  5. Max length: a number that specifies the number of characters horizontally the input can take up. Default: distance from the current cursor position to the right edge of the screen.
  6. Print after: a boolean that specifies whether or not the input() function will print a newline after enter is pressed. Default: true
  7. Start position: a number that specifies where in the default text the cursor will be placed. NOTE: This is ZERO-indexed, not ONE-indexed like most of Lua! Default: the length of the current line (0 if default is not specified)
  8. Colorizer: a function that should return two strings to be used with term.blit, in order of foreground and then background. Default: just returns white on black, with the completion being black on (not light) gray.
The colorizer should take 5 arguments:
  1. The current line: string.
  2. The cursor position: number.
  3. The current scroll position (number of characters starting from the left that are not shown): number.
  4. Whether this is the last draw (when Enter is pressed, the function is immediately called one last time): boolean.
  5. The current completion: string. This will be in the blit, so you are expected to provide (concatenate at the end) foreground/background information for it as well.
It can be installed using pastebin get 7bBdj5Lw input. Then, you can load it using dofile('input'). Sorry, but os.loadAPI is not supported (and will not give you results).

Any suggestions to improve this post are appreciated.
Edited on 19 February 2018 - 10:13 PM
LoganDark2 #2
Posted 19 February 2018 - 10:41 PM
No feedback at all? No comments?
Bomb Bloke #3
Posted 20 February 2018 - 01:07 AM
It seems to me that it'd be easier to have the script file override _G.read on its own, so that you don't have to enter the Lua console to invoke it. It could still return the function for the sake of backwards compatibility.
Saldor010 #4
Posted 20 February 2018 - 04:53 AM
No feedback at all? No comments?

I can't answer for anyone else, but I thought it looked fairly interesting when I saw the post, I just haven't had time to try it out (and posting a comment on mobile is a pain for me). It's always bugged me how read() works in Lua, so I'm glad that someone fixed my issues with it ^_^/>
LDDestroier #5
Posted 20 February 2018 - 05:41 AM
I looked over the source code to see if you copied textutils.prompt() so I could rub it in your face, but alas, it's all original code. I haven't tested it, but a read prompt like the one you are providing is sorely needed and greatly appreciated.

I made a function called funcread() which tried to be a better read function for Enchat and other things, but it's not as stable as read().
LoganDark2 #6
Posted 20 February 2018 - 07:57 AM
It seems to me that it'd be easier to have the script file override _G.read on its own, so that you don't have to enter the Lua console to invoke it. It could still return the function for the sake of backwards compatibility.

It's a library. Overriding _G.read is something that can be done by the user. Anyone genuinely interested in this library should have the knowledge to write their own script to replace _G.read if they really want to.
LoganDark2 #7
Posted 20 February 2018 - 07:59 AM
I looked over the source code to see if you copied textutils.prompt() so I could rub it in your face, but alas, it's all original code. I haven't tested it, but a read prompt like the one you are providing is sorely needed and greatly appreciated.

I made a function called funcread() which tried to be a better read function for Enchat and other things, but it's not as stable as read().

Maybe in the future I could try stress testing this library so hard the computer explodes…

Dunno. If it's a Command Computer, it actually could explode as the result of a malicious program…
LoganDark2 #8
Posted 10 March 2018 - 06:57 PM
[…] see if you copied textutils.prompt() […]

Wait, is textutils.prompt even a thing? I can't find it in the wiki anywhere, and I can't even find it on an emulated computer.
LDDestroier #9
Posted 11 March 2018 - 05:28 AM
[…] see if you copied textutils.prompt() […]

Wait, is textutils.prompt even a thing? I can't find it in the wiki anywhere, and I can't even find it on an emulated computer.

It was in a commit on GitHub somewhere.
LoganDark2 #10
Posted 11 March 2018 - 05:29 AM
[…] see if you copied textutils.prompt() […]

Wait, is textutils.prompt even a thing? I can't find it in the wiki anywhere, and I can't even find it on an emulated computer.

It was in a commit on GitHub somewhere.

Hmm.. Well, in any case, I don't think anyone needs a second read function…

…in the standard library. Improved or re-done read functions (like hey, this one) are always nice.
Bomb Bloke #11
Posted 11 March 2018 - 05:30 AM
https://github.com/dan200/ComputerCraft/pull/470

Some builds floating around the place include it, but Dan hasn't accepted it into the main repo.