mirror of
https://github.com/walles/moar.git
synced 2024-11-12 17:22:53 +03:00
Have the tokenizer provide an unformatted string as well
This commit is contained in:
parent
4541f7fb08
commit
f16d818715
@ -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 {
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
24
m/pager.go
24
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user