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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
package dashboard
import (
"fmt"
"strings"
"unicode/utf8"
common "ior/internal/tui/common"
"charm.land/lipgloss/v2"
)
// 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
// TabFlame is the live flamegraph tab.
TabFlame
)
var allTabs = []Tab{
TabFlame,
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"
case TabFlame:
return "Flame"
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 {
if width > 0 && width < 90 {
return renderTabBarPlain(active, width)
}
build := func(short bool) string {
parts := make([]string, 0, len(allTabs))
for i, tab := range allTabs {
label := fmt.Sprintf("%d:%s", i+1, tabLabel(tab, short))
if tab == active {
parts = append(parts, common.TabActiveStyle.Render(label))
} else {
parts = append(parts, common.TabInactiveStyle.Render(label))
}
}
return lipgloss.JoinHorizontal(lipgloss.Left, parts...)
}
bar := build(false)
if width > 0 && lipgloss.Width(bar) > width {
bar = build(true)
}
if width > 0 && lipgloss.Width(bar) > width {
label := fmt.Sprintf("%d:%s", tabIndex(active)+1, tabLabel(active, false))
bar = common.TabActiveStyle.Render(label)
}
if width <= 0 {
return bar
}
styled := lipgloss.NewStyle().Width(width).Render(bar)
if strings.Contains(styled, "\n") {
return renderTabBarPlain(active, width)
}
return styled
}
func renderHelpBar(keys common.KeyMap, width int) string {
return renderHelpBarWithStatus(keys, width, "")
}
func renderHelpBarWithStatus(keys common.KeyMap, width int, status string) string {
sections := keys.DashboardStatusHelpSections()
lines := make([]string, 0, len(sections))
for _, section := range sections {
parts := make([]string, 0, len(section.Bindings))
for _, binding := range section.Bindings {
help := binding.Help()
parts = append(parts, help.Key+" "+help.Desc)
}
line := section.Title + ": " + strings.Join(parts, " • ")
if width > 0 {
line = truncatePlain(line, width)
}
lines = append(lines, line)
}
if status != "" && len(lines) > 0 {
lines[len(lines)-1] = appendStatusText(lines[len(lines)-1], status, width)
}
text := strings.Join(lines, "\n")
if width > 0 && width < 90 {
return text
}
return common.HelpBarStyle.Width(width).Render(text)
}
func renderHelpHint(width int) string {
return renderHelpHintWithStatus(width, "")
}
func renderHelpHintWithStatus(width int, status string) string {
hint := "press H for help"
if status != "" {
hint = appendStatusText(hint, status, width)
}
if width > 0 && width < 90 {
return hint
}
return common.HelpBarStyle.Width(width).Render(hint)
}
func appendStatusText(base, status string, width int) string {
if status == "" {
return base
}
line := base + " | " + status
if width > 0 {
return truncatePlain(line, width)
}
return line
}
func wrapHelpLines(parts []string, width int) (string, string) {
if len(parts) == 0 {
return "", ""
}
if width <= 0 {
return strings.Join(parts, " • "), ""
}
max := width
lines := []string{"", ""}
line := 0
for _, part := range parts {
token := part
if lines[line] != "" {
token = " • " + part
}
if utf8.RuneCountInString(lines[line]+token) <= max {
lines[line] += token
continue
}
if line == 0 {
line = 1
if utf8.RuneCountInString(part) <= max {
lines[line] = part
}
continue
}
break
}
lines[0] = truncatePlain(lines[0], max)
lines[1] = truncatePlain(lines[1], max)
return lines[0], lines[1]
}
func tabLabel(tab Tab, short bool) string {
if !short {
return tab.String()
}
switch tab {
case TabOverview:
return "Ovr"
case TabSyscalls:
return "Sys"
case TabFiles:
return "Fil"
case TabProcesses:
return "Pro"
case TabLatency:
return "Lat"
case TabStream:
return "Str"
case TabFlame:
return "Flm"
default:
return "Unk"
}
}
func truncatePlain(s string, width int) string {
if width <= 0 {
return ""
}
if utf8.RuneCountInString(s) <= width {
return s
}
if width == 1 {
return "…"
}
r := []rune(s)
return string(r[:width-1]) + "…"
}
func renderTabBarPlain(active Tab, width int) string {
parts := make([]string, 0, len(allTabs))
for i, tab := range allTabs {
label := fmt.Sprintf("%d:%s", i+1, tabLabel(tab, true))
if tab == active {
label = "[" + label + "]"
}
parts = append(parts, label)
}
text := strings.Join(parts, " ")
if width > 0 {
text = truncatePlain(text, width)
padding := width - utf8.RuneCountInString(text)
if padding > 0 {
text += strings.Repeat(" ", padding)
}
}
return text
}
|