-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathoscProcessing.ts
65 lines (54 loc) · 2.43 KB
/
oscProcessing.ts
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
import * as os from 'os'
import { Subject, Observable } from 'rxjs'
import { SessionMiddleware } from '../api/middleware'
const OSCPrefix = Buffer.from('\x1b]')
const OSCSuffixes = [Buffer.from('\x07'), Buffer.from('\x1b\\')]
export class OSCProcessor extends SessionMiddleware {
get cwdReported$ (): Observable<string> { return this.cwdReported }
get copyRequested$ (): Observable<string> { return this.copyRequested }
private cwdReported = new Subject<string>()
private copyRequested = new Subject<string>()
feedFromSession (data: Buffer): void {
let startIndex = 0
while (data.includes(OSCPrefix, startIndex)) {
const si = startIndex
if (!OSCSuffixes.some(s => data.includes(s, si))) {
break
}
const params = data.subarray(data.indexOf(OSCPrefix, startIndex) + OSCPrefix.length)
const [closesSuffix, closestSuffixIndex] = OSCSuffixes
.map((suffix): [Buffer, number] => [suffix, params.indexOf(suffix)])
.filter(([_, index]) => index !== -1)
.sort(([_, a], [__, b]) => a - b)[0]
const oscString = params.subarray(0, closestSuffixIndex).toString()
startIndex = data.indexOf(closesSuffix, startIndex) + closesSuffix.length
const [oscCodeString, ...oscParams] = oscString.split(';')
const oscCode = parseInt(oscCodeString)
if (oscCode === 1337) {
const paramString = oscParams.join(';')
if (paramString.startsWith('CurrentDir=')) {
let reportedCWD = paramString.split('=')[1]
if (reportedCWD.startsWith('~')) {
reportedCWD = os.homedir() + reportedCWD.substring(1)
}
this.cwdReported.next(reportedCWD)
} else {
console.debug('Unsupported OSC 1337 parameter:', paramString)
}
} else if (oscCode === 52) {
if (oscParams[0] === 'c' || oscParams[0] === '') {
const content = Buffer.from(oscParams[1], 'base64')
this.copyRequested.next(content.toString())
}
} else {
continue
}
}
super.feedFromSession(data)
}
close (): void {
this.cwdReported.complete()
this.copyRequested.complete()
super.close()
}
}