Skip to content

Commit

Permalink
feat: working end-to-end pane previews
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Oct 31, 2023
1 parent b6bf378 commit 4c95a90
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 152 deletions.
10 changes: 5 additions & 5 deletions pkg/cy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,25 @@ func (c *Client) initialize(handshake *P.HandshakeMessage) error {

c.muxClient = c.cy.muxServer.AddClient(
c.Ctx(),
info,
handshake.Size,
)

c.innerLayers = screen.NewLayers()
c.innerLayers.NewLayer(
c.Ctx(),
c.muxClient,
true,
true,
screen.PositionTop,
screen.WithOpaque,
screen.WithInteractive,
)
c.margins = screen.NewMargins(c.Ctx(), c.innerLayers)

c.outerLayers = screen.NewLayers()
c.outerLayers.NewLayer(
c.Ctx(),
c.margins,
true,
true,
screen.PositionTop,
screen.WithInteractive,
)

c.raw = emu.New(emu.WithSize(handshake.Size))
Expand Down
4 changes: 2 additions & 2 deletions pkg/cy/cy-boot.janet
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
(fn [&]
(-?>>
(group/children projects)
(map |(tuple (tree/name $) $))
(map |(tuple (tree/name $) ((group/children $) 0) $))
(fzf/find)
(group/children)
(0) # Gets the first index, the editor
Expand All @@ -54,7 +54,7 @@
(fn [&]
(-?>>
(group/children shells)
(map |(tuple (cmd/path $) $))
(map |(tuple (cmd/path $) $ $))
(fzf/find)
(pane/attach))))

Expand Down
37 changes: 11 additions & 26 deletions pkg/cy/janet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"

"github.com/cfoust/cy/pkg/anim"
"github.com/cfoust/cy/pkg/bind"
"github.com/cfoust/cy/pkg/cy/api"
"github.com/cfoust/cy/pkg/fuzzy"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/janet"
"github.com/cfoust/cy/pkg/mux/screen"
"github.com/cfoust/cy/pkg/mux/screen/tree"
"github.com/cfoust/cy/pkg/util"
)
Expand All @@ -19,11 +19,6 @@ import _ "embed"
//go:embed cy-boot.janet
var CY_BOOT_FILE []byte

type CmdParams struct {
Command string
Args []string
}

// execute runs some Janet code on behalf of the Client. This is only used in testing.
func (c *Client) execute(code string) error {
return c.cy.janet.ExecuteCall(c.Ctx(), c, janet.Call{
Expand Down Expand Up @@ -146,36 +141,26 @@ func (c *Cy) initJanet(ctx context.Context) (*janet.VM, error) {
return nil, err
}

cursor := client.outerLayers.State().Cursor
outerLayers := client.outerLayers
state := outerLayers.State()
cursor := state.Cursor
result := make(chan interface{})
fuzzy := fuzzy.NewFuzzy(
ctx,
client.info,
state.Image,
options,
geom.Vec2{
R: cursor.Y,
C: cursor.X,
},
geom.Vec2{R: cursor.Y, C: cursor.X},
c.tree,
c.muxServer.AddClient(ctx, geom.Vec2{}),
result,
)

client.outerLayers.NewLayer(
fuzzy.Ctx(),
anim.NewAnimator(
fuzzy.Ctx(),
anim.Random(),
client.outerLayers.State().Image,
23,
),
false,
true,
)

client.outerLayers.NewLayer(
fuzzy.Ctx(),
fuzzy,
true,
false,
screen.PositionTop,
screen.WithInteractive,
screen.WithOpaque,
)

select {
Expand Down
51 changes: 47 additions & 4 deletions pkg/fuzzy/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cfoust/cy/pkg/fuzzy/fzf"
"github.com/cfoust/cy/pkg/fuzzy/fzf/util"
"github.com/cfoust/cy/pkg/janet"
"github.com/cfoust/cy/pkg/mux/screen/tree"
)

type Match struct {
Expand All @@ -14,10 +15,14 @@ type Match struct {
}

type Option struct {
Text string
Chars *util.Chars
Match *Match
Result interface{}
Text string
// Supported types:
// - string: will be passed to a blank terminal
// - NodeID: will show in the background
Preview interface{}
Chars *util.Chars
Match *Match
Result interface{}
}

type tupleInput struct {
Expand All @@ -26,6 +31,13 @@ type tupleInput struct {
Value *janet.Value
}

type tripleInput struct {
_ struct{} `janet:"tuple"`
Text string
Preview *janet.Value
Value *janet.Value
}

func createOption(text string, result interface{}) Option {
chars := util.ToChars([]byte(text))
return Option{
Expand Down Expand Up @@ -64,6 +76,37 @@ func UnmarshalOptions(input *janet.Value) (result []Option, err error) {
return
}

var triples []tripleInput
var previewNode tree.NodeID
var previewStr string
err = input.Unmarshal(&triples)
if err == nil {
for i, triple := range triples {
option := createOption(
triple.Text,
triple.Value,
)

nodeErr := triple.Preview.Unmarshal(&previewNode)
strErr := triple.Preview.Unmarshal(&previewStr)
if nodeErr != nil && strErr != nil {
err = fmt.Errorf("preview for choice %d had invalid type", i)
return
}

if nodeErr == nil {
option.Preview = previewNode
}

if strErr == nil {
option.Preview = previewStr
}

result = append(result, option)
}
return
}

err = fmt.Errorf("input must be array of strings or tuples")
return
}
Expand Down
Loading

0 comments on commit 4c95a90

Please sign in to comment.