Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refresh tree shortcut #123

Merged
merged 6 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 2 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
80 changes: 48 additions & 32 deletions components/Tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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)
}