From c0e576751970a47e414f4ab50e6e46301e748ff4 Mon Sep 17 00:00:00 2001 From: gytic Date: Sat, 16 Nov 2024 23:37:08 +0100 Subject: [PATCH] add: refresh logic for tree - refactored tree node init - new function for reset - reset filter - delete old nodes -> init new nodes - TODO: close table view if table was deleted --- components/Tree.go | 97 +++++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/components/Tree.go b/components/Tree.go index 17ac075..99f7ff9 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -60,38 +60,7 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { tree.SetCurrentNode(rootNode) tree.SetFocusFunc(func() { - var databases []string - - if dbName == "" { - dbs, err := tree.DBDriver.GetDatabases() - if err != nil { - panic(err.Error()) - } - databases = dbs - } else { - databases = []string{dbName} - } - - if tree.GetSelectedDatabase() == "" { - for _, database := range databases { - childNode := tview.NewTreeNode(database) - childNode.SetExpanded(false) - childNode.SetReference(database) - childNode.SetColor(app.Styles.PrimaryTextColor) - rootNode.AddChild(childNode) - - go func(database string, node *tview.TreeNode) { - tables, err := tree.DBDriver.GetTables(database) - if err != nil { - logger.Error(err.Error(), nil) - return - } - - tree.databasesToNodes(tables, node, true) - App.Draw() - }(database, childNode) - } - } + tree.InitializeNodes(dbName) tree.SetFocusFunc(nil) }) @@ -202,6 +171,8 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { tree.CollapseAll() case commands.ExpandAll: tree.ExpandAll() + case commands.Refresh: + tree.Refresh(dbName) } return nil }) @@ -557,3 +528,65 @@ func (tree *Tree) ExpandAll() { return true }) } + +func (tree *Tree) InitializeNodes(dbName string) { + rootNode := tree.GetRoot() + if rootNode == nil { + panic("Internal Error: No tree root") + } + + var databases []string + + if dbName == "" { + dbs, err := tree.DBDriver.GetDatabases() + if err != nil { + panic(err.Error()) + } + databases = dbs + } else { + databases = []string{dbName} + } + + if tree.GetSelectedDatabase() == "" { + for _, database := range databases { + childNode := tview.NewTreeNode(database) + childNode.SetExpanded(false) + childNode.SetReference(database) + childNode.SetColor(app.Styles.PrimaryTextColor) + rootNode.AddChild(childNode) + + go func(database string, node *tview.TreeNode) { + tables, err := tree.DBDriver.GetTables(database) + if err != nil { + logger.Error(err.Error(), nil) + return + } + + tree.databasesToNodes(tables, node, true) + App.Draw() + }(database, childNode) + } + } +} + +func (tree *Tree) Refresh(dbName string) { + rootNode := tree.GetRoot() + if rootNode == nil { + return + } + // remove existing Children + rootNode.ClearChildren() + // readd nodes + tree.InitializeNodes(dbName) + // reset search + tree.search("") + + /** TODO: + check if the currently opened table was deleted + - if deleted + - selecedTable and selectedDatabase need to be reset + - close Table view + - if not + - do nothing + */ +}