Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Search race condition #176

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions example/search/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"log"
"sync"
"time"

"github.com/noborus/ov/oviewer"
)

func main() {
ov, err := oviewer.Open("main.go")
if err != nil {
log.Fatal(err)
}

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
if err := ov.Run(); err != nil {
log.Fatal(err)
}
}()
ov.StyleSearchHighlight = oviewer.OVStyle{
Foreground: "gold",
Reverse: true,
Blink: true,
}
time.Sleep(time.Second * 1)
ov.MoveBottom()
ov.BackSearch("main")

time.Sleep(time.Second * 1)
ov.MoveTop()
ov.Search("import")

time.Sleep(time.Second * 10)
ov.Quit()
wg.Wait()
}
File renamed without changes.
File renamed without changes.
12 changes: 8 additions & 4 deletions oviewer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (root *Root) main(ctx context.Context, quitChan chan<- struct{}) {
case *eventPaste:
root.getClipboard(ctx)
case *eventSearch:
searcher := root.setSearcher(root.input.value, root.CaseSensitive)
searcher := root.setSearcher(ev.str, root.CaseSensitive)
root.searchMove(ctx, true, root.Doc.topLN+root.Doc.firstLine()+1, searcher)
case *eventBackSearch:
searcher := root.setSearcher(root.input.value, root.CaseSensitive)
searcher := root.setSearcher(ev.str, root.CaseSensitive)
root.searchMove(ctx, false, root.Doc.topLN+root.Doc.firstLine()-1, searcher)
case *viewModeInput:
root.setViewMode(ev.value)
Expand Down Expand Up @@ -265,11 +265,13 @@ func (root *Root) MoveBottom() {

// eventSearch represents search event.
type eventSearch struct {
str string
tcell.EventTime
}

func (root *Root) eventNextSearch() {
ev := &eventSearch{}
ev.str = root.input.value
ev.SetEventNow()
err := root.Screen.PostEvent(ev)
if err != nil {
Expand All @@ -279,11 +281,13 @@ func (root *Root) eventNextSearch() {

// eventBackSearch represents backward search event.
type eventBackSearch struct {
str string
tcell.EventTime
}

func (root *Root) eventNextBackSearch() {
ev := &eventBackSearch{}
ev.str = root.input.value
ev.SetEventNow()
err := root.Screen.PostEvent(ev)
if err != nil {
Expand All @@ -298,8 +302,8 @@ func (root *Root) Search(str string) {
if !root.checkScreen() {
return
}
root.input.value = str
ev := &eventSearch{}
ev.str = str
ev.SetEventNow()
err := root.Screen.PostEvent(ev)
if err != nil {
Expand All @@ -314,8 +318,8 @@ func (root *Root) BackSearch(str string) {
if !root.checkScreen() {
return
}
root.input.value = str
ev := &eventBackSearch{}
ev.str = str
ev.SetEventNow()
err := root.Screen.PostEvent(ev)
if err != nil {
Expand Down