Class ui.panel.TabStrip
TabStrip panel for displaying horizontal tab labels.
This class creates a single-line panel that displays a horizontal list of tab labels with viewport scrolling and navigation capabilities. TabStrip is responsible only for the strip of labels and navigation between tab-labels.
A typical implementation would use a panel, vertically split, with the TabStrip in the top panel and the content panels (usually a tab-set panel) in the bottom panel. See ui.panel.Set, and the example tabstrip.lua.
Methods
| ui.panel.tabstrip:add_item (item[, before_id]) | Add a new item to the items list. |
| ui.panel.tabstrip:get_items () | Get a copy of the items table. |
| ui.panel.tabstrip:get_selected () | Get the currently selected tab id. |
| ui.panel.tabstrip:init (opts) | Create a new TabStrip instance. |
| ui.panel.tabstrip:remove_item (id) | Remove an item from the items list. |
| ui.panel.tabstrip:select (tab_id) | Select a tab by id. |
| ui.panel.tabstrip:select_next () | Select the next tab. |
| ui.panel.tabstrip:select_prev () | Select the previous tab. |
| ui.panel.tabstrip:set_items (items) | Set the items table. |
Methods
- ui.panel.tabstrip:add_item (item[, before_id])
-
Add a new item to the items list.
Parameters:
- item
table
The item to add (must have
labelfield). - before_id any If provided, insert before the item with this id. (optional)
Returns:
-
nothing
Usage:
tab_strip:add_item({ id = "tab3", label = "Tab 3" }) tab_strip:add_item({ id = "tab2.5", label = "Tab 2.5" }, "tab3")
- item
table
The item to add (must have
- ui.panel.tabstrip:get_items ()
-
Get a copy of the items table.
Returns:
-
table
A copy of the items array.
Usage:
local items = tab_strip:get_items()
- ui.panel.tabstrip:get_selected ()
-
Get the currently selected tab id.
Returns:
- any or nil The selected tab id, or nil if no tabs exist.
- string or nil Error message if no tabs exist.
Usage:
local selected_id, err = tab_strip:get_selected() if err then print("No tabs available") else print("Selected tab:", selected_id) end
- ui.panel.tabstrip:init (opts)
-
Create a new TabStrip instance.
Do not call this method directly, call on the class instead.
Parameters:
- opts Configuration options (see Panel:init for inherited properties)
- items
table
Array of tab items, each with
labelfield (required) and optionalidfield. - selected any Initial selected tab id. (optional)
- prefix string Prefix string for tab labels. (default "[")
- postfix string Postfix string for tab labels. (default "[")
- padding number Number of spaces between tabs. (default 1)
- attr table Text attributes for the entire strip. (optional)
- selected_attr table Text attributes for the selected tab. (optional)
- select_cb
function
Callback function called when selection changes:
select_cb(self, id). (optional)
- items
table
Array of tab items, each with
Returns:
-
TabStrip
A new TabStrip instance.
Usage:
local TabStrip = require("terminal.ui.panel.tab_strip") local tab_strip = TabStrip { items = { { id = "tab1", label = "Tab 1" }, { id = "tab2", label = "Tab 2" } }, selected = "tab1", attr = { fg = "white", bg = "black" }, selected_attr = { reverse = true } }
- opts Configuration options (see Panel:init for inherited properties)
- ui.panel.tabstrip:remove_item (id)
-
Remove an item from the items list.
Parameters:
- id any The id of the item to remove.
Returns:
- boolean or nil True on success, or nil if id not found.
- string or nil Error message if id not found.
Usage:
local success, err = tab_strip:remove_item("tab2")
- ui.panel.tabstrip:select (tab_id)
-
Select a tab by id.
Parameters:
- tab_id any The id of the tab to select.
Returns:
- boolean or nil True on success, or nil if id not found.
- string or nil Error message if id not found.
Usage:
local success, err = tab_strip:select("tab2") if not success then print("Error:", err) end
- ui.panel.tabstrip:select_next ()
-
Select the next tab.
Returns:
- any or nil The selected tab id, or nil if no tabs exist.
- string or nil Error message if no tabs exist.
Usage:
local selected_id, err = tab_strip:select_next() if err then print("Error:", err) end
- ui.panel.tabstrip:select_prev ()
-
Select the previous tab.
Returns:
- any or nil The selected tab id, or nil if no tabs exist.
- string or nil Error message if no tabs exist.
Usage:
local selected_id, err = tab_strip:select_prev() if err then print("Error:", err) end
- ui.panel.tabstrip:set_items (items)
-
Set the items table.
Parameters:
- items table The new items array.
Returns:
-
nothing
Usage:
tab_strip:set_items({ { id = "tab1", label = "Tab 1" }, { id = "tab2", label = "Tab 2" } })