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

WIP - Search-bar implementation for themes. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
5 changes: 5 additions & 0 deletions internal/themes/colors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package themes

var AllThemes = make(map[string]string)
var ThemesList []string

func init() {
AllThemes = map[string]string{
Expand All @@ -24,6 +25,10 @@ func init() {
"Gotham": Gotham,
"Solarized Light": SolarizedLight,
}

for theme := range AllThemes {
ThemesList = append(ThemesList, theme)
}
}

const AfterGlow string = `
Expand Down
38 changes: 29 additions & 9 deletions internal/ui/widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -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"
)
Expand All @@ -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()
Expand All @@ -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 "<C-c>", "q", "H", "J", "K", "L":
return e.ID
case "<Backspace>":

// Allow only alphanumeric characters
if alphanumericRe.Match([]byte(e.ID)) {
switch e.ID {
case "<C-c>", "q", "H", "J", "K", "L":
return e.ID
case "<Backspace>":
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
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/widgets_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down