Skip to content

Commit

Permalink
feat: break up big layout library
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Aug 6, 2024
1 parent ec0e576 commit 2aaa35b
Show file tree
Hide file tree
Showing 16 changed files with 593 additions and 578 deletions.
7 changes: 4 additions & 3 deletions pkg/cy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
P "github.com/cfoust/cy/pkg/io/protocol"
"github.com/cfoust/cy/pkg/janet"
"github.com/cfoust/cy/pkg/layout"
"github.com/cfoust/cy/pkg/layout/engine"
"github.com/cfoust/cy/pkg/mux"
"github.com/cfoust/cy/pkg/mux/screen"
"github.com/cfoust/cy/pkg/mux/screen/server"
Expand Down Expand Up @@ -54,7 +55,7 @@ type Client struct {
params *params.Parameters

muxClient *server.Client
layoutEngine *layout.LayoutEngine
layoutEngine *engine.LayoutEngine
toast *ToastLogger
toaster *taro.Program
frame *frames.Framer
Expand Down Expand Up @@ -237,11 +238,11 @@ func (c *Client) initialize(options ClientOptions) error {

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

c.layoutEngine = layout.NewLayoutEngine(
c.layoutEngine = engine.New(
c.Ctx(),
c.cy.tree,
c.cy.muxServer,
layout.WithParams(c.params),
engine.WithParams(c.params),
)

err = c.layoutEngine.Set(layout.New(layout.MarginsType{
Expand Down
4 changes: 2 additions & 2 deletions pkg/cy/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/layout"
"github.com/cfoust/cy/pkg/layout/split"
"github.com/cfoust/cy/pkg/mux"
S "github.com/cfoust/cy/pkg/mux/screen"
"github.com/cfoust/cy/pkg/stories"
Expand Down Expand Up @@ -234,7 +234,7 @@ func init() {
return nil, err
}

split := layout.NewSplit(
split := split.New(
ctx,
screenA,
screenB,
Expand Down
3 changes: 2 additions & 1 deletion pkg/input/fuzzy/preview/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/cfoust/cy/pkg/layout"
"github.com/cfoust/cy/pkg/layout/engine"
"github.com/cfoust/cy/pkg/mux"
"github.com/cfoust/cy/pkg/mux/screen/server"
"github.com/cfoust/cy/pkg/mux/screen/tree"
Expand All @@ -19,7 +20,7 @@ func NewLayout(
muxServer *server.Server,
args LayoutType,
) mux.Screen {
l := layout.NewLayoutEngine(
l := engine.New(
ctx,
tree,
muxServer,
Expand Down
24 changes: 21 additions & 3 deletions pkg/janet/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,24 @@ func isNamable(type_ reflect.Type) bool {
return getNamable(type_) != nil
}

func isMarshalable(type_ reflect.Type) bool {
if type_.Kind() != reflect.Pointer {
return false
}

_, ok := reflect.New(type_.Elem()).Interface().(Marshalable)
return ok
}

func isUnmarshalable(type_ reflect.Type) bool {
if type_.Kind() != reflect.Pointer {
return false
}

_, ok := reflect.New(type_.Elem()).Interface().(Unmarshalable)
return ok
}

func isParamType(type_ reflect.Type) bool {
if type_.Kind() == reflect.Pointer && isValidType(type_.Elem()) {
return true
Expand Down Expand Up @@ -404,7 +422,7 @@ func validateFunction(in, out []reflect.Type) error {
break
}

if !isSpecial(argType) && !isParamType(argType) && !isInterface(argType) {
if !isSpecial(argType) && !isUnmarshalable(argType) && !isParamType(argType) && !isInterface(argType) {
return fmt.Errorf(
"arg %d's type %s (%s) not supported",
i,
Expand All @@ -423,13 +441,13 @@ func validateFunction(in, out []reflect.Type) error {
// The first return value can be an error or valid type
if numResults == 1 {
first := out[0]
if !isParamType(first) && !isErrorType(first) && !isInterface(first) {
if !isParamType(first) && !isMarshalable(first) && !isErrorType(first) && !isInterface(first) {
return fmt.Errorf("first callback return type must be valid type or error")
}
}

if numResults == 2 {
if !isParamType(out[0]) && !isInterface(out[0]) {
if !isParamType(out[0]) && !isInterface(out[0]) && !isMarshalable(out[0]) {
return fmt.Errorf("first callback return type must be valid type")
}

Expand Down
44 changes: 7 additions & 37 deletions pkg/layout/borders.go → pkg/layout/borders/module.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package layout
package borders

import (
"context"

"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/geom/tty"
L "github.com/cfoust/cy/pkg/layout"
"github.com/cfoust/cy/pkg/mux"
"github.com/cfoust/cy/pkg/style"
"github.com/cfoust/cy/pkg/taro"
Expand All @@ -26,10 +27,10 @@ type Borders struct {
}

var _ mux.Screen = (*Borders)(nil)
var _ reusable = (*Borders)(nil)
var _ L.Reusable = (*Borders)(nil)

func (l *Borders) reuse(node NodeType) (bool, error) {
config, ok := node.(BorderType)
func (l *Borders) Apply(node L.NodeType) (bool, error) {
config, ok := node.(L.BorderType)
if !ok {
return false, nil
}
Expand Down Expand Up @@ -130,7 +131,7 @@ func (l *Borders) poll(ctx context.Context) {
case <-ctx.Done():
return
case event := <-updates.Recv():
if _, ok := event.(nodeChangeEvent); ok {
if _, ok := event.(L.NodeChangeEvent); ok {
continue
}
l.Publish(event)
Expand Down Expand Up @@ -170,38 +171,7 @@ func (l *Borders) Resize(size geom.Size) error {
return l.recalculate()
}

func (l *LayoutEngine) createBorders(
node *screenNode,
config BorderType,
) error {
innerNode, err := l.createNode(
node.Ctx(),
config.Node,
)
if err != nil {
return err
}

borders := NewBorders(
node.Ctx(),
innerNode.Screen,
)
borders.borderStyle = config.Border

if config.Title != nil {
borders.title = *config.Title
}

if config.TitleBottom != nil {
borders.titleBottom = *config.TitleBottom
}

node.Screen = borders
node.Children = []*screenNode{innerNode}
return nil
}

func NewBorders(ctx context.Context, screen mux.Screen) *Borders {
func New(ctx context.Context, screen mux.Screen) *Borders {
borders := &Borders{
UpdatePublisher: mux.NewPublisher(),
size: geom.DEFAULT_SIZE,
Expand Down
Loading

0 comments on commit 2aaa35b

Please sign in to comment.