/
osc1337Processing.ts
51 lines (43 loc) · 1.88 KB
/
osc1337Processing.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
import * as os from 'os'
import { Subject, Observable } from 'rxjs'
const OSCPrefix = Buffer.from('\x1b]')
const OSCSuffix = Buffer.from('\x07')
export class OSCProcessor {
get cwdReported$ (): Observable<string> { return this.cwdReported }
get copyRequested$ (): Observable<string> { return this.copyRequested }
private cwdReported = new Subject<string>()
private copyRequested = new Subject<string>()
process (data: Buffer): Buffer {
let startIndex = 0
while (data.includes(OSCPrefix, startIndex) && data.includes(OSCSuffix, startIndex)) {
const params = data.subarray(data.indexOf(OSCPrefix, startIndex) + OSCPrefix.length)
const oscString = params.subarray(0, params.indexOf(OSCSuffix)).toString()
startIndex = data.indexOf(OSCSuffix, startIndex) + OSCSuffix.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') {
const content = Buffer.from(oscParams[1], 'base64')
this.copyRequested.next(content.toString())
}
} else {
continue
}
}
return data
}
close (): void {
this.cwdReported.complete()
}
}