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

import (
	"sort"
	"strings"
)

// orderedMatchIndices returns the keys of matchSet as a sorted int slice.
// Used by search navigation and the search footer to determine match position.
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
}

// openSearch activates search mode on the Model by delegating to SearchController.
func (m *Model) openSearch() {
	m.SearchController.open()
}

// clearSearch deactivates search mode and clears all search state.
// Delegates to SearchController and updates the Model status message.
func (m *Model) clearSearch() {
	m.statusMessage = m.SearchController.clear()
}

// applySearchQuery applies a new search query, rebuilds filter state, and jumps
// to the first match. Delegates to SearchController.applyQuery, then applies
// any resulting selection change via the shared jumpMatch helper.
func (m *Model) applySearchQuery(raw string) {
	statusMsg, jumpDir := m.SearchController.applyQuery(raw, m.frames, m.ancestry)
	m.statusMessage = statusMsg
	if jumpDir != 0 {
		m.selectedIdx, m.subtreeSet = jumpMatch(
			m.frames, m.matchIndices, m.ancestry, m.selectedIdx, jumpDir,
		)
	} else {
		m.SelectionManager.ensureNavigable(m.frames, m.matchIndices, m.searchQuery, m.filterVisible)
	}
}

// recomputeFilterState rebuilds the match and filter-visible sets after a
// frame layout change. Delegates to SearchController.recomputeFilterState.
func (m *Model) recomputeFilterState() {
	m.SearchController.recomputeFilterState(m.frames, m.ancestry)
}

// searchFooter renders the search bar with match position info. Delegates to
// SearchController.footerLine.
func (m Model) searchFooter() string {
	return m.SearchController.footerLine(m.frames, m.selectedIdx)
}

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)
	}
}