fixed bug in lipglosspadding and allowed matching 1 etc.

This commit is contained in:
Mikael Fangel 2023-04-06 19:30:35 +02:00 committed by Maas Lalani
parent 603b3a286c
commit ea772d6677
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000
2 changed files with 15 additions and 7 deletions

View file

@ -15,9 +15,9 @@ func LipglossTruncate(s string, width int) string {
}
// LipglossLengthPadding calculated calculates how much padding a string is given by a style.
func LipglossLengthPadding(s string, style lipgloss.Style) (int, int) {
render := style.Render(s)
before := strings.Index(render, s)
after := len(render) - len(s) - before
func LipglossPadding(style lipgloss.Style) (int, int) {
render := style.Render(" ")
before := strings.Index(render, " ")
after := len(render) - len(" ") - before
return before, after
}

View file

@ -45,9 +45,9 @@ func (s *search) Execute(m *model) {
m.content = query.ReplaceAllString(m.content, m.matchStyle.Render("$1"))
// Recompile the regex to match the an replace the highlights.
leftPad, rightPad := utils.LipglossLengthPadding("", m.matchStyle)
metaString := m.matchStyle.Render("")
s.query = regexp.MustCompile(regexp.QuoteMeta(metaString[:leftPad]) + s.query.String() + regexp.QuoteMeta(metaString[rightPad:]))
leftPad, _ := utils.LipglossPadding(m.matchStyle)
matchingString := regexp.QuoteMeta(m.matchStyle.Render()[:leftPad]) + s.query.String() + regexp.QuoteMeta(m.matchStyle.Render()[leftPad:])
s.query = regexp.MustCompile(matchingString)
}
func (s *search) Done() {
@ -68,6 +68,10 @@ func (s *search) NextMatch(m *model) {
// Highlight the next match.
allMatches := s.query.FindAllStringIndex(m.content, -1)
if len(allMatches) == 0 {
return
}
s.matchIndex = (s.matchIndex + 1) % len(allMatches)
match := allMatches[s.matchIndex]
lhs := m.content[:match[0]]
@ -104,6 +108,10 @@ func (s *search) PrevMatch(m *model) {
// Highlight the previous match.
allMatches := s.query.FindAllStringIndex(m.content, -1)
if len(allMatches) == 0 {
return
}
s.matchIndex = (s.matchIndex - 1) % len(allMatches)
if s.matchIndex < 0 {
s.matchIndex = 0