Skip to content

Selectable Table

This component displays tabular data with row selection capabilities. Users can navigate through rows using keyboard controls and select a specific row. The component supports pagination for large datasets and returns the index of the selected row.

PropertyValue
InteractivityRequired (fails in non-interactive terminals)

Demo

A gif that shows the selectable table component in action

API

Basic Example

swift
// Simple seelctable table with string arrays
let noora = Noora()
let selectedIndex = try await noora.selectableTable(
    headers: ["Name", "Version", "Status"],
    rows: [
        ["React", "18.2.0", "Active"],
        ["Vue", "3.3.4", "Active"],
        ["Angular", "16.1.0", "Deprecated"],
        ["Svelte", "4.0.5", "Active"],
        ["Ember", "5.1.0", "Maintenance"]
    ],
    pageSize: 3
)
print("Selected row index: \(selectedIndex)")

Advanced Example with TableData

swift
// Advanced selectable table with custom styling and column configuration
let columns = [
    TableColumn(title: "Package", width: .flexible(min: 10, max: 20), alignment: .left),
    TableColumn(title: "Version", width: .fixed(10), alignment: .center),
    TableColumn(title: "Status", width: .auto, alignment: .right)
]

let rows: [[TerminalText]] = [
    [
        TerminalText("React"),
        TerminalText("\(.primary("18.2.0"))"),
        TerminalText("\(.success("Active"))")
    ],
    [
        TerminalText("Vue"),
        TerminalText("\(.primary("3.3.4"))"),
        TerminalText("\(.success("Active"))")
    ],
    [
        TerminalText("Angular"),
        TerminalText("\(.muted("16.1.0"))"),
        TerminalText("\(.warning("Deprecated"))")
    ],
    [
        TerminalText("Svelte"),
        TerminalText("\(.primary("4.0.5"))"),
        TerminalText("\(.success("Active"))")
    ]
]

let tableData = TableData(columns: columns, rows: rows)
let selectedIndex = try await noora.selectableTable(tableData, pageSize: 2)

// Use the selected index to get the corresponding data
let selectedRow = rows[selectedIndex]
print("Selected package: \(selectedRow[0].plain())")

Semantic Styling Example

swift
// Selectable table with semantic styling for better visual hierarchy
let headers: [TableCellStyle] = [
    .primary("Package"),
    .secondary("Version"),
    .accent("Status")
]

let rows: [StyledTableRow] = [
    [.plain("React"), .success("18.2.0"), .success("Active")],
    [.plain("Vue"), .success("3.3.4"), .success("Active")],
    [.plain("Angular"), .muted("16.1.0"), .warning("Deprecated")],
    [.plain("Svelte"), .success("4.0.5"), .success("Active")],
    [.plain("Ember"), .secondary("5.1.0"), .warning("Maintenance")]
]

let selectedIndex = try await noora.selectableTable(
    headers: headers,
    rows: rows,
    pageSize: 3
)

Error Handling

swift
do {
    let selectedIndex = try await noora.selectableTable(
        headers: ["Name", "Value"],
        rows: [["Item 1", "Value 1"], ["Item 2", "Value 2"]],
        pageSize: 5
    )
    print("User selected row: \(selectedIndex)")
} catch {
    print("Selectable table failed: \(error)")
    // This might happen in non-interactive terminals
}

Options

Basic selectable table method

AttributeDescriptionRequiredDefault value
headersArray of column header stringsYes
rowsArray of row data (each row is an array of strings)Yes
pageSizeNumber of rows visible at onceYes
rendererA rendering interface that holds the UI stateNoRenderer()

Advanced selectable table method with TableData

AttributeDescriptionRequiredDefault value
dataTableData object with custom columns and contentYes
pageSizeNumber of rows visible at onceYes
rendererA rendering interface that holds the UI stateNoRenderer()

Semantic styling method

AttributeDescriptionRequiredDefault value
headersArray of column headers with semantic stylingYes
rowsArray of row data with semantic stylingYes
pageSizeNumber of rows visible at onceYes
rendererA rendering interface that holds the UI stateNoRenderer()

The selectable table supports the following keyboard controls:

KeyAction
/ kMove selection up
/ jMove selection down
Enter / SpaceSelect current row
Page UpMove to previous page
Page DownMove to next page
HomeGo to first page
EndGo to last page
Esc / qCancel selection (throws error)

Return Value

The selectable table returns an Int representing the zero-based index of the selected row in the original data array. This allows you to:

  • Access the selected row data: rows[selectedIndex]
  • Perform actions based on the user's selection
  • Map the index to your domain objects

Error Conditions

The selectable table will throw an error in the following situations:

  • Non-interactive terminal: When running in a terminal that doesn't support interactive input
  • User cancellation: When the user presses Esc or q to cancel the selection
  • Empty data: When no rows are provided

Released under the MIT License.