From ea5b4fdefe4a16f4eecf98e6fbe59ad2446bb37a Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Sat, 6 Jul 2019 13:51:48 +0200 Subject: [PATCH] Extract search string compilation into its own testable function --- m/pager.go | 33 +++++++++++++++++++++------------ m/pager_test.go | 4 ++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/m/pager.go b/m/pager.go index 15279b6..808ecf0 100644 --- a/m/pager.go +++ b/m/pager.go @@ -260,28 +260,37 @@ func (p *_Pager) _ScrollToPreviousSearchHit() { } func (p *_Pager) _UpdateSearchPattern() { - if len(p.searchString) == 0 { - p.searchPattern = nil - return + p.searchPattern = ToPattern(p.searchString) + + p._ScrollToSearchHits() + + // FIXME: If the user is typing, indicate to user if we didn't find anything +} + +// ToPattern compiles a search string into a pattern. +// +// If the string contains only lower-case letter the pattern will be case insensitive. +// +// If the string is empty the pattern will be nil. +// +// If the string does not compile into a regexp the pattern will match the string verbatim +func ToPattern(compileMe string) *regexp.Regexp { + if len(compileMe) == 0 { + return nil } - defer p._ScrollToSearchHits() - // FIXME: Indicate to user if we didn't find anything - - pattern, err := regexp.Compile(p.searchString) + pattern, err := regexp.Compile(compileMe) if err == nil { // Search string is a regexp // FIXME: Make this case insensitive if input is all-lowercase - p.searchPattern = pattern - return + return pattern } - pattern, err = regexp.Compile(regexp.QuoteMeta(p.searchString)) + pattern, err = regexp.Compile(regexp.QuoteMeta(compileMe)) if err == nil { // Pattern matching the string exactly // FIXME: Make this case insensitive if input is all-lowercase - p.searchPattern = pattern - return + return pattern } // Unable to create a match-string-verbatim pattern diff --git a/m/pager_test.go b/m/pager_test.go index f0a8739..ea1fef5 100644 --- a/m/pager_test.go +++ b/m/pager_test.go @@ -215,3 +215,7 @@ func TestManPageFormatting(t *testing.T) { // FIXME: Test backspace between two uncombinable characters } + +func TestToPattern(t *testing.T) { + assert.Assert(t, ToPattern("") == nil) +}