forked from marwan-at-work/iterm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.go
69 lines (63 loc) · 1.55 KB
/
tab.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
package iterm2
import (
"fmt"
"github.com/trzsz/iterm2/api"
"github.com/trzsz/iterm2/client"
)
// Tab abstracts an iTerm2 window tab
type Tab interface {
SetTitle(string) error
ListSessions() ([]Session, error)
}
type tab struct {
c *client.Client
id string
windowID string
}
func (t *tab) SetTitle(s string) error {
_, err := t.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_InvokeFunctionRequest{
InvokeFunctionRequest: &api.InvokeFunctionRequest{
Invocation: str(fmt.Sprintf(`iterm2.set_title(title: "%s")`, s)),
Context: &api.InvokeFunctionRequest_Method_{
Method: &api.InvokeFunctionRequest_Method{
Receiver: &t.id,
},
},
},
},
})
if err != nil {
return fmt.Errorf("could not call set_title: %w", err)
}
return nil
}
func (t *tab) ListSessions() ([]Session, error) {
list := []Session{}
resp, err := t.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_ListSessionsRequest{
ListSessionsRequest: &api.ListSessionsRequest{},
},
})
if err != nil {
return nil, fmt.Errorf("error listing sessions for tab %q: %w", t.id, err)
}
lsr := resp.GetListSessionsResponse()
for _, window := range lsr.GetWindows() {
if window.GetWindowId() != t.windowID {
continue
}
for _, wt := range window.GetTabs() {
if wt.GetTabId() != t.id {
continue
}
for _, link := range wt.GetRoot().GetLinks() {
list = append(list, &session{
c: t.c,
id: link.GetSession().GetUniqueIdentifier(),
})
}
}
}
return list, nil
}