Skip to content

Commit

Permalink
feat: swap to any pane
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 6, 2023
1 parent 55410ee commit 58c9de1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/cy/api/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ func (g *GroupModule) Children(parentId tree.NodeID) ([]tree.NodeID, error) {
return nodes, nil
}

func (g *GroupModule) Leaves(parentId tree.NodeID) ([]tree.NodeID, error) {
group, ok := g.Tree.GroupById(parentId)
if !ok {
return nil, fmt.Errorf("node not found: %d", parentId)
}

nodes := make([]tree.NodeID, 0)
for _, child := range group.Leaves() {
nodes = append(nodes, child.Id())
}
return nodes, nil
}

type PaneModule struct {
Tree *tree.Tree
}
Expand Down Expand Up @@ -126,6 +139,30 @@ func (t *TreeModule) Name(id tree.NodeID) *string {
return &name
}

func (t *TreeModule) Path(id tree.NodeID) *string {
node, ok := t.Tree.NodeById(id)
if !ok {
return nil
}

path := t.Tree.PathTo(node)
if path == nil {
return nil
}

var result string
for i, node := range path {
// skip the root node
if i == 0 {
continue
}

result += fmt.Sprintf("/%s", node.Name())
}

return &result
}

func (t *TreeModule) Parent(id tree.NodeID) *tree.NodeID {
node, ok := t.Tree.NodeById(id)
if !ok {
Expand Down
10 changes: 10 additions & 0 deletions pkg/cy/cy-boot.janet
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@
(def [next] next-panes)
(pane/attach next))

(key/def
cy/jump-pane
"jump to a pane"
(-?>>
(group/leaves (tree/root))
(map |(tuple (tree/path $) $ $))
(fzf/find)
(pane/attach)))

(key/def
cy/kill-current-pane
"kill the current pane"
Expand Down Expand Up @@ -125,6 +134,7 @@
(key/bind :root [prefix "l"] ot/jump-shell)
(key/bind :root ["ctrl+l"] ot/next-pane)

(key/bind :root [prefix ";"] cy/jump-pane)
(key/bind :root [prefix "ctrl+p"] cy/command-palette)
(key/bind :root [prefix "x"] cy/kill-current-pane)
(key/bind :root [prefix "g"] cy/toggle-margins)
Expand Down
4 changes: 4 additions & 0 deletions pkg/mux/screen/tree/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (g *Group) removeNode(node Node) {
g.Unlock()
}

func (g *Group) Leaves() []Node {
return getLeaves(g)
}

func (g *Group) NewPane(
ctx context.Context,
stream mux.Stream,
Expand Down

0 comments on commit 58c9de1

Please sign in to comment.