-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpty.go
More file actions
216 lines (193 loc) · 4.32 KB
/
pty.go
File metadata and controls
216 lines (193 loc) · 4.32 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"context"
"io"
"log/slog"
"os"
"os/exec"
"sync"
"github.com/creack/pty"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// PTYSession represents an active PTY
type PTYSession struct {
id string
cmd *exec.Cmd
pty *os.File
cancel chan struct{}
}
// PTYManager manages multiple PTY sessions
type PTYManager struct {
ctx context.Context
sessions map[string]*PTYSession
mu sync.RWMutex
}
func NewPTYManager(ctx context.Context) *PTYManager {
return &PTYManager{
ctx: ctx,
sessions: make(map[string]*PTYSession),
}
}
// StartTerminalListeners registers event handlers for terminal operations
func (a *App) StartTerminalListeners() {
if a.ptyManager == nil {
a.ptyManager = NewPTYManager(a.ctx)
}
runtime.EventsOn(a.ctx, "terminal:start", func(data ...interface{}) {
params, ok := firstAs[map[string]interface{}](data)
if !ok {
slog.Error("terminal:start invalid params")
return
}
id := mapStr(params, "id")
cols := mapInt(params, "cols")
rows := mapInt(params, "rows")
if cols == 0 {
cols = 80
}
if rows == 0 {
rows = 24
}
slog.Info("terminal:start", "id", id, "cols", cols, "rows", rows)
if err := a.ptyManager.Start(id, uint16(cols), uint16(rows)); err != nil {
slog.Error("terminal start failed", "id", id, "error", err)
}
})
runtime.EventsOn(a.ctx, "terminal:input", func(data ...interface{}) {
params, ok := firstAs[map[string]interface{}](data)
if !ok {
slog.Error("terminal:input invalid params")
return
}
id := mapStr(params, "id")
input := mapStr(params, "data")
slog.Debug("terminal:input", "id", id, "len", len(input))
a.ptyManager.Write(id, []byte(input))
})
runtime.EventsOn(a.ctx, "terminal:resize", func(data ...interface{}) {
params, ok := firstAs[map[string]interface{}](data)
if !ok {
return
}
id := mapStr(params, "id")
cols := mapInt(params, "cols")
rows := mapInt(params, "rows")
a.ptyManager.Resize(id, uint16(cols), uint16(rows))
})
runtime.EventsOn(a.ctx, "terminal:stop", func(data ...interface{}) {
params, ok := firstAs[map[string]interface{}](data)
if !ok {
return
}
id := mapStr(params, "id")
a.ptyManager.Stop(id)
})
}
// Start creates a new PTY session
func (m *PTYManager) Start(id string, cols, rows uint16) error {
m.mu.Lock()
defer m.mu.Unlock()
// Stop existing session if any
if s, ok := m.sessions[id]; ok {
select {
case <-s.cancel:
// Already closed
default:
close(s.cancel)
}
s.pty.Close()
s.cmd.Process.Kill()
s.cmd.Wait()
delete(m.sessions, id)
}
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/bash"
}
cmd := exec.Command(shell)
cmd.Env = os.Environ()
ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: cols, Rows: rows})
if err != nil {
return err
}
session := &PTYSession{
id: id,
cmd: cmd,
pty: ptmx,
cancel: make(chan struct{}),
}
m.sessions[id] = session
// Read loop - emit output to frontend
go m.readLoop(session)
return nil
}
func (m *PTYManager) readLoop(session *PTYSession) {
buf := make([]byte, 4096)
for {
select {
case <-session.cancel:
return
default:
n, err := session.pty.Read(buf)
if err != nil {
if err != io.EOF {
// PTY closed or error
}
return
}
if n > 0 {
runtime.EventsEmit(m.ctx, "terminal:"+session.id+":output", string(buf[:n]))
}
}
}
}
// Write sends input to a PTY session
func (m *PTYManager) Write(id string, data []byte) {
m.mu.RLock()
s := m.sessions[id]
m.mu.RUnlock()
if s != nil {
s.pty.Write(data)
}
}
// Resize changes the PTY window size
func (m *PTYManager) Resize(id string, cols, rows uint16) {
m.mu.RLock()
s := m.sessions[id]
m.mu.RUnlock()
if s != nil {
pty.Setsize(s.pty, &pty.Winsize{Cols: cols, Rows: rows})
}
}
// Stop terminates a PTY session
func (m *PTYManager) Stop(id string) {
m.mu.Lock()
defer m.mu.Unlock()
if s, ok := m.sessions[id]; ok {
select {
case <-s.cancel:
default:
close(s.cancel)
}
s.pty.Close()
s.cmd.Process.Kill()
s.cmd.Wait()
delete(m.sessions, id)
}
}
// StopAll terminates all PTY sessions
func (m *PTYManager) StopAll() {
m.mu.Lock()
defer m.mu.Unlock()
for _, s := range m.sessions {
select {
case <-s.cancel:
default:
close(s.cancel)
}
s.pty.Close()
s.cmd.Process.Kill()
s.cmd.Wait()
}
m.sessions = make(map[string]*PTYSession)
}