wip: first fuzzy filter implementation

This commit is contained in:
decentral1se 2023-05-10 09:53:08 +02:00
parent 828f9f6d35
commit 34424bd317
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410
3 changed files with 28 additions and 13 deletions

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.18
require ( require (
github.com/charmbracelet/bubbles v0.15.0 github.com/charmbracelet/bubbles v0.15.0
github.com/charmbracelet/bubbletea v0.23.1 github.com/charmbracelet/bubbletea v0.23.1
github.com/sahilm/fuzzy v0.1.0
) )
require ( require (

2
go.sum
View File

@ -11,6 +11,7 @@ github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87ini
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk= github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw= github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
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/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
@ -37,6 +38,7 @@ github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4Y
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -8,6 +8,7 @@ import (
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/sahilm/fuzzy"
) )
const help = `go-sh-manymanuals: TODO const help = `go-sh-manymanuals: TODO
@ -20,7 +21,7 @@ Options:
// minCharsUntilFilter is the minimum amount of characters that are // minCharsUntilFilter is the minimum amount of characters that are
// required before the filtering logic commences actually filtering. // required before the filtering logic commences actually filtering.
const minCharsUntilFilter = 3 const minCharsUntilFilter = 2
var helpFlag bool var helpFlag bool
@ -34,17 +35,22 @@ func handleCliFlags() {
} }
type model struct { type model struct {
filterInput textinput.Model filterInput textinput.Model // fuzzy search interface
datasheets []string datasheets []string // all datasheets under cwd
dataSheetsView []string // filtered view on all datasheets
} }
func initialModel() model { func initialModel() model {
input := textinput.New() input := textinput.New()
input.Focus() input.Focus()
// TODO(d1): gather actual datasheets
ds := []string{"foo", "bar", "baz", "bing"}
return model{ return model{
filterInput: input, filterInput: input,
datasheets: []string{}, datasheets: ds,
dataSheetsView: ds,
} }
} }
@ -59,18 +65,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
if m.filterInput.Focused() { if m.filterInput.Focused() {
var filtered []string var matched []string
search := m.filterInput.Value() search := m.filterInput.Value()
if len(search) >= minCharsUntilFilter { if len(search) >= minCharsUntilFilter {
// NOTE(d1): naive prototype implementation matches := fuzzy.Find(search, m.datasheets)
for _, ds := range m.datasheets { for _, match := range matches {
if strings.HasPrefix(ds, search) { matched = append(matched, match.Str)
filtered = append(filtered, ds)
} }
if len(matches) > 0 {
m.dataSheetsView = matched
} else {
m.dataSheetsView = m.datasheets
} }
m.datasheets = filtered } else {
m.dataSheetsView = m.datasheets
} }
} }
switch msg := msg.(type) { switch msg := msg.(type) {
@ -89,7 +101,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string { func (m model) View() string {
body := strings.Builder{} body := strings.Builder{}
body.WriteString(strings.Join(m.datasheets, "\n") + "\n") body.WriteString(strings.Join(m.dataSheetsView, "\n") + "\n")
body.WriteString(m.filterInput.View() + "\n") body.WriteString(m.filterInput.View() + "\n")