From 235e21e52a5f83b28e692432ee4fb0bf21ff8772 Mon Sep 17 00:00:00 2001 From: Benjamin Nguyen Date: Mon, 30 Nov 2020 09:55:36 -0800 Subject: [PATCH] WIP - theme searchbar --- go.mod | 2 ++ go.sum | 4 ++++ internal/themes/colors.go | 5 ++++ internal/ui/widgets.go | 38 +++++++++++++++++++++++-------- internal/ui/widgets_controller.go | 2 +- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 11b8789..7c140aa 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,7 @@ go 1.13 require ( github.com/flopp/go-findfont v0.0.0-20200805110358-089b91d05de8 github.com/gizak/termui/v3 v3.1.0 + github.com/kylelemons/godebug v1.1.0 // indirect + github.com/sahilm/fuzzy v0.1.0 golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 ) diff --git a/go.sum b/go.sum index 82f87d5..e823493 100644 --- a/go.sum +++ b/go.sum @@ -3,11 +3,15 @@ github.com/flopp/go-findfont v0.0.0-20200805110358-089b91d05de8/go.mod h1:wKKxRD github.com/gizak/termui v3.1.0+incompatible h1:N3CFm+j087lanTxPpHOmQs0uS3s5I9TxoAFy6DqPqv8= github.com/gizak/termui/v3 v3.1.0 h1:ZZmVDgwHl7gR7elfKf1xc4IudXZ5qqfDh4wExk4Iajc= github.com/gizak/termui/v3 v3.1.0/go.mod h1:bXQEBkJpzxUAKf0+xq9MSWAvWZlE7c+aidmyFlkYTrY= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840= github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= +github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= +github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= golang.org/x/sys v0.0.0-20201018121011-98379d014ca7 h1:CNOpL+H7PSxBI7dF/EIUsfOguRSzWp6CQ91yxZE6PG4= golang.org/x/sys v0.0.0-20201018121011-98379d014ca7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/themes/colors.go b/internal/themes/colors.go index 7dad6c5..9fae4ce 100644 --- a/internal/themes/colors.go +++ b/internal/themes/colors.go @@ -1,6 +1,7 @@ package themes var AllThemes = make(map[string]string) +var ThemesList []string func init() { AllThemes = map[string]string{ @@ -24,6 +25,10 @@ func init() { "Gotham": Gotham, "Solarized Light": SolarizedLight, } + + for theme := range AllThemes { + ThemesList = append(ThemesList, theme) + } } const AfterGlow string = ` diff --git a/internal/ui/widgets.go b/internal/ui/widgets.go index 9bf1ee8..e0aab95 100644 --- a/internal/ui/widgets.go +++ b/internal/ui/widgets.go @@ -3,6 +3,7 @@ package ui import ( "fmt" "path/filepath" + "regexp" "sort" "strconv" "strings" @@ -15,6 +16,7 @@ import ( "github.com/flopp/go-findfont" ui "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets" + "github.com/sahilm/fuzzy" "golang.org/x/sys/unix" ) @@ -37,7 +39,7 @@ func init() { termHeight = int(ws.Row) } -func themeSearchbox() (*widgets.Paragraph, func() string) { +func themeSearchbox(themeShufflerWidget *widgets.List) (*widgets.Paragraph, func() string) { var x1, x2, y1, y2 int = columnOneStart, columnOneEnd, 0, 3 searchbox := widgets.NewParagraph() @@ -55,23 +57,41 @@ func themeSearchbox() (*widgets.Paragraph, func() string) { } } + alphanumericRe := regexp.Compile("^[a-zA-Z0-9]*$") + setState := func() string { uiEvents := ui.PollEvents() for { e := <-uiEvents - switch e.ID { - case "", "q", "H", "J", "K", "L": - return e.ID - case "": + + // Allow only alphanumeric characters + if alphanumericRe.Match([]byte(e.ID)) { + switch e.ID { + case "", "q", "H", "J", "K", "L": + return e.ID + case "": + searchbox.Text = string([]byte(searchbox.Text)[:len(searchbox.Text)-1]) + default: + searchbox.Text = searchbox.Text + processKey(e.ID) + } + if len(searchbox.Text) == 0 { + themeShufflerWidget.Rows = themes.ThemesList + ui.Render(searchbox, themeShufflerWidget) continue } - searchbox.Text = string([]byte(searchbox.Text)[:len(searchbox.Text)-1]) - default: - searchbox.Text = searchbox.Text + processKey(e.ID) + + matches := fuzzy.Find(searchbox.Text, themes.ThemesList) + themes := make([]string, len(matches)) + for i := 0; i < len(themes); i++ { + themes[i] = matches[i].Str + } + + themeShufflerWidget.Rows = themes + ui.Render(searchbox, themeShufflerWidget) } - ui.Render(searchbox) + } } return searchbox, setState diff --git a/internal/ui/widgets_controller.go b/internal/ui/widgets_controller.go index 5a5e069..60760cd 100644 --- a/internal/ui/widgets_controller.go +++ b/internal/ui/widgets_controller.go @@ -21,7 +21,7 @@ func WidgetsController(fileContent *string) { opacityGauge, setOpacityState := opacityAdjuster(fileContent) fontSizeAdjuster, setFontSizeState := fontSizeAdjuster(fileContent) fontShuffler, setFontState := fontShuffler(fileContent) - themeSearch, setThemeSearchState := themeSearchbox() + themeSearch, setThemeSearchState := themeSearchbox(themesList) fontSearch, setFontSearchState := fontSearchbox() help := helpBox()