summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/search.go
blob: 6bedc3e90d6f12499f9fd743436da0408d5ecd0d (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
package flamegraph

import (
	"fmt"
	"sort"
	"strings"
)

func (m *Model) openSearch() {
	m.searchActive = true
	m.searchInput.SetValue(m.searchQuery)
	m.searchInput.CursorEnd()
	m.searchInput.Focus()
}

func (m *Model) clearSearch() {
	m.searchActive = false
	m.searchQuery = ""
	clearBoolMap(m.matchIndices)
	clearBoolMap(m.filterVisible)
	m.searchInput.SetValue("")
	m.searchInput.Blur()
	m.statusMessage = "Filter cleared"
}

func (m *Model) applySearchQuery(raw string) {
	m.searchQuery = strings.ToLower(strings.TrimSpace(raw))
	m.recomputeFilterState()
	query := m.searchQuery
	if query == "" {
		m.ensureSelectionNavigable()
		m.statusMessage = "Filter cleared"
		return
	}

	if len(m.matchIndices) > 0 {
		m.jumpMatch(1)
		m.statusMessage = fmt.Sprintf("Filter %q: %d matches", query, len(m.matchIndices))
		return
	}
	m.statusMessage = fmt.Sprintf("Filter %q: no matches", query)
}

func (m *Model) jumpMatch(direction int) {
	matches := orderedMatchIndices(m.matchIndices)
	if len(matches) == 0 {
		return
	}
	currentPos := indexOf(matches, m.selectedIdx)
	if currentPos == -1 {
		if direction < 0 {
			m.selectedIdx = matches[len(matches)-1]
		} else {
			m.selectedIdx = matches[0]
		}
		m.subtreeSet = computeSubtreeSetInto(m.frames, m.selectedIdx, m.subtreeSet)
		return
	}

	next := currentPos + direction
	if next < 0 {
		next = len(matches) - 1
	}
	if next >= len(matches) {
		next = 0
	}
	m.selectedIdx = matches[next]
	m.subtreeSet = computeSubtreeSetInto(m.frames, m.selectedIdx, m.subtreeSet)
}

func (m *Model) recomputeFilterState() {
	if m.matchIndices == nil {
		m.matchIndices = make(map[int]bool)
	} else {
		clearBoolMap(m.matchIndices)
	}
	if m.filterVisible == nil {
		m.filterVisible = make(map[int]bool)
	} else {
		clearBoolMap(m.filterVisible)
	}
	if m.searchQuery == "" {
		return
	}

	for idx, frame := range m.frames {
		if strings.Contains(strings.ToLower(frame.Name), m.searchQuery) {
			m.matchIndices[idx] = true
		}
	}
	m.filterVisible = computeFilterVisibleSetInto(m.frames, m.matchIndices, m.filterVisible)
}

func orderedMatchIndices(matchSet map[int]bool) []int {
	matches := make([]int, 0, len(matchSet))
	for idx := range matchSet {
		matches = append(matches, idx)
	}
	sort.Ints(matches)
	return matches
}

func (m Model) searchFooter() string {
	matches := orderedMatchIndices(m.matchIndices)
	pos := 0
	if len(matches) > 0 {
		idx := indexOf(matches, m.selectedIdx)
		if idx >= 0 {
			pos = idx + 1
		}
	}
	return fmt.Sprintf("%s  %d/%d matches", m.searchInput.View(), pos, len(matches))
}

func replaceFooterLine(content, footer string) string {
	if content == "" {
		return footer
	}
	lastNewline := strings.LastIndexByte(content, '\n')
	if lastNewline == -1 {
		return footer
	}
	return content[:lastNewline+1] + footer
}

func replaceHeaderLine(content, header string) string {
	if content == "" {
		return header
	}
	firstNewline := strings.IndexByte(content, '\n')
	if firstNewline == -1 {
		return header
	}
	return header + content[firstNewline:]
}

func clearBoolMap[K comparable](values map[K]bool) {
	for key := range values {
		delete(values, key)
	}
}