-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindow.go
96 lines (88 loc) · 2.26 KB
/
window.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
package iterm2
import (
"fmt"
"strconv"
"marwan.io/iterm2/api"
"marwan.io/iterm2/client"
)
// Window represents an iTerm2 Window
type Window interface {
SetTitle(s string) error
CreateTab() (Tab, error)
ListTabs() ([]Tab, error)
Activate() error
}
type window struct {
c *client.Client
id string
session string
}
func (w *window) CreateTab() (Tab, error) {
resp, err := w.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_CreateTabRequest{
CreateTabRequest: &api.CreateTabRequest{
WindowId: str(w.id),
},
},
})
if err != nil {
return nil, fmt.Errorf("could not create tab for window %q: %w", w.id, err)
}
ctr := resp.GetCreateTabResponse()
if ctr.GetStatus() != api.CreateTabResponse_OK {
return nil, fmt.Errorf("unexpected tab status: %s", ctr.GetStatus())
}
return &tab{
c: w.c,
id: strconv.Itoa(int(ctr.GetTabId())),
windowID: w.id,
}, nil
}
func (w *window) ListTabs() ([]Tab, error) {
list := []Tab{}
resp, err := w.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_ListSessionsRequest{
ListSessionsRequest: &api.ListSessionsRequest{},
},
})
if err != nil {
return nil, fmt.Errorf("could not list sessions: %w", err)
}
for _, window := range resp.GetListSessionsResponse().GetWindows() {
if window.GetWindowId() != w.id {
continue
}
for _, t := range window.GetTabs() {
list = append(list, &tab{
c: w.c,
id: t.GetTabId(),
windowID: w.id,
})
}
}
return list, nil
}
func (w *window) SetTitle(s string) error {
_, err := w.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: &w.id,
},
},
},
},
})
return err
}
func (w *window) Activate() error {
_, err := w.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_ActivateRequest{ActivateRequest: &api.ActivateRequest{
Identifier: &api.ActivateRequest_WindowId{WindowId: w.id},
OrderWindowFront: b(true),
}},
})
return err
}