wip: towards file name filtering

This commit is contained in:
decentral1se 2023-05-10 01:36:11 +02:00
parent 6de545a3d3
commit 828f9f6d35
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410

View File

@ -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() {