diff --git a/app/Keymap.go b/app/Keymap.go index b1332f3..86db4f0 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -80,6 +80,7 @@ var Keymaps = KeymapSystem{ Bind{Key: Key{Char: 'p'}, Cmd: cmd.PreviousFoundNode, Description: "Go to previous found node"}, Bind{Key: Key{Char: 'c'}, Cmd: cmd.TreeCollapseAll, Description: "Collapse all"}, Bind{Key: Key{Char: 'e'}, Cmd: cmd.ExpandAll, Description: "Expand all"}, + Bind{Key: Key{Char: 'R'}, Cmd: cmd.Refresh, Description: "Refresh tree"}, }, TreeFilterGroup: { Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.UnfocusTreeFilter, Description: "Unfocus tree filter"}, diff --git a/commands/commands.go b/commands/commands.go index 75abc6f..4a4cf45 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -162,6 +162,8 @@ func (c Command) String() string { return "EditConnection" case DeleteConnection: return "DeleteConnection" + case Refresh: + return "Refresh" case UnfocusEditor: return "UnfocusEditor" case RecordsMenu: diff --git a/components/Tree.go b/components/Tree.go index 17ac075..008793a 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,48 @@ 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} + } + + 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() + rootNode.ClearChildren() + // re-add nodes + tree.InitializeNodes(dbName) +}