Skip to content

Commit

Permalink
show children in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Sep 8, 2021
1 parent 9ff5970 commit 87305ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
63 changes: 47 additions & 16 deletions ui/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"

"code.rocketnine.space/tslocum/cbind"
Expand All @@ -20,6 +21,32 @@ type RepoTree struct {
inputHandler *cbind.Configuration
}

func (r *RepoTree) buildNodes(basePath string, entries ...*api.MfsLsEntry) []*cview.TreeNode {
nodes := []*cview.TreeNode{}
for _, i := range entries {
fmt.Println(i.Name)
node := cview.NewTreeNode(i.Name)
node.SetReference(i)

if i.Type == api.TDirectory {

path := filepath.Join(basePath, i.Name)
children, err := r.app.client.ListFiles(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

childrenNodes := r.buildNodes(path, children...)
node.SetChildren(childrenNodes)
node.SetExpanded(false)
}

nodes = append(nodes, node)
}
return nodes
}

func NewRepoTree(app *App) *RepoTree {
m := &RepoTree{
TreeView: cview.NewTreeView(),
Expand All @@ -44,23 +71,9 @@ func NewRepoTree(app *App) *RepoTree {
os.Exit(1)
}

for _, i := range entries {
node := cview.NewTreeNode(i.Name)
node.SetReference(i)
rootNode.AddChild(node)
}

m.SetSelectedFunc(func(n *cview.TreeNode) {
ref := n.GetReference()
entry, ok := ref.(*api.MfsLsEntry)
if !ok {
return
}

m.app.info.SetItem(entry)
m.app.dag.SetItem(entry)
nodes := m.buildNodes("/", entries...)
rootNode.SetChildren(nodes)

})
return m
}
func (r *RepoTree) handleOpen(ev *tcell.EventKey) *tcell.EventKey {
Expand All @@ -76,7 +89,25 @@ func (r *RepoTree) handleOpen(ev *tcell.EventKey) *tcell.EventKey {

}

func (r *RepoTree) handleSelect(ev *tcell.EventKey) *tcell.EventKey {
node := r.GetCurrentNode()
ref := node.GetReference()
entry, ok := ref.(*api.MfsLsEntry)
if !ok {
return nil
}

if len(node.GetChildren()) > 0 {
node.SetExpanded(true)
}

r.app.info.SetItem(entry)
r.app.dag.SetItem(entry)
return nil
}

func (t *RepoTree) initBindings() {
t.inputHandler.SetKey(tcell.ModNone, tcell.KeyEnter, t.handleSelect)
t.inputHandler.SetRune(tcell.ModNone, 'o', t.handleOpen)
t.SetInputCapture(t.inputHandler.Capture)

Expand Down
2 changes: 1 addition & 1 deletion ui/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (i *FileInfo) SetItem(entry *api.MfsLsEntry) {

stat, err := i.app.client.StatEntry(entry)
if err != nil {
panic(err)
return
}
info = fmt.Sprintf("%s\n%s", entry.Name, stat)

Expand Down
2 changes: 1 addition & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (app *App) initViews() {
mid := cview.NewFlex()
mid.SetBackgroundColor(tcell.ColorDefault)
mid.SetDirection(cview.FlexRow)
mid.AddItem(app.panels, 0, 4, true)
// mid.AddItem(app.panels, 0, 4, true)
mid.AddItem(app.info, 0, 4, false)
mid.AddItem(app.dag, 0, 4, false)

Expand Down

0 comments on commit 87305ce

Please sign in to comment.