|
|
@ -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) |
|
|
|
} |
|
|
|
|
|
|
|
if len(matches) > 0 { |
|
|
|
m.dataSheetsView = matched |
|
|
|
} else { |
|
|
|
m.dataSheetsView = m.datasheets |
|
|
|
} |
|
|
|
m.datasheets = filtered |
|
|
|
} 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") |
|
|
|
|
|
|
|