~yerinalexey/rex

47a94bf077d50f44f36bbb19e8104b2cee0098b7 — Alexey Yerin 5 months ago 1186af9
Debounce input by 1ms
1 files changed, 26 insertions(+), 20 deletions(-)

M main.go
M main.go => main.go +26 -20
@@ 23,6 23,7 @@ import (
	"io/ioutil"
	"os"
	"strings"
	"time"

	"regexp"



@@ 56,35 57,40 @@ func fatalf(format string, v ...interface{}) {
// regexWorker matches data from `in` to each line in `tests` and writes
// colored result to `out`
func regexWorker(in chan string, tests string, out *tview.TextView) {
	// TODO: Debounce input
	//
	// App might mess up if you type too fast
	var timer *time.Timer

	for {
		expr := <-in

		rg, err := regexp.Compile(expr)
		if err != nil {
			// TODO: Notify about the error
			continue
		if timer != nil {
			timer.Stop()
		}

		out.Clear()
		timer = time.AfterFunc(time.Millisecond, func() {
			rg, err := regexp.Compile(expr)
			if err != nil {
				// TODO: Notify about the error
				return
			}

		// TODO: crlf?
		for _, line := range strings.Split(tests, "\n") {
			matches := rg.MatchString(line)
			out.Clear()

			color := "red"
			if matches {
				color = "green"
			}
			// TODO: crlf?
			for _, line := range strings.Split(tests, "\n") {
				matches := rg.MatchString(line)

			// tview.Escape prevents messing up text if line includes
			// [squarebrackets]
			fmt.Fprintf(out, "[%s]%s[white]\n", color, tview.Escape(line))
		}
				color := "red"
				if matches {
					color = "green"
				}

				// tview.Escape prevents messing up text if line includes
				// [squarebrackets]
				fmt.Fprintf(out, "[%s]%s[white]\n", color, tview.Escape(line))
			}

		out.ScrollToBeginning()
			out.ScrollToBeginning()
		})
	}
}