diff --git a/m/ansiTokenizer.go b/m/ansiTokenizer.go index 9af3717..83f4a16 100644 --- a/m/ansiTokenizer.go +++ b/m/ansiTokenizer.go @@ -16,8 +16,9 @@ type Token struct { Style tcell.Style } -// TokensFromString turns a string into a series of tokens -func TokensFromString(logger *log.Logger, s string) []Token { +// TokensFromString turns a (formatted) string into a series of tokens, +// and an unformatted string +func TokensFromString(logger *log.Logger, s string) ([]Token, *string) { var tokens []Token styleBrokenUtf8 := tcell.StyleDefault.Background(7).Foreground(1) @@ -57,7 +58,11 @@ func TokensFromString(logger *log.Logger, s string) []Token { } } - return tokens + plainString := "" + for _, token := range tokens { + plainString += string(token.Rune) + } + return tokens, &plainString } func _TokensFromStyledString(styledString _StyledString) []Token { diff --git a/m/matchRanges.go b/m/matchRanges.go index 89d7187..46da9d2 100644 --- a/m/matchRanges.go +++ b/m/matchRanges.go @@ -8,13 +8,13 @@ type MatchRanges struct { } // GetMatchRanges locates a regexp in a string -func GetMatchRanges(String string, Pattern *regexp.Regexp) *MatchRanges { +func GetMatchRanges(String *string, Pattern *regexp.Regexp) *MatchRanges { if Pattern == nil { return nil } return &MatchRanges{ - Matches: Pattern.FindAllStringIndex(String, -1), + Matches: Pattern.FindAllStringIndex(*String, -1), } } diff --git a/m/matchRanges_test.go b/m/matchRanges_test.go index 2dc9954..3d99e4f 100644 --- a/m/matchRanges_test.go +++ b/m/matchRanges_test.go @@ -7,9 +7,11 @@ import ( "gotest.tools/assert" ) +// This is really a constant, don't change it! +var _TestString = "mamma" + func TestGetMatchRanges(t *testing.T) { - // Should match the one in TestInRange() - matchRanges := GetMatchRanges("mamma", regexp.MustCompile("m+")) + matchRanges := GetMatchRanges(&_TestString, regexp.MustCompile("m+")) assert.Equal(t, len(matchRanges.Matches), 2) // Two matches assert.DeepEqual(t, matchRanges.Matches[0][0], 0) // First match starts at 0 @@ -20,14 +22,14 @@ func TestGetMatchRanges(t *testing.T) { } func TestGetMatchRangesNilPattern(t *testing.T) { - matchRanges := GetMatchRanges("mamma", nil) + matchRanges := GetMatchRanges(&_TestString, nil) assert.Assert(t, matchRanges == nil) assert.Assert(t, !matchRanges.InRange(0)) } func TestInRange(t *testing.T) { // Should match the one in TestGetMatchRanges() - matchRanges := GetMatchRanges("mamma", regexp.MustCompile("m+")) + matchRanges := GetMatchRanges(&_TestString, regexp.MustCompile("m+")) assert.Assert(t, !matchRanges.InRange(-1)) // Before start assert.Assert(t, matchRanges.InRange(0)) // m diff --git a/m/pager.go b/m/pager.go index 71dd0dc..ed3d190 100644 --- a/m/pager.go +++ b/m/pager.go @@ -32,12 +32,7 @@ func NewPager(r Reader) *_Pager { } func (p *_Pager) _AddLine(logger *log.Logger, lineNumber int, line string) { - tokens := TokensFromString(logger, line) - - plainString := "" - for _, token := range tokens { - plainString += string(token.Rune) - } + tokens, plainString := TokensFromString(logger, line) matchRanges := GetMatchRanges(plainString, p.searchPattern) for pos, token := range tokens { @@ -109,12 +104,23 @@ func (p *_Pager) Quit() { p.quit = true } -func (p *_Pager) UpdateSearchPattern() { +func (p *_Pager) _ScrollToSearchHits() { + // FIXME: If there are hits on the current page, do nothing + + // FIXME: Scan from here to the last line and scroll to the first hit found + + // FIXME: On no hits, somehow inform the user + +} + +func (p *_Pager) _UpdateSearchPattern() { if len(p.searchString) == 0 { p.searchPattern = nil return } + defer p._ScrollToSearchHits() + pattern, err := regexp.Compile(p.searchString) if err == nil { // Search string is a regexp @@ -146,7 +152,7 @@ func (p *_Pager) _OnSearchKey(logger *log.Logger, key tcell.Key) { } p.searchString = p.searchString[:len(p.searchString)-1] - p.UpdateSearchPattern() + p._UpdateSearchPattern() default: logger.Printf("Unhandled search key event %v", key) @@ -193,7 +199,7 @@ func (p *_Pager) _OnKey(logger *log.Logger, key tcell.Key) { func (p *_Pager) _OnSearchRune(logger *log.Logger, char rune) { p.searchString = p.searchString + string(char) - p.UpdateSearchPattern() + p._UpdateSearchPattern() } func (p *_Pager) _OnRune(logger *log.Logger, char rune) {