From c9e585c37c8e3a1bed947baa1f599c26c83f0228 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Thu, 14 Sep 2023 21:45:51 +0200 Subject: [PATCH] wip --- main.go | 24 ++++++++++++++++-------- search.go | 30 +++++++++++++++++++----------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index ce6744b..96c76c7 100644 --- a/main.go +++ b/main.go @@ -128,7 +128,7 @@ func main() { fileName: fileName, digInput: digInput, searchInput: searchInput, - searchResultsIndexes: make(map[*node][][]int), + searchResultsIndexes: make(map[*node][]match), } p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion()) @@ -150,7 +150,7 @@ type model struct { searchError error searchResults []*node searchResultsCursor int - searchResultsIndexes map[*node][][]int + searchResultsIndexes map[*node][]match } func (m *model) Init() tea.Cmd { @@ -557,13 +557,17 @@ func (m *model) prettyPrint(node *node, selected bool) []byte { } if indexes, ok := m.searchResultsIndexes[node]; ok { - bb := splitBytesByIndexes(b, indexes) + куски := splitBytesByIndexes(b, indexes) var out []byte - for i, b := range bb { + for i, кусочек := range куски { if i%2 == 0 { - out = append(out, style(b)...) + out = append(out, style(кусочек.b)...) } else { - out = append(out, currentTheme.Search(b)...) + if кусочек.index == m.searchResultsCursor { + out = append(out, currentTheme.Cursor(кусочек.b)...) + } else { + out = append(out, currentTheme.Search(кусочек.b)...) + } } } return out @@ -718,6 +722,7 @@ func (m *model) search(s string) { } n := m.top + searchIndex := 0 for n != nil { indexes := re.FindAllIndex(n.value, -1) if len(indexes) > 0 { @@ -745,13 +750,16 @@ func (m *model) search(s string) { it = it.next } - chunkMatches := splitIndexesToChunks(chunks, indexes) + chunkMatches := splitIndexesToChunks(chunks, indexes, searchIndex) for i, matches := range chunkMatches { m.searchResultsIndexes[chunkNodes[i]] = matches } } else { - m.searchResultsIndexes[n] = indexes + for i, pair := range indexes { + m.searchResultsIndexes[n] = append(m.searchResultsIndexes[n], match{start: pair[0], end: pair[1], index: searchIndex + i}) + } } + searchIndex += len(indexes) } if n.isCollapsed() { diff --git a/search.go b/search.go index a90e5a4..c195a4d 100644 --- a/search.go +++ b/search.go @@ -5,15 +5,20 @@ type match struct { index int } -func splitBytesByIndexes(b []byte, indexes []match) [][]byte { - out := make([][]byte, 0, 1) +type кусочек struct { + b []byte + index int +} + +func splitBytesByIndexes(b []byte, indexes []match) []кусочек { + out := make([]кусочек, 0, 1) pos := 0 for _, pair := range indexes { - out = append(out, safeSlice(b, pos, pair[0])) - out = append(out, safeSlice(b, pair[0], pair[1])) - pos = pair[1] + out = append(out, кусочек{safeSlice(b, pos, pair.start), -1}) + out = append(out, кусочек{safeSlice(b, pair.start, pair.end), pair.index}) + pos = pair.end } - out = append(out, safeSlice(b, pos, len(b))) + out = append(out, кусочек{safeSlice(b, pos, len(b)), -1}) return out } @@ -32,15 +37,18 @@ func safeSlice(b []byte, start, end int) []byte { if end < 0 { end = 0 } + if start > end { + start = end + } return b[start:end] } -func splitIndexesToChunks(chunks [][]byte, indexes [][]int) (chunkIndexes [][][]int) { +func splitIndexesToChunks(chunks [][]byte, indexes [][]int, searchIndex int) (chunkIndexes [][]match) { // Initialize a slice for indexes of each chunk - chunkIndexes = make([][][]int, len(chunks)) + chunkIndexes = make([][]match, len(chunks)) // Iterate over each index pair from regex results - for _, idx := range indexes { + for index, idx := range indexes { // Calculate the current position in the whole byte slice position := 0 for i, chunk := range chunks { @@ -52,11 +60,11 @@ func splitIndexesToChunks(chunks [][]byte, indexes [][]int) (chunkIndexes [][][] // If the end index also lies in this chunk if idx[1] <= position+len(chunk) { - chunkIndexes[i] = append(chunkIndexes[i], []int{localStart, localEnd}) + chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: localEnd, index: searchIndex + index}) break } else { // If the end index is outside this chunk, split the index - chunkIndexes[i] = append(chunkIndexes[i], []int{localStart, len(chunk)}) + chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: len(chunk), index: searchIndex + index}) // Adjust the starting index for the next chunk idx[0] = position + len(chunk)