From 55ca224bee03d6b446a21336c462fd52e8e11dd2 Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Fri, 2 Feb 2024 06:49:51 +0800 Subject: [PATCH] feat: (key/get) and (key/current) --- pkg/bind/actions.go | 7 +++--- pkg/bind/bind_test.go | 4 ++-- pkg/bind/module.go | 4 ++-- pkg/bind/trie/module.go | 12 ++++++++-- pkg/bind/trie/trie_test.go | 4 ++-- pkg/cy/api/input.go | 7 ++++++ pkg/cy/api/key.go | 41 ++++++++++++++++++++++++++++++++ pkg/cy/api/module.go | 1 + pkg/cy/client.go | 17 +++++++++++++ pkg/cy/module.go | 2 +- pkg/mux/screen/replay/preview.go | 2 +- pkg/mux/screen/replay/stories.go | 2 +- pkg/mux/screen/tree/group.go | 8 +++---- pkg/mux/screen/tree/pane.go | 1 - pkg/mux/screen/tree/tree.go | 16 ++++++------- 15 files changed, 99 insertions(+), 29 deletions(-) diff --git a/pkg/bind/actions.go b/pkg/bind/actions.go index ca09db7d..b4057cfe 100644 --- a/pkg/bind/actions.go +++ b/pkg/bind/actions.go @@ -6,13 +6,12 @@ import ( ) type Action struct { - Description string - Callback *janet.Function + Callback *janet.Function } type BindScope = trie.Trie[Action] type BindEvent = ActionEvent[Action] -func NewBindScope() *BindScope { - return NewScope[Action]() +func NewBindScope(source interface{}) *BindScope { + return NewScope[Action](source) } diff --git a/pkg/bind/bind_test.go b/pkg/bind/bind_test.go index abc1d003..fc8c028e 100644 --- a/pkg/bind/bind_test.go +++ b/pkg/bind/bind_test.go @@ -37,7 +37,7 @@ func TestAction(t *testing.T) { engine := NewEngine[int]() go engine.Poll(context.Background()) - scope := NewScope[int]() + scope := NewScope[int](nil) scope.Set( []interface{}{"ctrl+a"}, 2, @@ -71,7 +71,7 @@ func TestIdle(t *testing.T) { } }() - scope := NewScope[int]() + scope := NewScope[int](nil) scope.Set( []interface{}{"ctrl+a", "a"}, 2, diff --git a/pkg/bind/module.go b/pkg/bind/module.go index 39973a19..4f762104 100644 --- a/pkg/bind/module.go +++ b/pkg/bind/module.go @@ -11,8 +11,8 @@ import ( "github.com/sasha-s/go-deadlock" ) -func NewScope[T any]() *trie.Trie[T] { - return trie.New[T]() +func NewScope[T any](source interface{}) *trie.Trie[T] { + return trie.New[T](source) } type Event interface{} diff --git a/pkg/bind/trie/module.go b/pkg/bind/trie/module.go index 418c02c2..492ae340 100644 --- a/pkg/bind/trie/module.go +++ b/pkg/bind/trie/module.go @@ -14,6 +14,9 @@ type Trie[T any] struct { // a mapping from raw regex pattern -> node or T nextRe map[string]*Regex + + // arbitrary extra data associated with this Trie + source interface{} } func (t *Trie[T]) resolve(key interface{}) (value interface{}, matched bool, regex bool) { @@ -84,7 +87,7 @@ func (t *Trie[T]) access(sequence []interface{}, shouldCreate bool) *Trie[T] { if !shouldCreate { return nil } - next = New[T]() + next = New[T](nil) } if shouldCreate { @@ -353,9 +356,14 @@ func (t *Trie[T]) Remap(from, to []interface{}) { } } -func New[T any]() *Trie[T] { +func (t *Trie[T]) Source() interface{} { + return t.source +} + +func New[T any](source interface{}) *Trie[T] { return &Trie[T]{ next: make(map[string]interface{}), nextRe: make(map[string]*Regex), + source: source, } } diff --git a/pkg/bind/trie/trie_test.go b/pkg/bind/trie/trie_test.go index 65e94714..165ebe67 100644 --- a/pkg/bind/trie/trie_test.go +++ b/pkg/bind/trie/trie_test.go @@ -16,7 +16,7 @@ func re(pattern string) *Regex { } func TestTrie(t *testing.T) { - trie := New[int]() + trie := New[int](nil) trie.Set([]interface{}{ "one", "two", @@ -75,7 +75,7 @@ func TestTrie(t *testing.T) { } func TestRegex(t *testing.T) { - trie := New[int]() + trie := New[int](nil) trie.Set([]interface{}{ re("[abc]"), "t", diff --git a/pkg/cy/api/input.go b/pkg/cy/api/input.go index d4030315..1503328b 100644 --- a/pkg/cy/api/input.go +++ b/pkg/cy/api/input.go @@ -76,6 +76,13 @@ func (i *InputModule) Find( settings = append(settings, fuzzy.WithAnimation(state.Image)) } + if params.Headers != nil { + settings = append( + settings, + fuzzy.WithHeaders(*params.Headers...), + ) + } + fuzzy := fuzzy.NewFuzzy( ctx, options, diff --git a/pkg/cy/api/key.go b/pkg/cy/api/key.go index 6a7f1bba..b4a4a7ba 100644 --- a/pkg/cy/api/key.go +++ b/pkg/cy/api/key.go @@ -140,3 +140,44 @@ func (k *KeyModule) Remap(target *janet.Value, from, to *janet.Value) error { return nil } + +type Binding struct { + Node tree.NodeID + Sequence []string + Function *janet.Value +} + +func NewBinding(node tree.Node, leaf trie.Leaf[bind.Action]) Binding { + return Binding{ + Node: node.Id(), + Sequence: leaf.Path, + Function: leaf.Value.Callback.Value, + } +} + +func (k *KeyModule) Get(target *janet.Value) ([]Binding, error) { + defer target.Free() + node, err := resolveNode(k.Tree, target) + if err != nil { + return nil, err + } + + binds := []Binding{} + for _, leaf := range node.Binds().Leaves() { + binds = append( + binds, + NewBinding(node, leaf), + ) + } + + return binds, nil +} + +func (k *KeyModule) Current(user interface{}) []Binding { + client, ok := user.(Client) + if !ok { + return nil + } + + return client.Binds() +} diff --git a/pkg/cy/api/module.go b/pkg/cy/api/module.go index 0eece21b..b3d83fde 100644 --- a/pkg/cy/api/module.go +++ b/pkg/cy/api/module.go @@ -15,4 +15,5 @@ type Client interface { OuterLayers() *screen.Layers Margins() *screen.Margins Frame() *frames.Framer + Binds() []Binding } diff --git a/pkg/cy/client.go b/pkg/cy/client.go index 4f3cf606..12af4907 100644 --- a/pkg/cy/client.go +++ b/pkg/cy/client.go @@ -398,6 +398,23 @@ func (c *Client) Frame() *frames.Framer { return c.frame } +func (c *Client) Binds() (binds []api.Binding) { + for _, scope := range c.binds.Scopes() { + node, ok := scope.Source().(tree.Node) + if !ok { + continue + } + + for _, leaf := range scope.Leaves() { + binds = append( + binds, + api.NewBinding(node, leaf), + ) + } + } + return +} + func (c *Client) Detach(reason string) error { //err := c.conn.Send(P.CloseMessage{ //Reason: reason, diff --git a/pkg/cy/module.go b/pkg/cy/module.go index 7a658a41..09949829 100644 --- a/pkg/cy/module.go +++ b/pkg/cy/module.go @@ -202,7 +202,7 @@ func (c *Cy) pollNodeEvents(ctx context.Context, events <-chan events.Msg) { } func Start(ctx context.Context, options Options) (*Cy, error) { - replayBinds := bind.NewBindScope() + replayBinds := bind.NewBindScope(nil) defaults := params.New() t := tree.NewTree(tree.WithParams(defaults.NewChild())) diff --git a/pkg/mux/screen/replay/preview.go b/pkg/mux/screen/replay/preview.go index 8c7d98ff..2c3aaeca 100644 --- a/pkg/mux/screen/replay/preview.go +++ b/pkg/mux/screen/replay/preview.go @@ -60,7 +60,7 @@ func (r *ReplayPreview) Init() tea.Cmd { replay := New( ctx, events, - bind.NewBindScope(), + bind.NewBindScope(nil), ) replay.Resize(size) diff --git a/pkg/mux/screen/replay/stories.go b/pkg/mux/screen/replay/stories.go index 43e3b904..3f9769bb 100644 --- a/pkg/mux/screen/replay/stories.go +++ b/pkg/mux/screen/replay/stories.go @@ -30,7 +30,7 @@ func createStorySession() []sessions.Event { } func createStory(ctx context.Context, events []sessions.Event, msgs ...interface{}) mux.Screen { - replay := New(ctx, events, bind.NewBindScope()) + replay := New(ctx, events, bind.NewBindScope(nil)) var realMsg tea.Msg for _, msg := range msgs { diff --git a/pkg/mux/screen/tree/group.go b/pkg/mux/screen/tree/group.go index 0e9b6b6e..dc73d902 100644 --- a/pkg/mux/screen/tree/group.go +++ b/pkg/mux/screen/tree/group.go @@ -50,8 +50,8 @@ func (g *Group) Leaves() []Node { } func (g *Group) NewPane(ctx context.Context, screen mux.Screen) *Pane { - metadata := g.tree.newMetadata() - pane := newPane(ctx, metadata.Id(), screen) + pane := newPane(ctx, screen) + metadata := g.tree.newMetadata(pane) pane.metaData = metadata metadata.params = g.params.NewChild() g.addNode(pane) @@ -76,9 +76,9 @@ func (g *Group) NewPane(ctx context.Context, screen mux.Screen) *Pane { func (g *Group) NewGroup() *Group { group := &Group{ - metaData: g.tree.newMetadata(), - tree: g.tree, + tree: g.tree, } + group.metaData = g.tree.newMetadata(group) group.params = g.params.NewChild() g.addNode(group) return group diff --git a/pkg/mux/screen/tree/pane.go b/pkg/mux/screen/tree/pane.go index 22e96c3f..f333a017 100644 --- a/pkg/mux/screen/tree/pane.go +++ b/pkg/mux/screen/tree/pane.go @@ -21,7 +21,6 @@ func (p *Pane) Screen() mux.Screen { func newPane( ctx context.Context, - id NodeID, s mux.Screen, ) *Pane { return &Pane{ diff --git a/pkg/mux/screen/tree/tree.go b/pkg/mux/screen/tree/tree.go index b75b41ae..4e1d71f9 100644 --- a/pkg/mux/screen/tree/tree.go +++ b/pkg/mux/screen/tree/tree.go @@ -19,18 +19,18 @@ type Tree struct { nextNodeID atomic.Int32 } -func (t *Tree) newMetadata() *metaData { +func (t *Tree) newMetadata(node Node) *metaData { t.Lock() defer t.Unlock() id := t.nextNodeID.Add(1) - node := &metaData{ + metadata := &metaData{ id: id, - binds: bind.NewBindScope(), + binds: bind.NewBindScope(node), name: fmt.Sprintf("%d", id), } - return node + return metadata } func (t *Tree) storeNode(node Node) { @@ -155,11 +155,9 @@ func NewTree(options ...TreeOption) *Tree { nodes: make(map[NodeID]Node), } - tree.root = &Group{ - metaData: tree.newMetadata(), - tree: tree, - } - + root := &Group{tree: tree} + root.metaData = tree.newMetadata(root) + tree.root = root tree.storeNode(tree.root) for _, option := range options {