summaryrefslogtreecommitdiff
path: root/src/osc-notify-middleware.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/osc-notify-middleware.ts')
-rw-r--r--src/osc-notify-middleware.ts36
1 files changed, 29 insertions, 7 deletions
diff --git a/src/osc-notify-middleware.ts b/src/osc-notify-middleware.ts
index 15548f4..dcfcc1a 100644
--- a/src/osc-notify-middleware.ts
+++ b/src/osc-notify-middleware.ts
@@ -4,13 +4,25 @@ import { Observable, Subject } from "rxjs";
const oscPrefix = Buffer.from("\x1b]");
const oscSuffix = Buffer.from("\x07");
+function cleanTitle(raw: string): string {
+ const t = (raw ?? "").trim();
+ if (!t) return "Terminal";
+ if (t.includes("/") || t.includes("\\")) {
+ const segs = t.split(/[/\\]/).filter(s => s.length > 0);
+ return segs.length ? segs[segs.length - 1] : t;
+ }
+ return t;
+}
+
export default class OscNotifyMiddleware extends SessionMiddleware {
- tabTitle: string;
+ getTitle: () => string;
+ onClick: () => void;
- constructor(tabTitle: string) {
+ constructor(getTitle: () => string, onClick: () => void) {
super();
- this.tabTitle = tabTitle;
+ this.getTitle = getTitle;
+ this.onClick = onClick;
}
get cwdReported$(): Observable<string> {
@@ -22,6 +34,7 @@ export default class OscNotifyMiddleware extends SessionMiddleware {
private cwdReported = new Subject<string>();
private copyRequested = new Subject<string>();
+ private notifications: Notification[] = [];
feedFromSession(data: Buffer): void {
let startIndex = 0;
@@ -36,10 +49,11 @@ export default class OscNotifyMiddleware extends SessionMiddleware {
const oscCode = parseInt(oscCodeString);
if (oscCode === 9) {
- // TODO: onclick switch to tab
- new Notification(this.tabTitle, {
- body: oscParams[0]
- });
+ this.showNotification(cleanTitle(this.getTitle()), oscParams[0] ?? "");
+ } else if (oscCode === 777 && oscParams[0] === "notify") {
+ const title = oscParams[1] || cleanTitle(this.getTitle());
+ const body = oscParams.slice(2).join(";");
+ this.showNotification(title, body);
} else {
continue;
}
@@ -48,7 +62,15 @@ export default class OscNotifyMiddleware extends SessionMiddleware {
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();