From 828f9f6d353b8c7b72a56794a96f6b1ed004a123 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 10 May 2023 01:36:11 +0200 Subject: [PATCH] wip: towards file name filtering --- gshmm.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/gshmm.go b/gshmm.go index 94b9c8f..cb26c4d 100644 --- a/gshmm.go +++ b/gshmm.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "strings" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" @@ -17,6 +18,10 @@ Options: -h output help ` +// minCharsUntilFilter is the minimum amount of characters that are +// required before the filtering logic commences actually filtering. +const minCharsUntilFilter = 3 + var helpFlag bool type Configuration struct { @@ -30,6 +35,7 @@ func handleCliFlags() { type model struct { filterInput textinput.Model + datasheets []string } func initialModel() model { @@ -38,16 +44,35 @@ func initialModel() model { return model{ filterInput: input, + datasheets: []string{}, } } func (m model) Init() tea.Cmd { + // TODO(d1): implement reading all PDFs in cwd + // https://stackoverflow.com/a/67214584 + // requires error reporting also return textinput.Blink } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd + if m.filterInput.Focused() { + var filtered []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) + } + } + m.datasheets = filtered + } + } + switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { @@ -62,7 +87,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m model) View() string { - return m.filterInput.View() + body := strings.Builder{} + + body.WriteString(strings.Join(m.datasheets, "\n") + "\n") + + body.WriteString(m.filterInput.View() + "\n") + + return body.String() } func main() {