Overview
TheSelect widget displays a list of options that users can navigate with arrow keys and select. Itβs useful for menus, dropdowns, and any UI that requires choosing from a list.
Constructor
SelectOptions
Initial list of options to display. Can be modified later using
addOption(), removeOption(), and clearOptions().Width of the select widget. Accepts:
- Number: fixed width in cells
- String: percentage (e.g.,
"50%") or"auto"
Height of the select widget. Accepts:
- Number: fixed height in rows
- String: percentage (e.g.,
"100%") or"auto"
Foreground (text) color. Accepts:
- Hex string:
"#FF0000" - Color name:
"red","blue", etc. - 256-color code:
196
Background color. Same format as
fg.Border style. Valid values:
"none"- no border"single"- single-line border"double"- double-line border"rounded"- rounded corners"bold"- bold border
Methods
addOption()
Add an option to the end of the list.The option text to add.
ts/src/widgets/select.ts:34
removeOption()
Remove an option by index.Zero-based index of the option to remove. Does nothing if index is out of range.
ts/src/widgets/select.ts:42
clearOptions()
Remove all options from the list.ts/src/widgets/select.ts:46
getOptionCount()
Get the total number of options.The number of options in the list.
ts/src/widgets/select.ts:50
getOption()
Get the text of a specific option.Zero-based index of the option to retrieve.
The option text at the specified index.
ts/src/widgets/select.ts:56
setSelected()
Set the currently selected option.Zero-based index of the option to select. Will be clamped to valid range.
ts/src/widgets/select.ts:63
getSelected()
Get the currently selected option index.Zero-based index of the selected option. Returns
-1 if no options exist.ts/src/widgets/select.ts:67
Examples
Inherited Methods
Select inherits all methods from the Widget base class, including:setVisible(visible)- Control visibilitysetForeground(color)- Set text colorsetBackground(color)- Set background colorsetBorderStyle(style)- Change border styledestroy()- Clean up resources
Event Handling
Select widgets respond to keyboard events:- Up/Down arrows - Navigate between options
- Page Up/Down - Jump multiple options
- Home/End - Jump to first/last option
- Enter - Confirm selection (application handles this)
app.drainEvents() in your event loop.
Notes
- Selection is automatically clamped to valid range
[0, optionCount-1] - Selected option is visually highlighted (styling depends on theme)
- Options have a 256-byte limit (UTF-8 encoded) due to fixed buffer size
- Use arrow keys for navigation; the selected index updates automatically
- For dropdown-style menus, combine with visibility controls
- Select widgets cannot have children