summaryrefslogtreecommitdiff
path: root/internal/tui/common/keys.go
blob: e50ee943f60c6cdfff95958dbd0ccd3bd85d05e2 (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
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
package common

import "charm.land/bubbles/v2/key"

// HelpSection groups related key bindings under a shared heading.
type HelpSection struct {
	Title    string
	Bindings []key.Binding
}

// KeyMap groups all key bindings shared by TUI screens.
type KeyMap struct {
	Tab         key.Binding
	ShiftTab    key.Binding
	One         key.Binding
	Two         key.Binding
	Three       key.Binding
	Four        key.Binding
	Five        key.Binding
	Six         key.Binding
	Seven       key.Binding
	Visualize   key.Binding
	Metric      key.Binding
	Sort        key.Binding
	ReverseSort key.Binding
	DirGroup    key.Binding
	SelectPID   key.Binding
	SelectTID   key.Binding
	Probes      key.Binding
	Filter      key.Binding
	FilterUndo  key.Binding
	Export      key.Binding
	Record      key.Binding
	Quit        key.Binding
	Enter       key.Binding
	Esc         key.Binding
	Refresh     key.Binding
	// AutoReset toggles/cycles the dashboard's auto-reset timer. The
	// timer periodically clears aggregate state (same as Refresh) to
	// prevent unbounded growth of the flamegraph trie and stats engine.
	// Bound to capital `I` because lowercase `t` is the TID picker; we
	// keep `i` unbound so future use isn't blocked.
	AutoReset key.Binding
}

// Keys contains the default shared key map.
var Keys = DefaultKeyMap()

func keyBinding(desc string, keys ...string) key.Binding {
	return key.NewBinding(key.WithKeys(keys...), key.WithHelp(keys[0], desc))
}

// DefaultKeyMap builds the default key bindings used by models.
func DefaultKeyMap() KeyMap {
	return KeyMap{
		Tab:         keyBinding("next tab", "tab"),
		ShiftTab:    keyBinding("prev tab", "shift+tab"),
		One:         keyBinding("flame", "1"),
		Two:         keyBinding("overview", "2"),
		Three:       keyBinding("syscalls", "3"),
		Four:        keyBinding("files", "4"),
		Five:        keyBinding("processes", "5"),
		Six:         keyBinding("lat+gaps", "6"),
		Seven:       keyBinding("stream", "7"),
		Visualize:   keyBinding("viz", "v"),
		Metric:      keyBinding("metric", "b"),
		Sort:        keyBinding("sort table", "s"),
		ReverseSort: keyBinding("reverse sort", "S"),
		DirGroup:    keyBinding("dir group", "d"),
		SelectPID:   keyBinding("select pid", "p"),
		SelectTID:   keyBinding("select tid", "t"),
		Probes:      keyBinding("probes", "o"),
		Filter:      keyBinding("filter", "f"),
		FilterUndo:  keyBinding("undo filter", "F"),
		Export:      keyBinding("stream export", "e"),
		Record:      keyBinding("parquet rec", "R"),
		Quit:        keyBinding("quit", "q", "ctrl+c"),
		Enter:       keyBinding("select", "enter"),
		Esc:         keyBinding("back", "esc"),
		Refresh:     keyBinding("reset baseline", "r"),
		AutoReset:   keyBinding("cycle auto-reset", "I"),
	}
}

// DashboardStatusHelp returns expanded bindings for dashboard status bars.
func (k KeyMap) DashboardStatusHelp() []key.Binding {
	sections := k.DashboardStatusHelpSections()
	total := 0
	for _, section := range sections {
		total += len(section.Bindings)
	}
	bindings := make([]key.Binding, 0, total)
	for _, section := range sections {
		bindings = append(bindings, section.Bindings...)
	}
	return bindings
}

// DashboardStatusHelpSections returns grouped bindings for dashboard status bars.
func (k KeyMap) DashboardStatusHelpSections() []HelpSection {
	return []HelpSection{
		{Title: "Global", Bindings: k.globalStatusBindings()},
		{Title: "Dashboard", Bindings: dashboardStatusBindings(k)},
	}
}

// globalStatusBindings returns the global key bindings shown in the status bar,
// appending the optional export binding when it has a non-empty label.
func (k KeyMap) globalStatusBindings() []key.Binding {
	bindings := []key.Binding{
		helpTextBinding("H", "toggle help"),
		k.Tab, k.ShiftTab,
		k.One, k.Two, k.Three, k.Four, k.Five, k.Six, k.Seven,
		k.Visualize, k.Metric, k.Sort, k.ReverseSort,
		k.Filter, k.FilterUndo,
		k.SelectPID, k.SelectTID,
		k.Probes, k.Record, k.Refresh, k.AutoReset, k.Quit,
	}
	if help := k.Export.Help(); help.Key != "" || help.Desc != "" {
		bindings = append(bindings, k.Export)
	}
	return bindings
}

// dashboardStatusBindings returns the dashboard-specific bindings shown in
// the status bar (table navigation, stream controls, and export shortcuts).
func dashboardStatusBindings(k KeyMap) []key.Binding {
	return []key.Binding{
		k.DirGroup, k.Visualize, k.Metric, k.Sort, k.ReverseSort,
		helpTextBinding("space", "stream pause"),
		helpTextBinding("enter", "selected filter"),
		helpTextBinding("esc", "stream undo filter"),
		helpTextBinding("g/G", "table top/bottom"),
		helpTextBinding("left/right", "table col"),
		helpTextBinding("h/l", "table col"),
		helpTextBinding("j/k", "table row"),
		helpTextBinding("up/down", "table row"),
		helpTextBinding("pgup/down", "table page"),
		helpTextBinding("/,?", "stream regex search"),
		helpTextBinding("n/N", "stream search next/prev"),
		helpTextBinding("x", "stream export"),
		helpTextBinding("X", "stream export as"),
		helpTextBinding("E", "stream open last"),
	}
}

// DashboardFullHelp returns grouped bindings for dashboard overlays.
func (k KeyMap) DashboardFullHelp() [][]key.Binding {
	controls := []key.Binding{k.Tab, k.ShiftTab}
	if help := k.Export.Help(); help.Key != "" || help.Desc != "" {
		controls = append(controls, k.Export)
	}
	controls = append(controls, k.DirGroup, k.SelectPID, k.SelectTID, k.Probes, k.Record, k.Refresh, k.AutoReset, k.Quit)
	controls = append(controls, k.Visualize, k.Metric, k.Sort, k.ReverseSort, k.Filter, k.FilterUndo)

	return [][]key.Binding{
		{k.One, k.Two, k.Three, k.Four, k.Five, k.Six, k.Seven},
		controls,
		{
			helpTextBinding("space", "stream pause"),
			helpTextBinding("enter", "selected filter"),
			helpTextBinding("esc", "stream undo filter"),
			helpTextBinding("g/G", "table top/bottom"),
			helpTextBinding("left/right", "table col"),
			helpTextBinding("h/l", "table col"),
			helpTextBinding("j/k", "table row"),
			helpTextBinding("up/down", "table row"),
			helpTextBinding("pgup/down", "table page"),
			helpTextBinding("x", "stream export"),
			helpTextBinding("X", "stream export as"),
			helpTextBinding("E", "stream open last"),
			helpTextBinding("/,?", "stream regex search"),
			helpTextBinding("n/N", "stream search next/prev"),
		},
	}
}

// PickerShortHelp returns compact bindings for the PID picker.
func (k KeyMap) PickerShortHelp() []key.Binding {
	return []key.Binding{k.Enter, k.Refresh, k.Esc}
}

func helpTextBinding(keyText, desc string) key.Binding {
	return key.NewBinding(key.WithHelp(keyText, desc))
}