The selectableTable method allows users to navigate and select a row even while the underlying data is continuously updating. This is useful when you need to pick an item from a list that changes frequently (e.g., a list of active network connections).
When using selectableTable, you typically maintain a local reference to the latest data snapshot to ensure that when a selection index is returned, you can map it back to the correct row content.
let columns = [
TableColumn(title: "SSID", width: .auto, alignment: .left),
TableColumn(title: "Signal", width: .auto, alignment: .right),
]
var latest = TableData(columns: columns, rows: [["Home", "-40 dBm"].map(TerminalText.init)])
let updates = AsyncStream<TableData> { continuation in
Task.detached {
while !Task.isCancelled {
latest = makeSnapshot() // Build new TableData from your source
continuation.yield(latest)
try? await Task.sleep(for: .seconds(1))
}
continuation.finish()
}
}
let selectedIndex = try await Noora().selectableTable(
latest,
updates: updates,
pageSize: 8
)
let selectedRow = latest.rows[selectedIndex]
print("Picked network: \(selectedRow[0].plain())")