Skip to content

Commit

Permalink
Merge pull request #123 from gytic/refresh-tree
Browse files Browse the repository at this point in the history
feat: Refresh tree shortcut
  • Loading branch information
jorgerojas26 authored Nov 30, 2024
2 parents 906ecd5 + ab57a00 commit 78be965
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
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 @@ -63,38 +63,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 @@ -205,6 +174,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 @@ -569,3 +540,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)
}

0 comments on commit 78be965

Please sign in to comment.