gunk-dev/the-valley

client.go

source672eff5ffaecmain's published tip history
5.9 KiB
1package tmux
2
3import "context"
4
5// Client defines all tmux operations that Klaus uses.
6// Implementations can wrap the real tmux binary or provide
7// test doubles for unit testing.
8type Client interface {
9 // InSession returns true if we're currently inside a tmux session.
10 InSession() bool
11
12 // SplitWindow creates a new tmux pane by splitting vertically.
13 // Returns the new pane ID. The command runs in the specified dir.
14 SplitWindow(ctx context.Context, targetPane, dir, command string) (string, error)
15
16 // SplitWindowSized creates a new tmux pane with a specified size.
17 // orientation is "-v" for top/bottom or "-h" for left/right.
18 // size is passed to tmux's -l flag (e.g. "30%" or "15").
19 SplitWindowSized(ctx context.Context, targetPane, dir, command, orientation, size string) (string, error)
20
21 // SessionExists reports whether a tmux session with exactly this name exists.
22 SessionExists(ctx context.Context, session string) bool
23
24 // NewDetachedWindow creates a window running command in the named detached
25 // session, creating that session if it does not exist yet.
26 NewDetachedWindow(ctx context.Context, session, window, dir, command string) (string, error)
27
28 // SessionPanes returns the pane IDs in the named session.
29 SessionPanes(ctx context.Context, session string) ([]string, error)
30
31 // KillSession kills a tmux session by name.
32 KillSession(ctx context.Context, session string) error
33
34 // SetPaneTitle sets the title of a tmux pane.
35 SetPaneTitle(ctx context.Context, paneID, title string) error
36
37 // LockPaneTitle prevents applications in the pane from overriding the title.
38 LockPaneTitle(ctx context.Context, paneID string) error
39
40 // RebalanceLayout rebalances the pane layout for the window containing targetPane.
41 RebalanceLayout(ctx context.Context, targetPane string) error
42
43 // SwapPane swaps two tmux panes.
44 SwapPane(ctx context.Context, src, dst string) error
45
46 // ListWindowPanes returns the pane IDs for the window containing targetPane.
47 ListWindowPanes(ctx context.Context, targetPane string) ([]string, error)
48
49 // PaneExists checks if a tmux pane is still alive.
50 PaneExists(ctx context.Context, paneID string) bool
51
52 // PaneIsDead checks if a tmux pane's command has exited.
53 PaneIsDead(ctx context.Context, paneID string) bool
54
55 // PaneIsIdle checks if a tmux pane's command has finished running.
56 PaneIsIdle(ctx context.Context, paneID string) bool
57
58 // CapturePane captures the visible content of a pane.
59 CapturePane(ctx context.Context, paneID string, history int) (string, error)
60
61 // KillPane kills a tmux pane.
62 KillPane(ctx context.Context, paneID string) error
63
64 // SetWindowOption sets a tmux window option for the window containing target pane.
65 SetWindowOption(ctx context.Context, target, option, value string) error
66
67 // RenameWindow renames the tmux window containing the target pane.
68 RenameWindow(ctx context.Context, target, name string) error
69
70 // SendKeys types literal text into a pane (does not press Enter).
71 SendKeys(ctx context.Context, paneID, keys string) error
72
73 // SelectPane moves tmux focus to the given pane.
74 SelectPane(ctx context.Context, paneID string) error
75}
76
77// ExecClient implements Client by shelling out to the tmux binary.
78type ExecClient struct{}
79
80// NewExecClient returns a Client backed by the real tmux binary.
81func NewExecClient() *ExecClient { return &ExecClient{} }
82
83func (c *ExecClient) InSession() bool { return InSession() }
84
85func (c *ExecClient) SplitWindow(ctx context.Context, targetPane, dir, command string) (string, error) {
86 return SplitWindow(ctx, targetPane, dir, command)
87}
88
89func (c *ExecClient) SplitWindowSized(ctx context.Context, targetPane, dir, command, orientation, size string) (string, error) {
90 return SplitWindowSized(ctx, targetPane, dir, command, orientation, size)
91}
92
93func (c *ExecClient) SessionExists(ctx context.Context, session string) bool {
94 return SessionExists(ctx, session)
95}
96
97func (c *ExecClient) NewDetachedWindow(ctx context.Context, session, window, dir, command string) (string, error) {
98 return NewDetachedWindow(ctx, session, window, dir, command)
99}
100
101func (c *ExecClient) SessionPanes(ctx context.Context, session string) ([]string, error) {
102 return SessionPanes(ctx, session)
103}
104
105func (c *ExecClient) KillSession(ctx context.Context, session string) error {
106 return KillSession(ctx, session)
107}
108
109func (c *ExecClient) SetPaneTitle(ctx context.Context, paneID, title string) error {
110 return SetPaneTitle(ctx, paneID, title)
111}
112
113func (c *ExecClient) LockPaneTitle(ctx context.Context, paneID string) error {
114 return LockPaneTitle(ctx, paneID)
115}
116
117func (c *ExecClient) RebalanceLayout(ctx context.Context, targetPane string) error {
118 return RebalanceLayout(ctx, targetPane)
119}
120
121func (c *ExecClient) SwapPane(ctx context.Context, src, dst string) error {
122 return SwapPane(ctx, src, dst)
123}
124
125func (c *ExecClient) ListWindowPanes(ctx context.Context, targetPane string) ([]string, error) {
126 return ListWindowPanes(ctx, targetPane)
127}
128
129func (c *ExecClient) PaneExists(ctx context.Context, paneID string) bool {
130 return PaneExists(ctx, paneID)
131}
132
133func (c *ExecClient) PaneIsDead(ctx context.Context, paneID string) bool {
134 return PaneIsDead(ctx, paneID)
135}
136
137func (c *ExecClient) PaneIsIdle(ctx context.Context, paneID string) bool {
138 return PaneIsIdle(ctx, paneID)
139}
140
141func (c *ExecClient) CapturePane(ctx context.Context, paneID string, history int) (string, error) {
142 return CapturePane(ctx, paneID, history)
143}
144
145func (c *ExecClient) KillPane(ctx context.Context, paneID string) error {
146 return KillPane(ctx, paneID)
147}
148
149func (c *ExecClient) SetWindowOption(ctx context.Context, target, option, value string) error {
150 return SetWindowOption(ctx, target, option, value)
151}
152
153func (c *ExecClient) RenameWindow(ctx context.Context, target, name string) error {
154 return RenameWindow(ctx, target, name)
155}
156
157func (c *ExecClient) SendKeys(ctx context.Context, paneID, keys string) error {
158 return SendKeys(ctx, paneID, keys)
159}
160
161func (c *ExecClient) SelectPane(ctx context.Context, paneID string) error {
162 return SelectPane(ctx, paneID)
163}