|
@ -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") |
|
|
|
|
|
|
|
|