-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
120 lines (106 loc) · 2.83 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package iterm2
import (
"fmt"
"io"
"marwan.io/iterm2/api"
"marwan.io/iterm2/client"
)
// App represents an open iTerm2 application
type App interface {
io.Closer
CreateWindow() (Window, error)
ListWindows() ([]Window, error)
SelectMenuItem(item string) error
Activate(raiseAllWindows, ignoreOtherApps bool) error
}
// NewApp establishes a connection
// with iTerm2 and returns an App.
// Name is an optional parameter that
// can be used to register your application
// name with iTerm2 so that it doesn't
// require explicit permissions every
// time you run the plugin.
func NewApp(name string) (App, error) {
c, err := client.New(name)
if err != nil {
return nil, err
}
return &app{c: c}, nil
}
type app struct {
c *client.Client
}
func (a *app) Activate(raiseAllWindows bool, ignoreOtherApps bool) error {
_, err := a.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_ActivateRequest{ActivateRequest: &api.ActivateRequest{
OrderWindowFront: b(true),
ActivateApp: &api.ActivateRequest_App{
RaiseAllWindows: &raiseAllWindows,
IgnoringOtherApps: &ignoreOtherApps,
},
}},
})
return err
}
func (a *app) CreateWindow() (Window, error) {
resp, err := a.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_CreateTabRequest{
CreateTabRequest: &api.CreateTabRequest{},
},
})
if err != nil {
return nil, fmt.Errorf("could not create window tab: %w", err)
}
ctr := resp.GetCreateTabResponse()
if ctr.GetStatus() != api.CreateTabResponse_OK {
return nil, fmt.Errorf("unexpected window tab status: %s", ctr.GetStatus())
}
return &window{
c: a.c,
id: ctr.GetWindowId(),
session: ctr.GetSessionId(),
}, nil
}
func (a *app) ListWindows() ([]Window, error) {
list := []Window{}
resp, err := a.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 _, w := range resp.GetListSessionsResponse().GetWindows() {
list = append(list, &window{
c: a.c,
id: w.GetWindowId(),
})
}
return list, nil
}
func (a *app) Close() error {
return a.c.Close()
}
func str(s string) *string {
return &s
}
func b(b bool) *bool {
return &b
}
func (a *app) SelectMenuItem(item string) error {
resp, err := a.c.Call(&api.ClientOriginatedMessage{
Submessage: &api.ClientOriginatedMessage_MenuItemRequest{
MenuItemRequest: &api.MenuItemRequest{
Identifier: &item,
},
},
})
if err != nil {
return fmt.Errorf("error selecting menu item %q: %w", item, err)
}
if resp.GetMenuItemResponse().GetStatus() != api.MenuItemResponse_OK {
return fmt.Errorf("menu item %q returned unexpected status: %q", item, resp.GetMenuItemResponse().GetStatus().String())
}
return nil
}