summaryrefslogtreecommitdiff
path: root/internal/tui/dashboard/tabs.go
blob: a2fe36682e2e802a78ec8936f400ca4a1909b58c (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package dashboard

import (
	"fmt"
	common "ior/internal/tui/common"
	"strings"

	"github.com/charmbracelet/lipgloss"
)

// Tab is a dashboard tab identifier.
type Tab int

const (
	// TabOverview is the high-level summary tab.
	TabOverview Tab = iota
	// TabSyscalls is the syscall table tab.
	TabSyscalls
	// TabFiles is the file ranking tab.
	TabFiles
	// TabProcesses is the process breakdown tab.
	TabProcesses
	// TabLatency is the latency histogram tab.
	TabLatency
	// TabStream is the live event stream tab.
	TabStream
)

var allTabs = []Tab{
	TabOverview,
	TabSyscalls,
	TabFiles,
	TabProcesses,
	TabLatency,
	TabStream,
}

func (t Tab) String() string {
	switch t {
	case TabOverview:
		return "Overview"
	case TabSyscalls:
		return "Syscalls"
	case TabFiles:
		return "Files"
	case TabProcesses:
		return "Processes"
	case TabLatency:
		return "Latency+Gaps"
	case TabStream:
		return "Stream"
	default:
		return "Unknown"
	}
}

func nextTab(tab Tab) Tab {
	idx := tabIndex(tab)
	return allTabs[(idx+1)%len(allTabs)]
}

func prevTab(tab Tab) Tab {
	idx := tabIndex(tab)
	if idx == 0 {
		return allTabs[len(allTabs)-1]
	}
	return allTabs[idx-1]
}

func tabIndex(tab Tab) int {
	for i, candidate := range allTabs {
		if candidate == tab {
			return i
		}
	}
	return 0
}

func renderTabBar(active Tab, width int) string {
	parts := make([]string, 0, len(allTabs))
	for i, tab := range allTabs {
		label := fmt.Sprintf("%d:%s", i+1, tab.String())
		if tab == active {
			parts = append(parts, common.TabActiveStyle.Render(label))
		} else {
			parts = append(parts, common.TabInactiveStyle.Render(label))
		}
	}

	bar := lipgloss.JoinHorizontal(lipgloss.Left, parts...)
	if width <= 0 {
		return bar
	}
	return lipgloss.NewStyle().Width(width).Render(bar)
}

func renderHelpBar(keys common.KeyMap) string {
	parts := make([]string, 0, len(keys.DashboardShortHelp()))
	for _, binding := range keys.DashboardShortHelp() {
		help := binding.Help()
		parts = append(parts, help.Key+" "+help.Desc)
	}
	return common.HelpBarStyle.Render(strings.Join(parts, " • "))
}