From 1d73d4c407023340cad50dc995122856bbe881b5 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 2 Mar 2021 12:48:02 -0700 Subject: [PATCH] Use tview synchronization API to prevent race conditions --- main.go | 43 +++++++++++++------------------------------ 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 8a62505..bc06909 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,6 @@ import ( "log" "os" "os/exec" - "time" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -92,7 +91,7 @@ func (d *Document) FromFile(filename string) error { return err } - d.contents += string(bytes) + d.contents = string(bytes) return nil } @@ -218,14 +217,24 @@ func createApp(doc Document, filter string) *tview.Application { outputWriter := tview.ANSIWriter(outputView) - inputChan := make(chan string) filterInput := tview.NewInputField() filterInput. SetText(filter). SetFieldBackgroundColor(tcell.ColorBlack). SetFieldTextColor(tcell.ColorSilver). SetChangedFunc(func(text string) { - inputChan <- text + go app.QueueUpdate(func() { + out, err := doc.Filter(text) + if err != nil { + filterInput.SetFieldTextColor(tcell.ColorMaroon) + return + } + + filterInput.SetFieldTextColor(tcell.ColorSilver) + outputView.Clear() + fmt.Fprint(outputWriter, out) + outputView.ScrollToBeginning() + }) }). SetDoneFunc(func(key tcell.Key) { switch key { @@ -254,32 +263,6 @@ func createApp(doc Document, filter string) *tview.Application { fmt.Fprint(outputWriter, out) }() - // Debounce filter input - go func() { - var text string - var timer *time.Timer - interval := 5 * time.Millisecond - for { - text = <-inputChan - if timer != nil { - timer.Stop() - } - - timer = time.AfterFunc(interval, func() { - out, err := doc.Filter(text) - if err != nil { - filterInput.SetFieldTextColor(tcell.ColorMaroon) - return - } - - filterInput.SetFieldTextColor(tcell.ColorSilver) - outputView.Clear() - fmt.Fprint(outputWriter, out) - outputView.ScrollToBeginning() - }) - } - }() - grid := tview.NewGrid(). SetRows(0, 3). SetColumns(0). -- 2.45.2