mirror of
https://github.com/charmbracelet/gum
synced 2024-06-08 16:52:17 +02:00
avoid crashes when regex doesn't compile
This commit is contained in:
parent
be3e250892
commit
562bcaa9df
|
@ -41,14 +41,22 @@ func (s *search) Execute(m *model) {
|
|||
return
|
||||
}
|
||||
|
||||
s.query = regexp.MustCompile(s.input.Value())
|
||||
var err error
|
||||
s.query, err = regexp.Compile(s.input.Value())
|
||||
if err != nil {
|
||||
s.query = nil
|
||||
return
|
||||
}
|
||||
query := regexp.MustCompile(fmt.Sprintf("(%s)", s.query.String()))
|
||||
m.content = query.ReplaceAllString(m.content, m.matchStyle.Render("$1"))
|
||||
|
||||
// Recompile the regex to match the an replace the highlights.
|
||||
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)
|
||||
s.query, err = regexp.Compile(matchingString)
|
||||
if err != nil {
|
||||
s.query = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *search) Done() {
|
||||
|
|
Loading…
Reference in a new issue