Skip to content

Commit

Permalink
feat: (key/get) and (key/current)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Feb 1, 2024
1 parent ef99050 commit 55ca224
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 29 deletions.
7 changes: 3 additions & 4 deletions pkg/bind/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions pkg/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestIdle(t *testing.T) {
}
}()

scope := NewScope[int]()
scope := NewScope[int](nil)
scope.Set(
[]interface{}{"ctrl+a", "a"},
2,
Expand Down
4 changes: 2 additions & 2 deletions pkg/bind/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
12 changes: 10 additions & 2 deletions pkg/bind/trie/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
4 changes: 2 additions & 2 deletions pkg/bind/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions pkg/cy/api/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions pkg/cy/api/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
1 change: 1 addition & 0 deletions pkg/cy/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Client interface {
OuterLayers() *screen.Layers
Margins() *screen.Margins
Frame() *frames.Framer
Binds() []Binding
}
17 changes: 17 additions & 0 deletions pkg/cy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion pkg/mux/screen/replay/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *ReplayPreview) Init() tea.Cmd {
replay := New(
ctx,
events,
bind.NewBindScope(),
bind.NewBindScope(nil),
)
replay.Resize(size)

Expand Down
2 changes: 1 addition & 1 deletion pkg/mux/screen/replay/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/mux/screen/tree/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/mux/screen/tree/pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (p *Pane) Screen() mux.Screen {

func newPane(
ctx context.Context,
id NodeID,
s mux.Screen,
) *Pane {
return &Pane{
Expand Down
16 changes: 7 additions & 9 deletions pkg/mux/screen/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 55ca224

Please sign in to comment.