From 367d62ff009186e5aa584fd069dd57aaf7d46a8a Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 5 Feb 2026 11:46:41 -0600 Subject: [PATCH] Tree Browser Work --- app/screen_home.go | 39 ++++--- cmd/root.go | 2 + data/app_log_handler.go | 2 +- data/repo.go | 28 +++-- data/repo_auth.go | 17 ++- data/repo_auth_store.go | 14 +++ widgets/status_bar_block.go | 2 +- widgets/tree_browser.go | 221 ++++++++++++++++++++++++++++++++++-- 8 files changed, 286 insertions(+), 39 deletions(-) diff --git a/app/screen_home.go b/app/screen_home.go index 40d8f12..3fd382b 100644 --- a/app/screen_home.go +++ b/app/screen_home.go @@ -23,8 +23,10 @@ package app import ( "fmt" + "log/slog" "net/url" "strings" + "time" "git.bullercodeworks.com/brian/expds/data" "git.bullercodeworks.com/brian/expds/data/models" @@ -43,8 +45,7 @@ type ScreenHome struct { w, h int style tcell.Style - alert *w.Alert - showAlert bool + alert *w.Alert menuLayout *w.TopMenuLayout @@ -164,9 +165,6 @@ func (s *ScreenHome) HandleResize(ev *tcell.EventResize) { } func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool { - if s.showAlert { - return s.alert.HandleKey(ev) - } if ev.Key() == tcell.KeyF12 { s.toggleCli() return true @@ -213,9 +211,6 @@ func (s *ScreenHome) Draw() { s.a.DrawWidget(s.loading) } s.a.DrawWidget(s.status) - if s.showAlert { - s.a.DrawWidget(s.alert) - } } func (s *ScreenHome) Exit() error { return nil } func (s *ScreenHome) Log(t string, a ...any) { @@ -316,9 +311,11 @@ func (s *ScreenHome) initCli() { func (s *ScreenHome) toggleCli() { if s.layout.Contains(s.cli) { s.layout.Delete(s.cli) + s.layout.ActivateWidget(s.columns) } else { s.layout.Add(s.cli) s.cli.SetVisible(true) + s.layout.ActivateWidget(s.cli) } } func (s *ScreenHome) hideCli() { @@ -344,7 +341,13 @@ func (s *ScreenHome) cliGetPds(args ...string) bool { } go func() { defer func() { s.isLoading = false }() - pds, err := s.r.GetPDS(args[1]) + var pds *models.Pds + var err error + if s.activePds != nil && string(s.activePds.AtId) == args[1] { + pds, err = s.r.ReloadPds(args[1]) + } else { + pds, err = s.r.GetPDS(args[1]) + } if err != nil { s.Log(err.Error()) return @@ -353,7 +356,7 @@ func (s *ScreenHome) cliGetPds(args ...string) bool { return } s.doOpen = false - s.cli.Log("Retrieved: %s (%s)", pds.AtId, pds.Did) + slog.Default().Debug(fmt.Sprintf("Retrieved: %s (%s) at %s", pds.AtId, pds.Did, time.Now().Format(time.RFC3339))) s.activePds = pds s.updatePdsListing() n, err := s.pdsListing.GetActiveNode() @@ -388,9 +391,8 @@ func (s *ScreenHome) cliAuthPds(args ...string) bool { if err != nil { s.Log("Error starting auth flow: %w", err) } - s.alert.SetTitle("Authentication Started") - s.alert.SetMessage(fmt.Sprintf("OAuth Process Started.\nIf a browser window didn't open, you can open this URL manually:\n%s", authUrl)) - s.showAlert = true + s.Log("Authentication Started") + s.Log(fmt.Sprintf("OAuth Process Started.\nIf a browser window didn't open, you can open this URL manually:\n%s", strings.ReplaceAll(authUrl, "%", "%%"))) }() return true } @@ -421,11 +423,14 @@ func (s *ScreenHome) cliSendStatus(args ...string) bool { s.isLoading = true go func() { defer func() { s.isLoading = false }() - if !s.r.Auth.HasAuth() { + if !s.r.Auth.HasAuth(s.activePds.Did) { s.Log("Not authorized. Run `authpds`") return } - s.r.SendToPDS() + err := s.r.SendToPDS(s.activePds.Did) + if err != nil { + s.Log("Error sending status: %w", err) + } }() return true } @@ -434,6 +439,7 @@ func (s *ScreenHome) updatePdsListing() { s.pdsListing.SetTitle(fmt.Sprintf("─ %s (%s)", s.activePds.AtId.String(), s.activePds.Did.String())) s.pdsListing.Clear() nsidList := s.activePds.NSIDStringList() + var tree []*wd.TreeNode for i, v := range nsidList { t := wd.NewTreeNode(v, v) nsid := s.activePds.NSIDs[i] @@ -456,8 +462,9 @@ func (s *ScreenHome) updatePdsListing() { c := wd.NewTreeNode(label, rIds[j]) t.AddChild(c) } - s.pdsListing.Add(t) + tree = append(tree, t) } + s.pdsListing.SetTree(tree) s.layout.ActivateWidget(s.columns) s.columns.ActivateWidget(s.pdsListing) s.pdsListing.SetActive(true) diff --git a/cmd/root.go b/cmd/root.go index 127a8ba..1da2d09 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -41,6 +41,7 @@ var ( Name = "expds" cfgFile string ConfigDir = "" + debug = false rootCmd = &cobra.Command{ Use: "expds", Short: "Utility to edit and view PDS", @@ -60,6 +61,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") + rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug mode") } func initConfig() { diff --git a/data/app_log_handler.go b/data/app_log_handler.go index 618f040..f65a674 100644 --- a/data/app_log_handler.go +++ b/data/app_log_handler.go @@ -27,7 +27,7 @@ func (a *AppLogHandler) addGroups(groups ...string) { a.groups = append(a.group // AppLogHandler can handle all levels func (a *AppLogHandler) Enabled(_ context.Context, lvl slog.Level) bool { return lvl >= a.level } -func (a *AppLogHandler) Handle(ctx context.Context, rcd slog.Record) error { +func (a *AppLogHandler) Handle(_ context.Context, rcd slog.Record) error { if a.logFunc == nil { return errors.New("no log func defined") } diff --git a/data/repo.go b/data/repo.go index d3a2f87..41a5d15 100644 --- a/data/repo.go +++ b/data/repo.go @@ -40,6 +40,7 @@ type Repo struct { handler *AppLogHandler logFunc func(string, ...any) + Logger *slog.Logger context context.Context } @@ -56,6 +57,7 @@ func NewRepo() (*Repo, error) { } else { r.handler.SetLevel(slog.LevelWarn) } + r.Logger = slog.Default() var err error r.Auth, err = NewAuthRepo(r) if err != nil { @@ -64,10 +66,7 @@ func NewRepo() (*Repo, error) { return r, nil } -func (r *Repo) GetPDS(atId string) (*models.Pds, error) { - if p, ok := r.LoadedPDSs[atId]; ok && time.Since(p.RefreshTime) < r.BestBy { - return p, nil - } +func (r *Repo) fetchPds(atId string) (*models.Pds, error) { p, err := models.NewPdsFromDid(atId) if err != nil { return nil, err @@ -76,8 +75,19 @@ func (r *Repo) GetPDS(atId string) (*models.Pds, error) { return p, nil } -func (r *Repo) SendToPDS() error { - session, err := r.Auth.GetSession() +func (r *Repo) ReloadPds(atId string) (*models.Pds, error) { + return r.fetchPds(atId) +} + +func (r *Repo) GetPDS(atId string) (*models.Pds, error) { + if p, ok := r.LoadedPDSs[atId]; ok && time.Since(p.RefreshTime) < r.BestBy { + return p, nil + } + return r.fetchPds(atId) +} + +func (r *Repo) SendToPDS(did syntax.DID) error { + session, err := r.Auth.GetSession(did) if err != nil { return err } @@ -94,11 +104,11 @@ func (r *Repo) SendToPDS() error { var resp struct { Uri syntax.ATURI `json:"uri"` } - slog.Debug("posting expds status...") + r.Logger.Debug("posting expds status...") if err := c.Post(r.context, "com.atproto.repo.CreateRecord", body, &resp); err != nil { return err } - slog.Debug(fmt.Sprintf("posted: %s :: %s", resp.Uri.Authority(), resp.Uri.RecordKey())) + r.Logger.Debug(fmt.Sprintf("posted: %s :: %s", resp.Uri.Authority(), resp.Uri.RecordKey())) return nil } @@ -106,4 +116,6 @@ func (r *Repo) SetLogFunc(l func(string, ...any)) { r.logFunc = l r.handler = NewAppLogHandler(r.logFunc) slog.SetDefault(slog.New(r.handler)) + r.Logger = slog.Default() + r.Logger.Debug("New Log Func Set for slog") } diff --git a/data/repo_auth.go b/data/repo_auth.go index 4a51d1b..cb6b7de 100644 --- a/data/repo_auth.go +++ b/data/repo_auth.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "log/slog" "net" "net/http" "net/url" @@ -13,6 +14,7 @@ import ( "time" "github.com/bluesky-social/indigo/atproto/auth/oauth" + "github.com/bluesky-social/indigo/atproto/syntax" "github.com/spf13/viper" ) @@ -24,12 +26,15 @@ type AuthRepo struct { context context.Context session *oauth.ClientSessionData + + Logger *slog.Logger } func NewAuthRepo(r *Repo) (*AuthRepo, error) { a := &AuthRepo{ r: r, context: r.context, + Logger: r.Logger, } var err error a.oauthConfig, a.oauthClient, a.store, err = a.buildOAuthClient() @@ -110,20 +115,22 @@ func (r *AuthRepo) ListenForCallback(res chan url.Values) (int, error) { if !errors.Is(err, http.ErrServerClosed) { panic(err) } + r.Logger.Debug("Server Shut Down") }() return listener.Addr().(*net.TCPAddr).Port, nil } -func (r *AuthRepo) HasAuth() bool { - sess, err := r.store.GetMostRecentSession(r.context) - return err != nil || sess == nil +func (r *AuthRepo) HasAuth(did syntax.DID) bool { + sess, err := r.GetSession(did) + return err == nil && sess != nil } -func (r *AuthRepo) GetSession() (*oauth.ClientSession, error) { - sess, err := r.store.GetMostRecentSession(r.context) +func (r *AuthRepo) GetSession(did syntax.DID) (*oauth.ClientSession, error) { + sess, err := r.store.GetMostRecentSessionFor(r.context, did) if err != nil { return nil, fmt.Errorf("error getting most recent session: %w", err) } + r.Logger.Warn(fmt.Sprintf("GetSession(): Resuming Session: %s (%s)", sess.SessionID, sess.AccountDID)) return r.oauthClient.ResumeSession(r.context, sess.AccountDID, sess.SessionID) } diff --git a/data/repo_auth_store.go b/data/repo_auth_store.go index 03ab6cc..2062325 100644 --- a/data/repo_auth_store.go +++ b/data/repo_auth_store.go @@ -96,6 +96,20 @@ func (m *SqliteStore) GetSession(ctx context.Context, did syntax.DID, sessionID return &row.Data, nil } +func (m *SqliteStore) GetMostRecentSessionFor(ctx context.Context, did syntax.DID) (*oauth.ClientSessionData, error) { + var row storedSessionData + res := m.db.WithContext(ctx).Where(&storedSessionData{ + AccountDid: did, + }).Order(clause.OrderByColumn{ + Column: clause.Column{Name: "updated_at"}, + Desc: true, + }).First(&row) + if res.Error != nil { + return nil, res.Error + } + return &row.Data, nil +} + // not part of the ClientAuthStore interface, just used for the CLI app func (m *SqliteStore) GetMostRecentSession(ctx context.Context) (*oauth.ClientSessionData, error) { var row storedSessionData diff --git a/widgets/status_bar_block.go b/widgets/status_bar_block.go index 78ded75..cfd75f3 100644 --- a/widgets/status_bar_block.go +++ b/widgets/status_bar_block.go @@ -114,7 +114,7 @@ func (b *StatusBlock) DrawPath(screen tcell.Screen) { x := b.x wh.DrawText(x, b.y, b.end, b.style, screen) x++ - pts := fmt.Sprintf(" %s ", strings.Join(b.parts, b.sep)) + pts := fmt.Sprintf(" %s ", strings.Join(b.parts, b.sep)) wh.DrawText(x, b.y, pts, b.style, screen) x += len(pts) - len(b.parts) wh.DrawText(x, b.y, b.end, b.style.Reverse(true), screen) diff --git a/widgets/tree_browser.go b/widgets/tree_browser.go index a6fcdc3..e5ab0d0 100644 --- a/widgets/tree_browser.go +++ b/widgets/tree_browser.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "sync" h "git.bullercodeworks.com/brian/expds/helpers" t "git.bullercodeworks.com/brian/tcell-widgets" @@ -36,7 +37,12 @@ type TreeBrowser struct { keyMap *t.KeyMap vimMode bool + searching bool + searchStr string + logger func(string, ...any) + + m sync.Mutex } var _ t.Widget = (*TreeBrowser)(nil) @@ -55,6 +61,10 @@ func (w *TreeBrowser) Init(id string, style tcell.Style) { t.NewKey(t.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }), t.NewKey(t.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }), t.NewKey(t.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool { + if w.searching { + w.searching = !w.searching + return true + } if w.onSelect != nil { n, err := w.GetActiveNode() if err != nil || n == nil { @@ -97,6 +107,14 @@ func (w *TreeBrowser) Init(id string, style tcell.Style) { } return false }), + t.NewKey(t.BuildEKr('/'), func(ev *tcell.EventKey) bool { + if !w.searching { + w.searching = true + w.searchStr = "" + return true + } + return false + }), ) } @@ -111,12 +129,20 @@ func (w *TreeBrowser) HandleKey(ev *tcell.EventKey) bool { if !w.active || !w.focusable { return false } - return w.keyMap.Handle(ev) + if w.keyMap.Handle(ev) { + return true + } else if w.searching { + w.updateSearch(ev) + return true + } + return false } func (w *TreeBrowser) HandleTime(ev *tcell.EventTime) {} func (w *TreeBrowser) Draw(screen tcell.Screen) { + w.m.Lock() + defer w.m.Unlock() if !w.visible { return } @@ -170,9 +196,23 @@ func (w *TreeBrowser) Draw(screen tcell.Screen) { if len(txt) > w.w-brdSz && w.w-brdSz >= 0 { txt = txt[:(w.w - brdSz)] } - wh.DrawText(x, y, txt, dS.Reverse(rev), screen) + if w.searching && strings.Contains(txt, w.searchStr) { + // TODO: Fix multi-byte depth indicator + srchIdx := strings.Index(txt, w.searchStr) + endSrchIdx := srchIdx + len(w.searchStr) + wh.DrawText(x, y, txt[:srchIdx], dS.Reverse(rev), screen) + wh.DrawText(x+srchIdx, y, txt[srchIdx:endSrchIdx], dS.Reverse(!rev), screen) + if len(txt) > endSrchIdx { + wh.DrawText(x+endSrchIdx, y, txt[endSrchIdx:], dS.Reverse(rev), screen) + } + } else { + wh.DrawText(x, y, txt, dS.Reverse(rev), screen) + } y += 1 } + if w.searching { + wh.DrawText(w.x, w.y+w.h, fmt.Sprintf("Searching: %s", w.searchStr), dS, screen) + } } func (w *TreeBrowser) SetStyle(s tcell.Style) { w.style = s } @@ -197,6 +237,8 @@ func (w *TreeBrowser) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) } func (w *TreeBrowser) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) } func (w *TreeBrowser) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y } func (w *TreeBrowser) WantW() int { + w.m.Lock() + defer w.m.Unlock() var want int for i := range w.list { want = h.MaxI(want, len(w.list[i])) @@ -204,6 +246,8 @@ func (w *TreeBrowser) WantW() int { return w.w } func (w *TreeBrowser) WantH() int { + w.m.Lock() + defer w.m.Unlock() want := len(w.list) if len(w.border) > 0 { return want + 2 @@ -225,8 +269,8 @@ func (w *TreeBrowser) ClearBorder() { w.border = []rune{} func (w *TreeBrowser) SetOnChange(c func(*TreeNode) bool) { w.onChange = c } func (w *TreeBrowser) SetOnSelect(s func(*TreeNode) bool) { w.onSelect = s } func (w *TreeBrowser) SetVimMode(b bool) { w.vimMode = b } -func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) { - if len(w.listNodes) < 0 { +func (w *TreeBrowser) nmGetActiveNode() (*TreeNode, error) { + if len(w.listNodes) <= 0 { return nil, errors.New("no nodes") } if w.cursor < 0 { @@ -237,6 +281,11 @@ func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) { } return w.listNodes[w.cursor], nil } +func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) { + w.m.Lock() + defer w.m.Unlock() + return w.nmGetActiveNode() +} func (w *TreeBrowser) SetCursorWrap(b bool) { w.cursorWrap = b } func (w *TreeBrowser) MoveUp() bool { if w.cursor > 0 { @@ -305,22 +354,30 @@ func (w *TreeBrowser) PageDn() bool { func (w *TreeBrowser) Title() string { return w.title } func (w *TreeBrowser) SetTitle(ttl string) { w.title = ttl } func (w *TreeBrowser) SetTree(l []*TreeNode) { + w.m.Lock() + defer w.m.Unlock() w.nodes = l - w.UpdateList() + w.nmUpdateList() } func (w *TreeBrowser) Clear() { + w.m.Lock() + defer w.m.Unlock() w.nodes = []*TreeNode{} - w.UpdateList() + w.nmUpdateList() } func (w *TreeBrowser) Add(n *TreeNode) { + w.m.Lock() + defer w.m.Lock() if n.depthIndic == "" { n.depthIndic = w.depthIndic } w.nodes = append(w.nodes, n) - w.UpdateList() + w.nmUpdateList() } -func (w *TreeBrowser) UpdateList() { +// Update the list, intended to be called locally within other functions that +// handle the mutex +func (w *TreeBrowser) nmUpdateList() { w.list = []string{} w.listNodes = []*TreeNode{} for i := range w.nodes { @@ -335,6 +392,98 @@ func (w *TreeBrowser) UpdateList() { } } +func (w *TreeBrowser) UpdateList() { + w.m.Lock() + defer w.m.Unlock() + w.nmUpdateList() +} + +func (w *TreeBrowser) nmSetNodeActive(tn *TreeNode) { + // Make sure that the selected node is visible + wrk := tn.parent + for wrk != nil { + wrk.expanded = true + wrk = wrk.parent + } + w.nmUpdateList() + for i := range w.listNodes { + if w.listNodes[i] == tn { + w.cursor = i + return + } + } +} +func (w *TreeBrowser) SetNodeActive(tn *TreeNode) { + w.m.Lock() + defer w.m.Unlock() + w.nmSetNodeActive(tn) +} + +func (w *TreeBrowser) updateSearch(ev *tcell.EventKey) { + w.m.Lock() + defer w.m.Unlock() + + if len(w.nodes) == 0 { + return + } + + if wh.IsBS(*ev) { + if len(w.searchStr) > 0 { + w.searchStr = w.searchStr[:len(w.searchStr)-1] + if len(w.searchStr) == 0 { + w.searching = false + } + } + return + } + w.searchStr = fmt.Sprintf("%s%s", w.searchStr, string(ev.Rune())) + wrk, _ := w.nmGetActiveNode() + if wrk == nil { + wrk = w.nodes[0] + } + // Check the ative node & it's children for the search + if fnd := wrk.SearchLabels(w.searchStr); fnd != nil { + w.nmSetNodeActive(fnd) + return + } + + // Didn't find a child of the active node that matched, look for a sibling + if wrk.parent != nil { + if fnd := wrk.parent.SearchLabels(w.searchStr); fnd != nil { + w.nmSetNodeActive(fnd) + return + } + } + // Check the next browser node + var stIdx int + for i := range w.nodes { + if w.nodes[i] == wrk { + stIdx = i + 1 + break + } + } + for i := 0; i < len(w.nodes); i++ { + idx := (i + stIdx) % len(w.nodes) + if fnd := w.nodes[idx].SearchLabels(w.searchStr); fnd != nil { + w.nmSetNodeActive(fnd) + return + } + } +} + +func (w *TreeBrowser) getCurrentLine() string { + w.m.Lock() + defer w.m.Unlock() + l := len(w.list) + if l == 0 { + return "" + } + if w.cursor < 0 || w.cursor >= l { + return "" + } + return w.list[w.cursor] +} + /* * Tree Node */ @@ -370,6 +519,7 @@ func (tn *TreeNode) GetLabelPath() []string { } return append(path, tn.Label()) } + func (tn *TreeNode) getList() []string { pre := strings.Repeat(tn.depthIndic, tn.Depth()) ret := []string{fmt.Sprintf("%s%s", pre, tn.label)} @@ -380,6 +530,7 @@ func (tn *TreeNode) getList() []string { } return ret } + func (tn *TreeNode) getVisibleNodeList() []*TreeNode { ret := []*TreeNode{tn} if tn.expanded { @@ -390,6 +541,19 @@ func (tn *TreeNode) getVisibleNodeList() []*TreeNode { return ret } +func (tn *TreeNode) SearchLabels(f string) *TreeNode { + if strings.Contains(tn.label, f) { + return tn + } + for i := 0; i < len(tn.children); i++ { + fnd := tn.children[i].SearchLabels(f) + if fnd != nil { + return fnd + } + } + return nil +} + func (tn *TreeNode) ToggleExpand() { tn.expanded = !tn.expanded } func (tn *TreeNode) AddChild(t *TreeNode, rest ...*TreeNode) { @@ -415,3 +579,44 @@ func (tn *TreeNode) GetPath() []string { } return append(path, tn.value) } + +func (tn *TreeNode) GetChildren() []*TreeNode { return tn.children } + +func (tn *TreeNode) GetFirstChild() *TreeNode { + if !tn.HasChildren() { + return nil + } + return tn.children[0] +} + +func (tn *TreeNode) GetPrevChild(before *TreeNode) *TreeNode { + if !tn.HasChildren() { + return nil + } + var found bool + for i := len(tn.children) - 1; i >= 0; i-- { + if found { + return tn.children[i] + } + if tn.children[i] == before { + found = true + } + } + return nil +} + +func (tn *TreeNode) GetNextChild(after *TreeNode) *TreeNode { + if !tn.HasChildren() { + return nil + } + var found bool + for i := range tn.children { + if found { + return tn.children[i] + } + if tn.children[i] == after { + found = true + } + } + return nil +}