|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\Tmux; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Process; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Service for managing individual tmux panes |
|
9
|
|
|
*/ |
|
10
|
|
|
class TmuxPaneManager |
|
11
|
|
|
{ |
|
12
|
|
|
protected string $sessionName; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(string $sessionName) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->sessionName = $sessionName; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new window |
|
21
|
|
|
*/ |
|
22
|
|
|
public function createWindow(int $index, string $name): bool |
|
23
|
|
|
{ |
|
24
|
|
|
$result = Process::timeout(10)->run( |
|
25
|
|
|
"tmux new-window -t {$this->sessionName}:{$index} -n {$name} 'printf \"\\033]2;{$name}\\033\"'" |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
return $result->successful(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Split a pane horizontally |
|
33
|
|
|
*/ |
|
34
|
|
|
public function splitHorizontal(string $target, string $percentage, string $title = ''): bool |
|
35
|
|
|
{ |
|
36
|
|
|
$titleCmd = $title ? "printf \"\\033]2;{$title}\\033\"" : ''; |
|
37
|
|
|
|
|
38
|
|
|
$result = Process::timeout(10)->run( |
|
39
|
|
|
"tmux splitw -t {$this->sessionName}:{$target} -h -l {$percentage} '{$titleCmd}'" |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return $result->successful(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Split a pane vertically |
|
47
|
|
|
*/ |
|
48
|
|
|
public function splitVertical(string $target, string $percentage, string $title = ''): bool |
|
49
|
|
|
{ |
|
50
|
|
|
$titleCmd = $title ? "printf \"\\033]2;{$title}\\033\"" : ''; |
|
51
|
|
|
|
|
52
|
|
|
$result = Process::timeout(10)->run( |
|
53
|
|
|
"tmux splitw -t {$this->sessionName}:{$target} -v -l {$percentage} '{$titleCmd}'" |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return $result->successful(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Select a specific pane |
|
61
|
|
|
*/ |
|
62
|
|
|
public function selectPane(string $target): bool |
|
63
|
|
|
{ |
|
64
|
|
|
$result = Process::timeout(5)->run( |
|
65
|
|
|
"tmux selectp -t {$this->sessionName}:{$target}" |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
return $result->successful(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Select a specific window |
|
73
|
|
|
*/ |
|
74
|
|
|
public function selectWindow(int $window): bool |
|
75
|
|
|
{ |
|
76
|
|
|
$result = Process::timeout(5)->run( |
|
77
|
|
|
"tmux select-window -t {$this->sessionName}:{$window}" |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return $result->successful(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Respawn a pane with a new command |
|
85
|
|
|
*/ |
|
86
|
|
|
public function respawnPane(string $target, string $command, bool $kill = false): bool |
|
87
|
|
|
{ |
|
88
|
|
|
$killFlag = $kill ? '-k' : ''; |
|
89
|
|
|
|
|
90
|
|
|
$result = Process::timeout(10)->run( |
|
91
|
|
|
"tmux respawnp {$killFlag} -t {$this->sessionName}:{$target} '{$command}'" |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
return $result->successful(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Send keys to a pane |
|
99
|
|
|
*/ |
|
100
|
|
|
public function sendKeys(string $target, string $keys, bool $enter = true): bool |
|
101
|
|
|
{ |
|
102
|
|
|
$enterKey = $enter ? ' Enter' : ''; |
|
103
|
|
|
|
|
104
|
|
|
$result = Process::timeout(5)->run( |
|
105
|
|
|
"tmux send-keys -t {$this->sessionName}:{$target} '{$keys}'{$enterKey}" |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return $result->successful(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Kill a specific pane |
|
113
|
|
|
*/ |
|
114
|
|
|
public function killPane(string $target): bool |
|
115
|
|
|
{ |
|
116
|
|
|
$result = Process::timeout(5)->run( |
|
117
|
|
|
"tmux kill-pane -t {$this->sessionName}:{$target}" |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
return $result->successful(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set pane title |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setPaneTitle(string $target, string $title): bool |
|
127
|
|
|
{ |
|
128
|
|
|
$result = Process::timeout(5)->run( |
|
129
|
|
|
"tmux select-pane -t {$this->sessionName}:{$target} -T '{$title}'" |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
return $result->successful(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get pane title |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getPaneTitle(string $target): ?string |
|
139
|
|
|
{ |
|
140
|
|
|
$result = Process::timeout(5)->run( |
|
141
|
|
|
"tmux display-message -p -t {$this->sessionName}:{$target} '#{pane_title}'" |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
return $result->successful() ? trim($result->output()) : null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Capture pane content |
|
149
|
|
|
*/ |
|
150
|
|
|
public function capturePane(string $target, int $lines = 100): string |
|
151
|
|
|
{ |
|
152
|
|
|
$result = Process::timeout(10)->run( |
|
153
|
|
|
"tmux capture-pane -p -t {$this->sessionName}:{$target} -S -{$lines}" |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
return $result->successful() ? $result->output() : ''; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|