blob: 5bd71083c3ce13d4d4897592a52b28e844b0f1cd (
plain)
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
|
import { SessionMiddleware } from "tabby-terminal";
import { Observable, Subject } from "rxjs";
const oscPrefix = Buffer.from("\x1b]");
const oscSuffix = Buffer.from("\x07");
export default class OscNotifyMiddleware extends SessionMiddleware {
getTitle: () => string;
onClick: () => void;
constructor(getTitle: () => string, onClick: () => void) {
super();
this.getTitle = getTitle;
this.onClick = onClick;
}
get cwdReported$(): Observable<string> {
return this.cwdReported;
}
get copyRequested$(): Observable<string> {
return this.copyRequested;
}
private cwdReported = new Subject<string>();
private copyRequested = new Subject<string>();
private notifications: Notification[] = [];
feedFromSession(data: Buffer): void {
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 === 9) {
const body = oscParams.slice().join(";");
this.showNotification(this.getTitle(), body);
} else if (oscCode === 777 && oscParams[0] === "notify") {
const title = oscParams[1] || this.getTitle();
const body = oscParams.slice(2).join(";");
this.showNotification(title, body);
} else {
continue;
}
}
super.feedFromSession(data);
}
private showNotification(title: string, body: string): void {
const notification = new Notification(title || "Terminal", { body });
notification.onclick = () => this.onClick();
this.notifications.push(notification);
}
close(): void {
this.notifications.forEach((notification) => notification.close());
this.notifications = [];
this.cwdReported.complete();
this.copyRequested.complete();
super.close();
}
}
|