formui v0.2.0

A dynamic form user interface library for ComputerCraft that provides interactive forms with various field types, validation, and peripheral detection. Features: Text and number input fields, select dropdowns and peripheral selection, checkbox/toggle fields, multi-select dropdowns, list fields with item management, built-in validation system, labels and buttons, real-time peripheral detection, keyboard navigation with arrow keys, and form submission and cancellation.

Installation

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua formui
View on GitHub →

This pattern downloads and runs libraries at runtime, automatically installing any that are missing:

-- Auto-install and require libraries
local libs = {"formui"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local formui = require(libDir .. "formui")

-- Use the library
-- (your code here)

Alternative: Direct download via wget:

wget https://raw.githubusercontent.com/Twijn/cc-misc/main/util/formui.lua

Examples

Using with Runtime Installation

This example shows how to download and use libraries with automatic installation:

-- Auto-install and require libraries
local libs = {"formui"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local formui = require(libDir .. "formui")

-- Use the library

Usage Examples

local FormUI = require("formui")
local form = FormUI.new("Configuration")
local nameField = form:text("Name", "default")
local portField = form:number("Port", 8080)
local modemField = form:peripheral("Modem", "modem")
local enabledField = form:checkbox("Enabled", true)
local featuresField = form:multiselect("Features", {"feature1", "feature2", "feature3"})
local itemsField = form:list("Items", {"item1", "item2"}, "string")
form:addSubmitCancel()
local result = form:run()
if result then
 print("Name:", nameField())
 print("Port:", portField())
 print("Enabled:", enabledField())
 print("Features:", table.concat(featuresField(), ", "))
end

Functions

FormUI.new(title?)

View source on GitHub

Create a new FormUI instance

Parameters:
Returns: FormUI # New FormUI instance

FormUI:addField(field)

View source on GitHub

Add a field to the form and return a getter function

Parameters:
Returns: fun(): any # Function that returns the field's final value after form submission

FormUI:text(label, default?, validator?)

View source on GitHub

Add a text input field

Parameters:
Returns: fun(): string # Function to get the field value after submission

FormUI:number(label, default?, validator?)

View source on GitHub

Add a number input field

Parameters:
Returns: fun(): number # Function to get the field value after submission

FormUI:select(label, options?, defaultIndex?, validator?)

View source on GitHub

Add a select dropdown field

Parameters:
Returns: fun(): string # Function to get the selected option after submission

FormUI:peripheral(label, filterType?, validator?, defaultValue?)

View source on GitHub

Add a peripheral selector field that automatically detects peripherals

Parameters:
Returns: fun(): string # Function to get the selected peripheral name after submission

FormUI:label(text)

View source on GitHub

Add a non-interactive label for display purposes

Parameters:
Returns: fun(): string # Function to get the label text (always returns the same text)

FormUI:button(text, action?)

View source on GitHub

Add a button that can trigger actions

Parameters:
Returns: fun(): string # Function to get the button text

FormUI:checkbox(label, default?)

View source on GitHub

Add a checkbox/toggle field

Parameters:
Returns: fun(): boolean # Function to get the field value after submission

FormUI:multiselect(label, options, defaultIndices?)

View source on GitHub

Add a multi-select dropdown field

Parameters:
Returns: fun(): string[] # Function to get selected options after submission

FormUI:list(label, default?, itemType?)

View source on GitHub

Add a list field (string or number list, with item reordering)

Parameters:
Returns: fun(): table # Function to get the list after submission

FormUI:addSubmitCancel()

View source on GitHub

Add standard Submit and Cancel buttons to the form

FormUI:validateField(i)

View source on GitHub

Validate a specific field by index

Parameters:
Returns: string? error Error message if validation failed

FormUI:isValid()

View source on GitHub

Validate all fields in the form

Returns: boolean # True if all fields are valid

FormUI:get(label)

View source on GitHub

Get the current value of a field by label

Parameters:
Returns: any # The field's current value, or nil if not found

FormUI:setValue(label, value)

View source on GitHub

Set the value of a field by label

Parameters:
Returns: boolean # True if field was found and updated, false otherwise

FormUI:draw()

View source on GitHub

Draw the form to the terminal

FormUI:edit(index)

View source on GitHub

Edit a field at the specified index

Parameters:
Returns: string? action Action identifier if a button was pressed

FormUI:nextSelectableField(from)

View source on GitHub

Find the next selectable field index (skips labels)

Parameters:
Returns: number # Next selectable field index (wraps around)

FormUI:prevSelectableField(from)

View source on GitHub

Find the previous selectable field index (skips labels)

Parameters:
Returns: number # Previous selectable field index (wraps around)

FormUI:run()

View source on GitHub

Run the form's main input loop

Returns: FormResult? result Table of field values indexed by label, or nil if cancelled