summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/table.go
blob: c47b3050ad874e3d215171abdae393dee0085261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package dashboard

import (
	"fmt"
	"strings"

	common "ior/internal/tui/common"

	"charm.land/lipgloss/v2"
)

func renderSelectableTable(columns []common.TableColumn, rows [][]string, height, selectedRow, selectedCol int, rowHint string, extraHints ...string) string {
	if len(rows) == 0 {
		return ""
	}

	selectedRow = clampOffset(selectedRow, len(rows))
	selectedCol = common.ClampTableCol(selectedCol, len(columns))
	start, end := common.VisibleTableWindow(selectedRow, len(rows), syscallTableHeight(height))

	lines := make([]string, 0, end-start+2)
	lines = append(lines, common.RenderTableHeader(columns))
	for idx := start; idx < end; idx++ {
		col := -1
		if idx == selectedRow {
			col = selectedCol
		}
		lines = append(lines, common.RenderTableRow(columns, rows[idx], idx == selectedRow, col, lipgloss.Style{}))
	}

	hints := []string{fmt.Sprintf("Row %d/%d Col %d/%d", selectedRow+1, len(rows), selectedCol+1, len(columns))}
	if rowHint != "" {
		hints = append(hints, rowHint)
	}
	hints = append(hints, extraHints...)
	lines = append(lines, "["+strings.Join(hints, "] [")+"]")
	return strings.Join(lines, "\n")
}

func tablePageStep(height int) int {
	rows := syscallTableHeight(height)
	if rows <= 1 {
		return 1
	}
	return rows - 1
}