Chore: simplify search
This commit is contained in:
70
tui.go
70
tui.go
@@ -389,50 +389,23 @@ func performSearch(term string) {
|
|||||||
colorText()
|
colorText()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Get formatted text and search directly in it to avoid mapping issues
|
||||||
// Get formatted text
|
|
||||||
formattedText := textView.GetText(true)
|
formattedText := textView.GetText(true)
|
||||||
plainText, mapping := stripTags(formattedText)
|
searchTermLower := strings.ToLower(searchText)
|
||||||
|
formattedTextLower := strings.ToLower(formattedText)
|
||||||
// Find all occurrences of the search term in plain text
|
// Find all occurrences of the search term in the formatted text directly
|
||||||
plainSearchResults := []int{}
|
formattedSearchResults := []int{}
|
||||||
|
searchStart := 0
|
||||||
start := 0
|
|
||||||
for {
|
for {
|
||||||
// Use case-insensitive search
|
pos := strings.Index(formattedTextLower[searchStart:], searchTermLower)
|
||||||
index := strings.Index(strings.ToLower(plainText[start:]), strings.ToLower(searchText))
|
if pos == -1 {
|
||||||
if index == -1 {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
absolutePos := searchStart + pos
|
||||||
absoluteIndex := start + index
|
formattedSearchResults = append(formattedSearchResults, absolutePos)
|
||||||
plainSearchResults = append(plainSearchResults, absoluteIndex)
|
searchStart = absolutePos + len(searchText)
|
||||||
start = absoluteIndex + len(searchText) // Advance past the last match
|
|
||||||
}
|
}
|
||||||
|
if len(formattedSearchResults) == 0 {
|
||||||
if len(plainSearchResults) > 0 {
|
|
||||||
searchResults = make([]int, len(plainSearchResults))
|
|
||||||
searchResultLengths = make([]int, len(plainSearchResults))
|
|
||||||
|
|
||||||
for i, p_start := range plainSearchResults {
|
|
||||||
p_end_exclusive := p_start + len(searchText)
|
|
||||||
|
|
||||||
f_start := mapping[p_start]
|
|
||||||
var f_end_exclusive int
|
|
||||||
if p_end_exclusive < len(mapping) {
|
|
||||||
f_end_exclusive = mapping[p_end_exclusive]
|
|
||||||
} else {
|
|
||||||
// Reached the end of the text
|
|
||||||
f_end_exclusive = len(formattedText)
|
|
||||||
}
|
|
||||||
|
|
||||||
searchResults[i] = f_start
|
|
||||||
searchResultLengths[i] = f_end_exclusive - f_start
|
|
||||||
}
|
|
||||||
|
|
||||||
searchIndex = 0
|
|
||||||
highlightCurrentMatch()
|
|
||||||
} else {
|
|
||||||
// No matches found
|
// No matches found
|
||||||
searchResults = nil
|
searchResults = nil
|
||||||
searchResultLengths = nil
|
searchResultLengths = nil
|
||||||
@@ -440,7 +413,17 @@ func performSearch(term string) {
|
|||||||
if err := notifyUser("search", notification); err != nil {
|
if err := notifyUser("search", notification); err != nil {
|
||||||
logger.Error("failed to send notification", "error", err)
|
logger.Error("failed to send notification", "error", err)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
// Store the formatted text positions and lengths for accurate highlighting
|
||||||
|
searchResults = formattedSearchResults
|
||||||
|
// Create lengths array - all matches have the same length as the search term
|
||||||
|
searchResultLengths = make([]int, len(formattedSearchResults))
|
||||||
|
for i := range searchResultLengths {
|
||||||
|
searchResultLengths[i] = len(searchText)
|
||||||
|
}
|
||||||
|
searchIndex = 0
|
||||||
|
highlightCurrentMatch()
|
||||||
}
|
}
|
||||||
|
|
||||||
// highlightCurrentMatch highlights the current search match and scrolls to it
|
// highlightCurrentMatch highlights the current search match and scrolls to it
|
||||||
@@ -665,10 +648,7 @@ func init() {
|
|||||||
textArea.SetBorder(true).SetTitle("input")
|
textArea.SetBorder(true).SetTitle("input")
|
||||||
textView = tview.NewTextView().
|
textView = tview.NewTextView().
|
||||||
SetDynamicColors(true).
|
SetDynamicColors(true).
|
||||||
SetRegions(true).
|
SetRegions(true)
|
||||||
SetChangedFunc(func() {
|
|
||||||
app.Draw()
|
|
||||||
})
|
|
||||||
|
|
||||||
flex = tview.NewFlex().SetDirection(tview.FlexRow).
|
flex = tview.NewFlex().SetDirection(tview.FlexRow).
|
||||||
AddItem(textView, 0, 40, false).
|
AddItem(textView, 0, 40, false).
|
||||||
@@ -734,9 +714,6 @@ func init() {
|
|||||||
position = tview.NewTextView().
|
position = tview.NewTextView().
|
||||||
SetDynamicColors(true).
|
SetDynamicColors(true).
|
||||||
SetTextAlign(tview.AlignCenter)
|
SetTextAlign(tview.AlignCenter)
|
||||||
position.SetChangedFunc(func() {
|
|
||||||
app.Draw()
|
|
||||||
})
|
|
||||||
// Initially set up flex without search bar
|
// Initially set up flex without search bar
|
||||||
flex = tview.NewFlex().SetDirection(tview.FlexRow).
|
flex = tview.NewFlex().SetDirection(tview.FlexRow).
|
||||||
AddItem(textView, 0, 40, false).
|
AddItem(textView, 0, 40, false).
|
||||||
@@ -1410,7 +1387,6 @@ func init() {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if isASCII(string(event.Rune())) && !botRespMode {
|
if isASCII(string(event.Rune())) && !botRespMode {
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user