From 8e257639e0047866a80a31f0dace2d556086296f Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 12 Feb 2025 18:16:57 +0100 Subject: [PATCH] fixed list items --- cmd/root.go | 17 ++++++++++++-- go.mod | 2 ++ go.sum | 4 ++++ ui/delegate.go | 5 ++++- ui/root.go | 61 ++++++++++++++++++++++++++++++++++++++------------ 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 16f05a4..50dcd78 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,6 +24,7 @@ import ( "path/filepath" "runtime/debug" + slogmulti "github.com/samber/slog-multi" "github.com/spf13/cobra" "github.com/tlinden/anydb/app" "github.com/tlinden/anydb/cfg" @@ -90,13 +91,25 @@ func Execute() { slog.SetLogLoggerLevel(slog.LevelDebug) - handler := yadu.NewHandler(os.Stdout, opts) - debuglogger := slog.New(handler).With( + dbg, err := os.Create("debug.log") + if err != nil { + return err + } + + // FIXME: control this with a flag! + //outhandler := yadu.NewHandler(os.Stdout, opts) + filehandler := yadu.NewHandler(dbg, opts) + + debuglogger := slog.New(slogmulti.Fanout( + //outhandler, + filehandler, + )).With( slog.Group("program_info", slog.Int("pid", os.Getpid()), slog.String("go_version", buildInfo.GoVersion), ), ) + slog.SetDefault(debuglogger) slog.Debug("parsed config", "conf", conf) diff --git a/go.mod b/go.mod index 964f81f..a6aece3 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,8 @@ require ( github.com/muesli/termenv v0.15.2 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/sahilm/fuzzy v0.1.1 // indirect + github.com/samber/lo v1.49.1 // indirect + github.com/samber/slog-multi v1.4.0 // indirect github.com/spf13/pflag v1.0.6 // indirect github.com/tlinden/yadu v0.1.3 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect diff --git a/go.sum b/go.sum index 3891b08..858c3cd 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,10 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/samber/lo v1.49.1 h1:4BIFyVfuQSEpluc7Fua+j1NolZHiEHEpaSEKdsH0tew= +github.com/samber/lo v1.49.1/go.mod h1:dO6KHFzUKXgP8LDhU0oI8d2hekjXnGOu0DB8Jecxd6o= +github.com/samber/slog-multi v1.4.0 h1:pwlPMIE7PrbTHQyKWDU+RIoxP1+HKTNOujk3/kdkbdg= +github.com/samber/slog-multi v1.4.0/go.mod h1:FsQ4Uv2L+E/8TZt+/BVgYZ1LoDWCbfCU21wVIoMMrO8= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/ui/delegate.go b/ui/delegate.go index 377dd1f..8419972 100644 --- a/ui/delegate.go +++ b/ui/delegate.go @@ -2,6 +2,7 @@ package ui import ( "log" + "log/slog" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" @@ -16,9 +17,11 @@ func newItemDelegate(keys *delegateKeyMap, config *cfg.Config) list.DefaultDeleg d.UpdateFunc = func(msg tea.Msg, m *list.Model) tea.Cmd { var title string - if entry, ok := m.SelectedItem().(app.DbEntry); ok { + if entry, ok := m.SelectedItem().(item); ok { title = entry.Title() + slog.Debug("active entry", "entry", title) } else { + slog.Debug("no active entry") return nil } diff --git a/ui/root.go b/ui/root.go index 8d322c6..20d0da0 100644 --- a/ui/root.go +++ b/ui/root.go @@ -25,15 +25,6 @@ import ( "github.com/tlinden/anydb/cfg" ) -type model struct { - conf *cfg.Config - quitting bool - err error - list list.Model - keys *listKeyMap - delegateKeys *delegateKeyMap -} - var ( appStyle = lipgloss.NewStyle().Padding(1, 2) @@ -47,6 +38,39 @@ var ( Render ) +type Loader struct { + items []list.Item + conf *cfg.Config +} + +func (loader *Loader) Update() error { + entries, err := loader.conf.DB.List(&app.DbAttr{}, loader.conf.Fulltext) + if err != nil { + return err + } + + loader.items = nil + + for _, entry := range entries { + loader.items = append(loader.items, item{ + title: entry.Key, + description: entry.Preview, + }) + } + + return nil +} + +type model struct { + conf *cfg.Config + loader *Loader + quitting bool + err error + list list.Model + keys *listKeyMap + delegateKeys *delegateKeyMap +} + type listKeyMap struct { toggleSpinner key.Binding toggleTitleBar key.Binding @@ -56,6 +80,15 @@ type listKeyMap struct { insertItem key.Binding } +type item struct { + title string + description string +} + +func (i item) Title() string { return i.title } +func (i item) Description() string { return i.description } +func (i item) FilterValue() string { return i.title } + func newListKeyMap() *listKeyMap { return &listKeyMap{ insertItem: key.NewBinding( @@ -89,16 +122,16 @@ func NewModel(config *cfg.Config, entries app.DbEntries) model { var ( delegateKeys = newDelegateKeyMap() listKeys = newListKeyMap() + loader = Loader{conf: config} ) - items := []list.Item{} - for _, entry := range entries { - items = append(items, entry) + // Setup list + if err := loader.Update(); err != nil { + panic(err) } - // Setup list delegate := newItemDelegate(delegateKeys, config) - dbList := list.New(items, delegate, 0, 0) + dbList := list.New(loader.items, delegate, 0, 0) dbList.Title = "DB Entries" dbList.Styles.Title = titleStyle