-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.go
97 lines (88 loc) · 2.49 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package iterm2
import (
"fmt"
"marwan.io/iterm2/api"
"marwan.io/iterm2/client"
)
// Session represents an iTerm2 Session which is a pane
// within a Tab where the terminal is active
type Session interface {
SendText(s string) error
Activate(selectTab, orderWindowFront bool) error
SplitPane(opts SplitPaneOptions) (Session, error)
GetSessionID() string
}
// SplitPaneOptions for customizing the new pane session.
// More options can be added here as needed
type SplitPaneOptions struct {
Vertical bool
}
type session struct {
c *client.Client
id string
}
func (s *session) SendText(t string) error {
resp, err := s.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_SendTextRequest{
SendTextRequest: &api.SendTextRequest{
Session: &s.id,
Text: &t,
},
},
})
if err != nil {
return fmt.Errorf("error sending text to session %q: %w", s.id, err)
}
if status := resp.GetSendTextResponse().GetStatus(); status != api.SendTextResponse_OK {
return fmt.Errorf("unexpected status for session %q: %s", s.id, status)
}
return nil
}
func (s *session) Activate(selectTab, orderWindowFront bool) error {
resp, err := s.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_ActivateRequest{
ActivateRequest: &api.ActivateRequest{
Identifier: &api.ActivateRequest_SessionId{
SessionId: s.id,
},
SelectTab: &selectTab,
OrderWindowFront: &orderWindowFront,
},
},
})
if err != nil {
return fmt.Errorf("error activating session %q: %w", s.id, err)
}
if status := resp.GetActivateResponse().GetStatus(); status != api.ActivateResponse_OK {
return fmt.Errorf("unexpected status for activate request: %s", status)
}
return nil
}
func (s *session) SplitPane(opts SplitPaneOptions) (Session, error) {
direction := api.SplitPaneRequest_HORIZONTAL.Enum()
if opts.Vertical {
direction = api.SplitPaneRequest_VERTICAL.Enum()
}
resp, err := s.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_SplitPaneRequest{
SplitPaneRequest: &api.SplitPaneRequest{
Session: &s.id,
SplitDirection: direction,
},
},
})
if err != nil {
return nil, fmt.Errorf("error splitting pane: %w", err)
}
spResp := resp.GetSplitPaneResponse()
if len(spResp.GetSessionId()) < 1 {
return nil, fmt.Errorf("expected at least one new session in split pane")
}
return &session{
c: s.c,
id: spResp.GetSessionId()[0],
}, nil
}
func (s *session) GetSessionID() string {
return s.id
}