Skip to content

Commit

Permalink
feat: disable animation on SSH
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 18, 2023
1 parent 96afdf6 commit d1757da
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 26 deletions.
19 changes: 16 additions & 3 deletions cmd/cy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,29 @@ if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
}
**/

func getEnv() map[string]string {
env := make(map[string]string)

for _, kv := range os.Environ() {
key, value, found := strings.Cut(kv, "=")
if !found {
continue
}
env[key] = value
}

return env
}

func buildHandshake(profile termenv.Profile) (*P.HandshakeMessage, error) {
columns, rows, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
return nil, err
}

return &P.HandshakeMessage{
TERM: os.Getenv("TERM"),
EDITOR: os.Getenv("EDITOR"),
Shell: getShell(),
Env: getEnv(),
Shell: getShell(),
Size: geom.Size{
R: rows,
C: columns,
Expand Down
10 changes: 9 additions & 1 deletion pkg/cy/api/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

cyParams "github.com/cfoust/cy/pkg/cy/params"
"github.com/cfoust/cy/pkg/fuzzy"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/janet"
Expand Down Expand Up @@ -46,6 +47,13 @@ func (i *InputModule) Find(
return nil, err
}

shouldAnimate := true
clientParams := client.Params()
animated, ok := clientParams.Get(cyParams.ParamAnimate)
if value, ok := animated.(bool); ok {
shouldAnimate = value
}

outerLayers := client.OuterLayers()
state := outerLayers.State()
cursor := state.Cursor
Expand All @@ -61,7 +69,7 @@ func (i *InputModule) Find(
fuzzy.WithInline(geom.Vec2{R: cursor.Y, C: cursor.X}),
}

if params.Animated == nil || (*params.Animated) == true {
if (params.Animated == nil || (*params.Animated) == true) && shouldAnimate {
settings = append(settings, fuzzy.WithAnimation(state.Image))
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cy/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package api
import (
"github.com/cfoust/cy/pkg/mux/screen"
"github.com/cfoust/cy/pkg/mux/screen/tree"
"github.com/cfoust/cy/pkg/params"
)

type Client interface {
Attach(tree.Node) error
Node() tree.Node
Params() *params.Parameters
OuterLayers() *screen.Layers
}
32 changes: 30 additions & 2 deletions pkg/cy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/cfoust/cy/pkg/bind"
cyParams "github.com/cfoust/cy/pkg/cy/params"
"github.com/cfoust/cy/pkg/events"
"github.com/cfoust/cy/pkg/frames"
"github.com/cfoust/cy/pkg/geom"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/cfoust/cy/pkg/mux/screen/toasts"
"github.com/cfoust/cy/pkg/mux/screen/tree"
"github.com/cfoust/cy/pkg/mux/stream/renderer"
"github.com/cfoust/cy/pkg/params"
"github.com/cfoust/cy/pkg/taro"
"github.com/cfoust/cy/pkg/util"

Expand All @@ -37,12 +39,17 @@ type Client struct {

cy *Cy

env Environment

node tree.Node
binds *bind.Engine[bind.Action]

// the text the client has copied
buffer string

// the client can have params of their own
params *params.Parameters

muxClient *server.Client
toast *ToastLogger
toaster *taro.Program
Expand Down Expand Up @@ -70,6 +77,7 @@ func (c *Cy) addClient(conn Connection) *Client {
Lifetime: util.NewLifetime(clientCtx),
cy: c,
conn: conn,
params: params.New(),
binds: bind.NewEngine[bind.Action](),
}
c.clients = append(c.clients, client)
Expand Down Expand Up @@ -297,15 +305,23 @@ func (c *Client) Resize(size geom.Vec2) {
c.renderer.Resize(size)
}

func isSSH(e Environment) bool {
return e.IsSet("SSH_CONNECTION") || e.IsSet("SSH_CLIENT") || e.IsSet("SSH_TTY")
}

func (c *Client) initialize(handshake *P.HandshakeMessage) error {
c.Lock()
defer c.Unlock()

info, err := terminfo.Load(handshake.TERM)
c.env = Environment(handshake.Env)

info, err := terminfo.Load(c.env.Default("TERM", "xterm-256color"))
if err != nil {
return err
}

isClientSSH := isSSH(c.env)

c.info = screen.RenderContext{
Terminfo: info,
Colors: handshake.Profile,
Expand Down Expand Up @@ -342,7 +358,7 @@ func (c *Client) initialize(handshake *P.HandshakeMessage) error {
screen.WithOpaque,
)

splashScreen := splash.New(c.Ctx(), handshake.Size)
splashScreen := splash.New(c.Ctx(), handshake.Size, !isClientSSH)
c.outerLayers.NewLayer(
splashScreen.Ctx(),
splashScreen,
Expand All @@ -366,6 +382,13 @@ func (c *Client) initialize(handshake *P.HandshakeMessage) error {
c.outerLayers,
)

if isClientSSH {
c.params.Set(cyParams.ParamAnimate, false)
c.toast.Send(toasts.Toast{
Message: "you joined via SSH; disabling animation",
})
}

go c.pollRender()

return nil
Expand Down Expand Up @@ -422,10 +445,15 @@ func (c *Client) Attach(node tree.Node) error {
}

c.binds.SetScopes(scopes...)
c.params.SetParent(node.Params())

return nil
}

func (c *Client) Params() *params.Parameters {
return c.params
}

func (c *Client) Detach(reason string) error {
err := c.conn.Send(P.CloseMessage{
Reason: reason,
Expand Down
1 change: 1 addition & 0 deletions pkg/cy/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
func (c *Cy) setDefaults(options Options) error {
defaults := map[string]interface{}{
params.ParamDataDirectory: options.DataDir,
params.ParamAnimate: true,
}

for key, value := range defaults {
Expand Down
25 changes: 25 additions & 0 deletions pkg/cy/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cy

type Environment map[string]string

func (e Environment) Get(key string) (string, bool) {
value, ok := e[key]
return value, ok
}

func (e Environment) Default(key string, defaultValue string) string {
value, ok := e[key]
if !ok {
value = defaultValue
}

return value
}

func (e Environment) IsSet(key string) bool {
value, ok := e[key]
if !ok {
return false
}
return len(value) > 0
}
21 changes: 14 additions & 7 deletions pkg/cy/janet.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,30 @@ func (c *Cy) initJanet(ctx context.Context, dataDir string) (*janet.VM, error) {
"cy/get": func(user interface{}, key *janet.Value) (interface{}, error) {
defer key.Free()

var keyword janet.Keyword
err := key.Unmarshal(&keyword)
if err != nil {
return nil, err
}

client, ok := user.(*Client)
if !ok {
return nil, fmt.Errorf("missing client context")
}

// First check the client's parameters
value, ok := client.params.Get(string(keyword))
if ok {
return value, nil
}

// Then those found in the tree
node := client.Node()
if node == nil {
return nil, fmt.Errorf("client was not attached")
}

var keyword janet.Keyword
err := key.Unmarshal(&keyword)
if err != nil {
return nil, err
}

value, ok := node.Params().Get(string(keyword))
value, ok = node.Params().Get(string(keyword))
return value, nil
},
"cy/set": func(user interface{}, key *janet.Value, value *janet.Value) error {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cy/params/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ package params

const (
// The directory in which .borg files will be saved.
// string, default: inferred from $XDG_DATA_HOME
ParamDataDirectory = "data-dir"
// Whether to enable animation.
// boolean, default: true
ParamAnimate = "animate"
)
3 changes: 1 addition & 2 deletions pkg/io/protocol/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func (i SizeMessage) Type() MessageType { return MessageTypeSize }

// The initial information necessary to render to the client.
type HandshakeMessage struct {
TERM string
EDITOR string
Env map[string]string
Shell string
Size geom.Vec2
Profile termenv.Profile
Expand Down
37 changes: 26 additions & 11 deletions pkg/mux/screen/splash/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"

"github.com/cfoust/cy/pkg/anim"
"github.com/cfoust/cy/pkg/frames"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/geom/image"
"github.com/cfoust/cy/pkg/geom/tty"
"github.com/cfoust/cy/pkg/mux"
"github.com/cfoust/cy/pkg/taro"
"github.com/cfoust/cy/pkg/util"
"github.com/cfoust/cy/pkg/version"
Expand All @@ -18,20 +20,20 @@ import (

type Splash struct {
util.Lifetime
anim *anim.Animator
render *taro.Renderer
bg mux.Screen
}

var _ taro.Model = (*Splash)(nil)

func (s *Splash) Init() taro.Cmd {
return taro.WaitScreens(s.Ctx(), s.anim)
return taro.WaitScreens(s.Ctx(), s.bg)
}

func (s *Splash) Update(msg tea.Msg) (taro.Model, tea.Cmd) {
switch msg.(type) {
case taro.ScreenUpdate:
return s, taro.WaitScreens(s.Ctx(), s.anim)
return s, taro.WaitScreens(s.Ctx(), s.bg)
case taro.KeyMsg:
return s, tea.Quit
}
Expand All @@ -43,7 +45,7 @@ func (s *Splash) View(state *tty.State) {
size := state.Image.Size()
state.CursorVisible = false

bg := s.anim.State().Image
bg := s.bg.State().Image
bgSize := bg.Size()
image.Copy(
geom.Vec2{
Expand Down Expand Up @@ -103,16 +105,29 @@ func (s *Splash) View(state *tty.State) {
)
}

func New(ctx context.Context, size geom.Size) *taro.Program {
func New(ctx context.Context, size geom.Size, shouldAnimate bool) *taro.Program {
render := taro.NewRenderer()
var bg mux.Screen
initial := generateBackground(render, size.Scalar(2))
if shouldAnimate {
bg = anim.NewAnimator(ctx, &anim.Midjo{}, initial, 23)
} else {
bg = frames.NewFramer(
ctx,
func(state image.Image) {
image.Copy(
geom.Vec2{},
state,
initial,
)
},
)
bg.Resize(size)
}

return taro.New(ctx, &Splash{
Lifetime: util.NewLifetime(ctx),
render: render,
anim: anim.NewAnimator(
ctx,
&anim.Midjo{},
generateBackground(render, size.Scalar(2)),
23,
),
bg: bg,
})
}
6 changes: 6 additions & 0 deletions pkg/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func (p *Parameters) NewChild() *Parameters {
return child
}

func (p *Parameters) SetParent(parent *Parameters) {
p.Lock()
p.parent = parent
p.Unlock()
}

func New() *Parameters {
return &Parameters{
table: make(map[string]interface{}),
Expand Down

0 comments on commit d1757da

Please sign in to comment.