forked from varia/go-sh-manymanuals
wip: first fuzzy filter implementation
This commit is contained in:
parent
828f9f6d35
commit
34424bd317
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.18
|
||||
require (
|
||||
github.com/charmbracelet/bubbles v0.15.0
|
||||
github.com/charmbracelet/bubbletea v0.23.1
|
||||
github.com/sahilm/fuzzy v0.1.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
2
go.sum
2
go.sum
@ -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/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
|
||||
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/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
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.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
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=
|
||||
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=
|
||||
|
38
gshmm.go
38
gshmm.go
@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/sahilm/fuzzy"
|
||||
)
|
||||
|
||||
const help = `go-sh-manymanuals: TODO
|
||||
@ -20,7 +21,7 @@ Options:
|
||||
|
||||
// minCharsUntilFilter is the minimum amount of characters that are
|
||||
// required before the filtering logic commences actually filtering.
|
||||
const minCharsUntilFilter = 3
|
||||
const minCharsUntilFilter = 2
|
||||
|
||||
var helpFlag bool
|
||||
|
||||
@ -34,17 +35,22 @@ func handleCliFlags() {
|
||||
}
|
||||
|
||||
type model struct {
|
||||
filterInput textinput.Model
|
||||
datasheets []string
|
||||
filterInput textinput.Model // fuzzy search interface
|
||||
datasheets []string // all datasheets under cwd
|
||||
dataSheetsView []string // filtered view on all datasheets
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
input := textinput.New()
|
||||
input.Focus()
|
||||
|
||||
// TODO(d1): gather actual datasheets
|
||||
ds := []string{"foo", "bar", "baz", "bing"}
|
||||
|
||||
return model{
|
||||
filterInput: input,
|
||||
datasheets: []string{},
|
||||
filterInput: input,
|
||||
datasheets: ds,
|
||||
dataSheetsView: ds,
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,18 +65,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmd tea.Cmd
|
||||
|
||||
if m.filterInput.Focused() {
|
||||
var filtered []string
|
||||
var matched []string
|
||||
|
||||
search := m.filterInput.Value()
|
||||
if len(search) >= minCharsUntilFilter {
|
||||
// NOTE(d1): naive prototype implementation
|
||||
for _, ds := range m.datasheets {
|
||||
if strings.HasPrefix(ds, search) {
|
||||
filtered = append(filtered, ds)
|
||||
}
|
||||
matches := fuzzy.Find(search, m.datasheets)
|
||||
for _, match := range matches {
|
||||
matched = append(matched, match.Str)
|
||||
}
|
||||
m.datasheets = filtered
|
||||
|
||||
if len(matches) > 0 {
|
||||
m.dataSheetsView = matched
|
||||
} else {
|
||||
m.dataSheetsView = m.datasheets
|
||||
}
|
||||
} else {
|
||||
m.dataSheetsView = m.datasheets
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
switch msg := msg.(type) {
|
||||
@ -89,7 +101,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
func (m model) View() string {
|
||||
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")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user