HTML-like Text Forms

Started by Missooni, Nov 23, 2024, 06:00 AM

Previous topic - Next topic

Missooni


A couple of functions that can be used to create responsive text forms. Colors are fully customizable.

On advanced computers, text fields and submit button respond to mouse inputs, but forms can also be navigated using the keyboard:
Tab: Go forward/cycle through fields.
Ctrl: Go back
Enter: Submit, when last field is selected.

wget https://raw.githubusercontent.com/Missooni/CC/refs/heads/main/textForms/forms.lua
Usage Example
Code: Lua
  1. local forms = require("forms")
  2.  
  3. -- Create text fields and put them into a table, 'safe' fields will show input with asterisks.
  4. -- forms.field(submitX, submitY, submitWidth, textColor, bgColor, defaultText, defaultColor)
  5. -- defaultText and defaultColor are optional parameters.
  6. formContents = {
  7. forms.field(2,3,5,colors.black,colors.white),
  8. forms.field(9,3,5,colors.black,colors.white),
  9. forms.safe(2,5,12,colors.black,colors.white),
  10. }
  11.  
  12. term.clear()
  13. print("Text Form:")
  14. -- Build the text form and wait for input, nothing else will run until the 'Submit' button is pressed.
  15. -- Syntax:
  16. -- forms.build(tableWithFields, submitFunction, submitX, submitY, submitWidth, textColor, bgColor, defaultText)
  17. forms.build(
  18. formContents,
  19. function(results)
  20.     term.setCursorPos(1,9)
  21.     print("Submitted Form!")
  22.     print("Results:")
  23.     for i, v in pairs(results) do
  24.     print(i..": "..v.input)
  25.     end
  26.     term.setCursorPos(1,20)
  27. end,
  28. 3,7,10,colors.white,colors.red,"Submit")

Source Code (GitHub)
" We are like the spider. We weave our life and then move along in it.
We are like the dreamer, who dreams and then lives in the dream. "
- Aitareya Upanishad