Repository navigation
Expand file tree
/
Copy pathpty-manager.ts
More file actions
80 lines (67 loc) · 1.65 KB
/
Copy pathpty-manager.ts
File metadata and controls
80 lines (67 loc) · 1.65 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
import { IPty, spawn } from 'node-pty'
import { EventEmitter } from 'events'
interface ManagedPty {
pty: IPty
tabId: string
cols: number
rows: number
}
export class PtyManager extends EventEmitter {
private ptys: Map<string, ManagedPty> = new Map()
spawn(tabId: string, cwd: string, cols = 120, rows = 30): string {
const shell = process.env.SHELL || '/bin/zsh'
const pty = spawn(shell, [], {
name: 'xterm-256color',
cols,
rows,
cwd,
env: {
...process.env,
TERM: 'xterm-256color',
COLORTERM: 'truecolor'
} as Record<string, string>
})
const managed: ManagedPty = { pty, tabId, cols, rows }
this.ptys.set(tabId, managed)
pty.onData((data: string) => {
this.emit('data', { tabId, data })
})
pty.onExit(({ exitCode }) => {
this.emit('exit', { tabId, exitCode })
this.ptys.delete(tabId)
})
return tabId
}
write(tabId: string, data: string) {
const managed = this.ptys.get(tabId)
if (managed) {
managed.pty.write(data)
}
}
resize(tabId: string, cols: number, rows: number) {
const managed = this.ptys.get(tabId)
if (managed) {
managed.pty.resize(cols, rows)
managed.cols = cols
managed.rows = rows
}
}
kill(tabId: string) {
const managed = this.ptys.get(tabId)
if (managed) {
managed.pty.kill()
this.ptys.delete(tabId)
}
}
killAll() {
for (const [id] of this.ptys) {
this.kill(id)
}
}
sendCommand(tabId: string, command: string) {
this.write(tabId, command + '\r')
}
has(tabId: string): boolean {
return this.ptys.has(tabId)
}
}